Log in

View Full Version : How to free memory after using env...


jordanh
1st June 2013, 10:15
I have a win32dll that loads avisynth dll and invokes avisynth scripts this way:


enum { AVISYNTH_INTERFACE_VERSION = 2 };

avsdll = LoadLibrary("avisynth.dll");

CreateEnv = (IScriptEnvironment *(__stdcall *)(int))GetProcAddress(avsdll, "CreateScriptEnvironment");

IScriptEnvironment* outputenv= CreateEnv(AVISYNTH_INTERFACE_VERSION);




The problem is, in the lifetime of my dll, it should be possible to destroy and respawn a lot of environment instances.
When i set env = 0 after using one environment, i am under the impression that a lot of memory is not freed. So the memory consumption of my dll raises all the time.


returnedAVSClip = 0;
env = 0;
FreeLibrary(avsdll);


The question is, can one destroy all the memory that is left over after using an env? I know about the new deleteEnvironment stuff in Interface 5 or 6, but it seems to be a lot of work to port to this intreface, so i would prefer the old version.

Thanks!!

IanB
1st June 2013, 11:13
Theoretically you just do delete env;
env = 0;
However there have been reports that due to mismatching msvcrt's the memory block holding the ScriptEnvironment class tries to be released from the wrong heap. The ~IScriptEnvironment() destructor is run so all the additional structures get correctly released. If it happens you would loose the 70 to 80 bytes of the base class. Not sure how many other problems would be caused. I am not even sure if it is a real problem, I have never witnessed it happening. I guess running it up under a debugger and sicking a heap walker onto the process would soon tell if something was not quite right.

Of course calling env->DeleteScriptEnvironment(); if available is the no worries solution.

I suppose for the paranoid you could do something like this :-...
// Set all smart pointers to zero
returnedAVSClip = 0;
...

try {
// Check to see if we have a DeleteScriptEnvironment?
env->CheckVersion(5);
env->DeleteScriptEnvironment(); // next vtable entry after SubframePlanar
env = 0;
}
catch (AvisynthError err) {
// Damn! Don't have one, just run the destructor and leak the ScriptEnvironment
env->~IScriptEnvironment();
env = 0;
}

FreeLibrary(avsdll);
...
It's safe to add vtable declaration to a class you just have to be sure they are physically present before you call them.

jordanh
2nd June 2013, 23:38
i successfully ported to 2.6, and use env->DeleteScriptEnvironment();

of course this works perfect.
I never tried delete(env) because i read that one should not do it...



Thanks a lot!