[Vorbis-dev] Memory Leak in vorbis_info_clear()

William Ahern william at 25thandClement.com
Mon May 14 12:10:14 PDT 2012


I'm having trouble tracking down why it leaks, but below is an example
program which shows--using valgrind--that vorbis_info_clear() leaks memory
if called before vorbis_dsp_clear(), but not if called after
vorbis_dsp_clear(). Just compile and run under valgrind, using the -l switch
to the example program to trigger a leak. Tested under OS X 10.7 and Ubuntu
12.04.

This may be by design, or just, "don't do that". If it is the Wiki page
should probably be edited to mention this. I tried, but it didn't look like
I could make the edit.

My solution is to clear in reverse order of initialization, though I've
rearranged the order of things in the example program to highlight the
immediate issue.

/* compile: cc -Wall -o vbs vbs.c -lvorbisenc -lvorbis */
/* noleak:  valgrind --leak-check=yes ./vbs */
/* leak:    valgrind --leak-check=yes ./vbs -l */
#include <unistd.h> /* getopt(3) */

#include <vorbis/codec.h>
#include <vorbis/vorbisenc.h>


static struct vorbis_info info;
static struct vorbis_dsp_state dsp;
static struct vorbis_comment comment;
static struct vorbis_block block;


void init(void) {
	struct { ogg_packet ident, comment, setup; } pkt;

	vorbis_info_init(&info);
	vorbis_encode_init_vbr(&info, 2, 44100, 1.0);
	vorbis_analysis_init(&dsp, &info);
	vorbis_comment_init(&comment);
	vorbis_analysis_headerout(&dsp, &comment,
		&pkt.ident, &pkt.comment, &pkt.setup);
	vorbis_block_init(&dsp, &block);
} /* init() */


void destroy(_Bool leak) {
	vorbis_block_clear(&block);
	if (leak)
		vorbis_info_clear(&info);
	vorbis_dsp_clear(&dsp);
	if (!leak)
		vorbis_info_clear(&info);
	vorbis_comment_clear(&comment);
} /* destroy() */


int main(int argc, char *argv[]) {
	_Bool leak = 0;
	int opt;

	while (-1 != (opt = getopt(argc, argv, "l"))) {
		switch (opt) {
		case 'l':
			leak = 1;
			break;
		}
	}

	init();
	destroy(leak);

	return 0;
} /* main() */


More information about the Vorbis-dev mailing list