Jump to content
  • Advertisement
Zelerin

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!