# Memory leak when setting argument on context before execution?

## Recommended Posts

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->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!

##### Share on other sites

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.

##### Share on other sites

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.

##### Share on other sites

@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!

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 18
• 15
• 9
• 9