Menu

#104 Setting an object equal to itself yields unexpected results

None
closed
nobody
None
5
2020-11-07
2020-11-06
No

This is a simple example:

Sqrat::Object sqObj = Sqrat::RootTable( Sqrat::DefaultVM::Get() );
sqObj = sqObj;

Now sqObj is null, against expectations.

The operator= should probably do nothing when this type of assignment happens, or the intrinsic should be rethought.

Discussion

  • shatteredmoon

    shatteredmoon - 2020-11-06

    Sorry, meant to say "or the intrinsic sq_resetobject should be rethought."

     
  • Wizzard

    Wizzard - 2020-11-06

    The sq_resetobject call probably needs to stay immediately after the sq_release call (problems were introduced by not following this convention in other places before), and so the issue should be fixed by checking if the object is the same. I also took the liberty of removing the release = so.release part because it felt correct to, but that might be wrong.

        Object& operator=(const Object& so) {
            if(obj != so.obj) {
                if(release) {
                    Release();
                }
                vm = so.vm;
                obj = so.obj;
                sq_addref(vm, &GetObject());
            }
            return *this;
        }
    
     
  • Wizzard

    Wizzard - 2020-11-06

    If you could check if that works and do a pull request it'd be appreciated!

     
    • shatteredmoon

      shatteredmoon - 2020-11-06

      Sorry, it's a bit more complicated than that:

      error C2678: binary '!=': no operator found which takes a left-hand operand of type 'HSQOBJECT' (or there is no acceptable conversion)

       

      Last edit: shatteredmoon 2020-11-06
  • Wizzard

    Wizzard - 2020-11-06

    I mistook HSQOBJECT to by a typedef to a pointer. As long as every Sqrat::Object has its own unique HSQOBJECT, the following will work as far as I can tell.

        Object& operator=(const Object& so) {
            if(this != &so) {
                if(release) {
                    Release();
                }
                vm = so.vm;
                obj = so.obj;
                sq_addref(vm, &GetObject());
            }
            return *this;
        }
    
     
    • shatteredmoon

      shatteredmoon - 2020-11-06

      Yes, this does the trick, thanks!

       
  • shatteredmoon

    shatteredmoon - 2020-11-06

    Nevermind, just saw your reply

     

    Last edit: shatteredmoon 2020-11-06
  • Wizzard

    Wizzard - 2020-11-07
    • status: open --> closed
    • Group: -->
     

Log in to post a comment.