🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Constructors/Destructors

Started by
12 comments, last by Desdemona 19 years, 9 months ago
Hi I've made a binding between AngelScript and std::string so that std::string is used as AngelScript strings. The problem I'm having now is that if I have a script like this: void blah () { if (something) return; String blah = "Bleh!"; } Then it seems like it runs the destructor of "blah" on the return, but never the constructor, which results in destroying an unconstructed object, which means crash in my case. If I put the "String blah..." before the return (which I guess forces it to construct) it seems to work. Any idea if I'm doing something wrong, or if it's a bug? /Anders Stenberg
Advertisement
This could actually be a bug. Would it be possible for you to pass me the code you used to register the string object. I'll use it to make some tests to see if it a bug in AngelScript or simply that you did something wrong when registering the object.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've got a couple of wrappers that look like this:
  std::string StringFactory (asUINT length, const char *s)  {    return std::string (s);  }  void ConstructString (std::string *thisPointer)  {    new(thisPointer) std::string ();  }  void DestructString (std::string *thisPointer)  {    thisPointer->~string ();  }  void AssignString (std::string &other, std::string *thisPointer)  {    new(thisPointer) std::string (other);  }


And I register them like this:

    engine->RegisterObjectType ("String", sizeof (std::string), asOBJ_IS_COMPLEX);    engine->RegisterStringFactory ("String", asFUNCTION(StringFactory), asCALL_CDECL | asCALL_RETURNBYREF);    engine->RegisterTypeBehaviour ("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);    engine->RegisterTypeBehaviour ("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);    engine->RegisterTypeBehaviour ("String", asBEHAVE_ASSIGNMENT, "String &f(const String&)", asFUNCTION(AssignString), asCALL_CDECL_OBJLAST);
Your AssignString doesn't return anything. It should return a reference to the string, i.e. the thisPointer.

I'm not sure if this is what is causing the original problem, I'll do some tests now.

...

Ok, I've verified that it is in fact a bug. Though it is not an implementation bug, but rather a design bug thus it will take me some time to correct it. For now, you can work around it by declaring the string at the beginning of the script (as you yourself noticed).



[Edited by - WitchLord on July 30, 2004 2:01:14 PM]

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Quote: Original post by WitchLord
Your AssignString doesn't return anything. It should return a reference to the string, i.e. the thisPointer.


Ooops. Interesting that it appears to work the way it is now too though. :)
It's working because the returned reference isn't used, so it doesn't matter that it is not a valid string reference. If you had written a script like the following it wouldn't work:

string a, b;a = b = "Bleh!";


I've figured out how to fix the bug, I hope that I'll be able to do so this weekend. Shouldn't be much more than an hour of work.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

The plot thickens...

When compiling a script like the one above the assert at as_bytecode.cpp:316 fails. "assert( n < destructors.GetLength() );"
The interesting thing is that now it only works if I put the "String blah;" _after_ the return, instead of the other way around as before.

I haven't tried to figure this out on my own. Any ideas?

Oh, and this is with 1.8.0c.

/Anders Stenberg
Please download 1.8.1 WIP 3 and use that instead of 1.8.0c. There are just too many bugs in 1.8.0c.

If the bug still continues to present itself with 1.8.1 WIP 3 let me know, and I'll fix it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Oh sorry, I was wrong about my version. It was either WIP 2 or 3 I used (I'm not home right now and I won't be for another week, so I cannot check what version I had.)

I usually check angelcode out pretty much every day, and update whenever a new version comes around. If WIP 3 is very recent I might have missed that one though.
I did a quick test, and was able to reproduce the error. I'll hopefully be able to fix this quickly.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement