View Full Version : Memory leak in GetFrame?
Hi, I`m writing a video/avisynth script reader for matlab by using avisynth. Everything works fine, but it seems like there`s a leak of about 3mb of memory everytime i call the IClip GetFrame function. Here`s the code that leaks....:
PVideoFrame frame = (*clip)->GetFrame( framenr, env );
As mentioned, the code works, but memory leaks. When i comment out this line there`s no leak.
tritical
4th May 2009, 16:37
You didn't show enough of your code to know for sure. However, it sounds like you may just be seeing the cache grow. If you don't specify setmemorymax() in the script, then avisynth's internal cache (there is actually a separate cache placed automatically after each filter, but all of them work with the same memory pool) uses some percentage of the available memory (I can't remember what it is atm, maybe 1/2 or 1/4). However, avisynth doesn't allocate all of this memory at once on startup (creation of scriptenvironment). The amount in use grows up to that level as you request frames from the script and new buffers are created so that current ones do not need to be reused (i.e. so that the frames stay cached). This growth should stop once the cache limit is hit, or the cache filters have cached all the frames they need to, and avisynth begins reusing buffers instead of creating new ones.
Of course it is possible that a filter in your script leaks memory on every getframe call, but you didn't show the filter chain you were using. This is rather unlikely if you are using common filters though.
thanks alot, that solved the "problem"... The memory usage now stopped after reaching the new memory_max limit I set. regards, ivar
By the way, do you know if there`s a way to free up the memory used by the cache? Then I could set a large memory_max, and just clear the cache every time I use the script to make sure avisynth has enough memory, but still avoid a buildup of memory usage
tritical
5th May 2009, 22:46
The only time the memory used by the cache gets released is when the scriptenvironment to which it is associated is destroyed AFAIK. I don't follow your idea here though:
Then I could set a large memory_max, and just clear the cache every time I use the script to make sure avisynth has enough memory, but still avoid a buildup of memory usage
When you say "every time I use the script" what do you mean? Are you accessing avisynth.dll directly, and invoking 'import' to load a script?
I`m writing a mex file for matlab. In the mex file, I load avisynth.dll, create a ScriptEnvironment, and either invoking import to load a script, or invoking Eval with DirectShowSource("avifile.avi").
I then get the clip, then get the frame, and return the rgb data from the frame.
the function is used like this in matlab:
img = getframe( 'avsfile.avs', framenr )
img = getframe( 'avifile.avi', framenr )
or
for i=1:numFrames
img{i} = getframe( avifile, i );
end
I also get some strange crashes when i try to load videos of very small sizes, like 20*30 w/h. Different crashes with different codecs, maybe this is a codec issue. 90 * 40 worked ok.
Another videoreader (videoIO toolbox) which uses DirectShow and not avisynth, also gives same strange results for small videos, so not an avisynth issue... (The small videos were just for testing purposes, I`m going to work on larger ones)
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.