fix memory leaks found with valgrind

This commit is contained in:
Josh Coalson
2002-12-04 07:01:37 +00:00
parent 44ca9fedaa
commit 4fa90599fe
7 changed files with 41 additions and 12 deletions

View File

@@ -980,8 +980,14 @@ int parse_option(int short_option, const char *long_option, const char *option_a
void free_options() void free_options()
{ {
if(0 != option_values.filenames) unsigned i;
if(0 != option_values.filenames) {
for(i = 0; i < option_values.num_files; i++) {
if(0 != option_values.filenames[i])
free(option_values.filenames[i]);
}
free(option_values.filenames); free(option_values.filenames);
}
if(0 != option_values.vorbis_comment) if(0 != option_values.vorbis_comment)
FLAC__metadata_object_delete(option_values.vorbis_comment); FLAC__metadata_object_delete(option_values.vorbis_comment);
} }

View File

@@ -392,8 +392,10 @@ FLAC_API FLAC__StreamMetadata *FLAC__metadata_object_clone(const FLAC__StreamMet
} }
break; break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT: case FLAC__METADATA_TYPE_VORBIS_COMMENT:
if(0 != to->data.vorbis_comment.vendor_string.entry) if(0 != to->data.vorbis_comment.vendor_string.entry) {
free(to->data.vorbis_comment.vendor_string.entry); free(to->data.vorbis_comment.vendor_string.entry);
to->data.vorbis_comment.vendor_string.entry = 0;
}
if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) { if(!copy_vcentry_(&to->data.vorbis_comment.vendor_string, &object->data.vorbis_comment.vendor_string)) {
FLAC__metadata_object_delete(to); FLAC__metadata_object_delete(to);
return 0; return 0;
@@ -446,16 +448,22 @@ void FLAC__metadata_object_delete_data(FLAC__StreamMetadata *object)
case FLAC__METADATA_TYPE_PADDING: case FLAC__METADATA_TYPE_PADDING:
break; break;
case FLAC__METADATA_TYPE_APPLICATION: case FLAC__METADATA_TYPE_APPLICATION:
if(0 != object->data.application.data) if(0 != object->data.application.data) {
free(object->data.application.data); free(object->data.application.data);
object->data.application.data = 0;
}
break; break;
case FLAC__METADATA_TYPE_SEEKTABLE: case FLAC__METADATA_TYPE_SEEKTABLE:
if(0 != object->data.seek_table.points) if(0 != object->data.seek_table.points) {
free(object->data.seek_table.points); free(object->data.seek_table.points);
object->data.seek_table.points = 0;
}
break; break;
case FLAC__METADATA_TYPE_VORBIS_COMMENT: case FLAC__METADATA_TYPE_VORBIS_COMMENT:
if(0 != object->data.vorbis_comment.vendor_string.entry) if(0 != object->data.vorbis_comment.vendor_string.entry) {
free(object->data.vorbis_comment.vendor_string.entry); free(object->data.vorbis_comment.vendor_string.entry);
object->data.vorbis_comment.vendor_string.entry = 0;
}
if(0 != object->data.vorbis_comment.comments) { if(0 != object->data.vorbis_comment.comments) {
FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0); FLAC__ASSERT(object->data.vorbis_comment.num_comments > 0);
vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments); vorbiscomment_entry_array_delete_(object->data.vorbis_comment.comments, object->data.vorbis_comment.num_comments);
@@ -1144,7 +1152,7 @@ FLAC_API FLAC__bool FLAC__metadata_object_cuesheet_track_delete_index(FLAC__Stre
track = &object->data.cue_sheet.tracks[track_num]; track = &object->data.cue_sheet.tracks[track_num];
/* move all indices > index_num backward one space */ /* move all indices > index_num backward one space */
memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Track)*(track->num_indices-index_num-1)); memmove(&track->indices[index_num], &track->indices[index_num+1], sizeof(FLAC__StreamMetadata_CueSheet_Index)*(track->num_indices-index_num-1));
FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1); FLAC__metadata_object_cuesheet_track_resize_indices(object, track_num, track->num_indices-1);
cuesheet_calculate_length_(object); cuesheet_calculate_length_(object);

View File

@@ -296,8 +296,7 @@ FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
FLAC__ASSERT(0 != decoder); FLAC__ASSERT(0 != decoder);
if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
return; return;
if(decoder->private_->has_seek_table) { if(0 != decoder->private_->seek_table.data.seek_table.points) {
FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
free(decoder->private_->seek_table.data.seek_table.points); free(decoder->private_->seek_table.data.seek_table.points);
decoder->private_->seek_table.data.seek_table.points = 0; decoder->private_->seek_table.data.seek_table.points = 0;
decoder->private_->has_seek_table = false; decoder->private_->has_seek_table = false;
@@ -1030,7 +1029,8 @@ FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_
decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) { /* use realloc since we may pass through here several times (e.g. after seeking) */
if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
return false; return false;
} }

View File

@@ -506,7 +506,7 @@ FLAC_API void FLAC__stream_encoder_delete(FLAC__StreamEncoder *encoder)
FLAC__stream_encoder_finish(encoder); FLAC__stream_encoder_finish(encoder);
if(encoder->protected_->verify && 0 != encoder->private_->verify.decoder) if(0 != encoder->private_->verify.decoder)
FLAC__stream_decoder_delete(encoder->private_->verify.decoder); FLAC__stream_decoder_delete(encoder->private_->verify.decoder);
for(i = 0; i < FLAC__MAX_CHANNELS; i++) { for(i = 0; i < FLAC__MAX_CHANNELS; i++) {

View File

@@ -294,8 +294,13 @@ void free_options(CommandLineOptions *options)
if(0 != options->args.arguments) if(0 != options->args.arguments)
free(options->args.arguments); free(options->args.arguments);
if(0 != options->filenames) if(0 != options->filenames) {
for(i = 0; i < options->num_files; i++) {
if(0 != options->filenames[i])
free(options->filenames[i]);
}
free(options->filenames); free(options->filenames);
}
} }
/* /*

View File

@@ -730,9 +730,17 @@ FLAC__bool test_bitbuffer()
} }
printf("OK\n"); printf("OK\n");
printf("testing free... OK\n"); printf("testing free... ");
FLAC__bitbuffer_free(bb); FLAC__bitbuffer_free(bb);
FLAC__bitbuffer_free(bbcopy); FLAC__bitbuffer_free(bbcopy);
printf("OK\n");
printf("testing delete... ");
FLAC__bitbuffer_delete(bb);
FLAC__bitbuffer_delete(bb_zero);
FLAC__bitbuffer_delete(bb_one);
FLAC__bitbuffer_delete(bbcopy);
printf("OK\n");
printf("\nPASSED!\n"); printf("\nPASSED!\n");
return true; return true;

View File

@@ -482,6 +482,7 @@ static FLAC__bool test_level_1_()
return die_("mismatch in min_blocksize"); return die_("mismatch in min_blocksize");
if(block->data.stream_info.max_blocksize != 576) if(block->data.stream_info.max_blocksize != 576)
return die_("mismatch in max_blocksize"); return die_("mismatch in max_blocksize");
FLAC__metadata_object_delete(block);
if(!FLAC__metadata_simple_iterator_next(iterator)) if(!FLAC__metadata_simple_iterator_next(iterator))
return die_("forward iterator ended early"); return die_("forward iterator ended early");
@@ -502,6 +503,7 @@ static FLAC__bool test_level_1_()
/* check to see if some basic data matches (c.f. generate_file_()) */ /* check to see if some basic data matches (c.f. generate_file_()) */
if(block->length != 1234) if(block->length != 1234)
return die_("bad PADDING length"); return die_("bad PADDING length");
FLAC__metadata_object_delete(block);
if(FLAC__metadata_simple_iterator_next(iterator)) if(FLAC__metadata_simple_iterator_next(iterator))
return die_("forward iterator returned true but should have returned false"); return die_("forward iterator returned true but should have returned false");