Jump to content
  • Advertisement
Zelerin

CScriptArray memory leak on inserted items from C++ side?

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 second leak concerns an issue with cscriptarray (version 2.33.0).  Basically, I'm finding when I do something like this (result is of type CScriptArray, and AEntity is a custom pass-by-reference that will register with the garbage collector upon construction):

result->InsertLast(new AEntity(engine_ptr, *iter));

When this line is ran, AEntity is inserted as expected, but when the script ends, Valgrind reports the AEntity instances are not cleaned up, but the CScriptArrays are.  AEntity instances outside CScriptArrays are cleaned up fine.  I'm thinking I'm doing something wrong, but am not sure what.  If I just missed something in the documentation, feel free to tell me and I'll go recheck.  Does anyone have any ideas what I'm doing wrong?  Thanks in advance for the help!

Share this post


Link to post
Share on other sites
Advertisement

InsertLast will add one reference to reference counted objects. Assuming AEntity starts off with a reference count of 1 you're leaking the reference by not doing Release() after adding it to the array.

Share this post


Link to post
Share on other sites

This fixed both my problems.  Thanks for your quick response!

A followup question:  In these situations, where I know I don't want to hold a reference to the object on the C++ side, is it safe to have the GC count start out as 0 when it registers with the engine?  Or does it need to be 1 at first to prevent any weird GC problems, and then be decremented after?  I'd like to automate this as much as possible behind constructors to keep the problem from returning.

Share this post


Link to post
Share on other sites

The reference count should always be valid, otherwise you could end up with edge cases where objects have a count of 0 while being treated as valid. That could cause crashes.

Share this post


Link to post
Share on other sites

When you put it that way, it makes complete sense.  Easy enough to do, and at least it sticks out in Valgrind if done wrong.  Thanks a lot for your help!  This is something I can document pretty easily as 'rules for working with AngelScript' in my code.  Oddly enough, I've found just about everything else in AngelScript easy to work with.

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!