Welcome Guest Search | Active Topics | Log In | Register

Maintaining strong references.
#1 Posted : Sunday, August 7, 2016 10:30:50 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 9/18/2015(UTC)
Posts: 4

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)

I'm trying to maintain strong references to some userdata memory chunks, but it doesn't seem to work. Specifically I have a set of C++ functions callable from squirrel that registers the userdata objects and I need them to stay alive even when no references to them from squirrel remain. I get a handle to the userdata objects when they are created and store them, and then add/remove references on certain function calls. I let squirrel handle the memory allocation of the objects. However it seems if I add a reference to a userdata object from C++, remove it, and then remove the last reference from squirrel, it's not deallocated. Nor can the garbage collector reach it. Also I noticed that when I query the number of references with sq_getrefcount it only detects the references assigned from C++. What's going on here?

Edit: To clarify I'm logging the adding and removing of references from C++ and every sq_addref is matched with a sq_release. The documentation says that sq_getrefcount returns only references added from the C API but it returns 0. It has zero references from the C++ code yet when I remove the last reference from squirrel it isn't deallocated.
#2 Posted : Saturday, August 13, 2016 11:24:26 AM(UTC)
Rank: Advanced Member

Groups: Registered, Administrators
Joined: 6/11/2005(UTC)
Posts: 1,056

Thanks: 0 times
Was thanked: 78 time(s) in 60 post(s)
Can you post a snippet of your code where you create and addref the userdata in question? Is hard to guess what could be your problem, as I'm pretty sure that the ref system works.

Follow me on Twitter @squirrellang
#3 Posted : Saturday, August 13, 2016 2:50:36 PM(UTC)
Rank: Advanced Member

Groups: Registered
Joined: 8/23/2014(UTC)
Posts: 109
Location: Northern Germany & Lincolnshire, U.K.

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
may I add an important info: You MUST NOT hold any reference over an sq_close() call, or you'll experience crashes when trying to sq_release() them after VM shutdown.

References: The doc's aren't saying this about it (where did you read that?), I am 100% sure the values of sq_getrefcount() event depend on wether the VM is with or without garbage collector.
If I recall correctly by heart: With GC it returns c++-references +1 and without GC it returns the sum of all references, be it internal or external.

Given that, receiving a reference count of zero is only possible for non-refcounted types (integer, float, bool, null). So you may want to check if you are working on the proper object

(And...yes, posting code snippets would be a good advice)
Users browsing this topic
Guest (2)
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Clean Slate theme by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.4 | YAF © 2003-2010, Yet Another Forum.NET
This page was generated in 0.059 seconds.