Memory leak when setting argument on context before execution?

Started by
3 comments, last by Zelerin 5 years ago

Hello!  I'm experiencing a couple of memory leaks and would like to see if anyone knows the correct way to clean them up.

The first leak concerns setting my custom pass-by-reference string class as an argument on the script 'main()' method.  I do the usual context SetArgObject() to set the string on the function, ran the script, and did a GarbageCollect() when done.  Even when I clean up the context and engine like so:


context_ptr->Abort();
context_ptr->Unprepare();
engine_ptr->DiscardModule(SCRIPT_MODULE_NAME.c_str());
engine_ptr->GarbageCollect();

engine_ptr->ReturnContext(context_ptr);
engine_ptr->ShutDownAndRelease();

 

The string argument is never cleaned up and Valgrind finds it.  The other string instances are cleaned up fine.  Am I supposed to always manually delete the argument once execution has completed?  The string class automatically registers with the engine's garbage collector, so I figured that would do it, but it doesn't seem to work.  It's also quite possible I'm doing something wrong, so any ideas are appreciated!  Thanks in advance for your help! :)

Advertisement

This is probably the same problem as your other memory leak, you're probably not doing Release() after setting the argument. If you're not sure, post your string class constructors and registration for the type, and the code used to set the argument.

We'll need more information on your code to better help you here.

What is the script function signature?

How are you allocating the string?

Is your string class reference counted? Or is it a value type?

My hunch is that your script function is expecting the string by reference, in which case the caller is the owner of thr string, thus angelscript won't do any cleanup on it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

@Solokiller had solved it in the previous post and your hunch is correct.  I made a custom string class that does it by reference, and didn't realize that the caller is indeed the owner in this situation.   I now have the caller's class decrement the string reference once the script completes, and now it GCs just fine, every time.

If you're curious, I can post a github link to the classes, though after making the fixes all my GC problems have been solved.  I now have a much better understanding of how it works (or at least think I do!).  It makes a lot more sense  than it did when I first started.

Thanks for responding, though!

This topic is closed to new replies.

Advertisement