YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Passing HSQOBJECT's
billbob
#1 Posted : Friday, June 22, 2018 10:36:20 PM(UTC)
Rank: Member

Groups: Registered
Joined: 1/19/2015(UTC)
Posts: 11

Thanks: 0 times
Was thanked: 0 time(s) in 0 post(s)
I'm having an issue with squirrel objects being deleted. My C code is as follows:

Code:

HSQOBJECT makeSQString(HSQUIRRELVM vm, const char *str) {
  HSQOBJECT s;
  sq_pushstring(vm, str->cstr(), -1);
  sq_getstackobj(vm, -1, &s);
  sq_pop(vm, 1);
  return s;
}


Since I'm not doing an sq_addref to s, when will the system delete it?

The HSQOBJECT passed back from this function is going to almost immediately be pushed onto the stack. I am getting some crashes due to what I think is the object being deleted during the sq_pop(). Is there a way to stop that besides doing a sq_addref()? I don't want to do that because I can't really track the object after it's returned without a lot of work. What are the best practices for holding on to HSQOBJECT's as they get passed around during function calls?
absence
#2 Posted : Wednesday, June 27, 2018 11:22:09 AM(UTC)
Rank: Advanced Member

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

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
That's exactly why there's sq_addref and sq_release.
Any reference counted object will be deleted exactly the very moment its reference count drops to zero.
Removing an object from the stack (like sq_pop) implies automatically decrementing the reference count, so it COULD drop to zero when you pop it from the stack (-> in case it's reference count is actually 1 it will be deleted by the pop)

You MUST reference it before removal and release the reference when you don't need the HSQOBJECT any more. As you are returning the HSQOBJECT to the caller, you need e.g. the callee drop the reference.
Always treat a HSQOBJECT like a pointer that must not go wild. BTW: in fact it IS a pointer (all macros starting with H are pointers). When you don't indicate to Squirrel when it is not allowed to deallocate the data, one has to assume any other API call or return of control to Squirrel may deallocate.

(PS: It is absolutely safe and appropriate to use addref and release on any Squirrel object, be it reference counted or not, you don't need to care about the type of object.)
Trying to save a few cycles by avoiding/omitting references is an approach usually not worth the hassle.
absence
#3 Posted : Wednesday, June 27, 2018 3:10:19 PM(UTC)
Rank: Advanced Member

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

Thanks: 1 times
Was thanked: 10 time(s) in 10 post(s)
Oh, I forgot to answer your last question. Best practice IMO is a wrapper, something along this (note: typed by heart, check especially parameters)

Code:

class SquirrelObject
{
public:
   SquirrelObject(HSQUIRRELVM v,HSQOBJECT &i) : obj(i) {
     sq_addref(v,&i);
   };
   SquirrelObject(HSQUIRRELVM v,SQInteger stackidx) {
     sq_getstackobj(v,stackidx);
     sq_addref(v,&obj);
   };
   ~SquirrelObject() { sq_release(v,&obj); }
  HSQOBJECT obj;
}


and then treat it as what it is - an object aka C++ class instance
I recommend adding all accesses and operations you need, too, and capsulate the members in private so you never ever end up fiddling around with HSQOBJECTs directly.
You could also additionally store the HSQUIRRELVM for convenience, which is safe for friend VMs/coroutines since a friend VM is basically just a stackframe for another "parent" VM)
Still, you need to take care of this object's lifetime - as usual regarding any class instance, as a C++ programmer you should be used to that.

Choose your destiny :-D
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.113 seconds.