Repository navigation
Conversation
HasEncoder, HasDecoder, NewAudioEncoderByName, AudioEncoder.SetOption, AudioEncoder.GetOption and the Resampler setup passed C.CString results directly as arguments, so the strings were never freed. None of the callees keep the pointer (av_dict_set copies key and value with flags 0), so free each string after the call. Fixes deepch#130
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #130
What changed
Several functions in
cgo/ffmpegpassedC.CString(...)directly as a call argument, so the C strings were never freed:HasEncoder,HasDecoder(ffmpeg.go)NewAudioEncoderByName,AudioEncoder.SetOption,AudioEncoder.GetOption(audio.go)av_opt_set_intcalls inResampler.Resample(audio.go)Each string is now bound to a variable and released with
defer C.free(...). The sixav_opt_set_intcalls go through a smallavOptSetInthelper that does the same.<stdlib.h>is added toffmpeg.hforC.free.Why it is safe
None of the callees keep the pointer after returning:
avcodec_find_encoder_by_name/avcodec_find_decoder_by_nameonly compare the name.avcodec_profile_name_to_int(inffmpeg.h) only compares the name withstrcasecmp.av_dict_getonly reads the key; the returned entry points into the dictionary, not into the key.av_dict_setwithflags == 0copies both key and value withav_strdup.av_opt_set_intonly uses the name to look up the option.How it was tested
In a
golang:1.22-bullseyecontainer with Debian's FFmpeg 4.3 dev packages (libavformat-dev libavcodec-dev libavutil-dev libavresample-dev libswscale-dev), before and after this change:go build ./cgo/ffmpeg/andgo vet ./cgo/ffmpeg/: both pass.HasEncoder("aac"),HasDecoder("aac"),NewAudioEncoderByName("aac"),SetOption("profile", "LC"),SetOption("b", 128000),GetOption("b", &s)andClose(), measuringmallinfo().uordblksaround the loop:224 B/cycle matches 7 leaked strings per cycle at glibc's 32-byte minimum chunk. The
Resamplerpath is covered by the build only.