Sign in to follow this  

Constructors/Destructors

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Great. (Although I personally don't require anything to be done quickly, since I probably won't be working on anything angelscript related in a week or so. :)

Share this post


Link to post
Share on other sites
No worries. I found the problem already.

I'll release a new version with the latest changes today, or first thing tomorrow.

I did however discover that GNUC returns objects of type std::string in memory, even though the size is only 4 bytes. This doesn't follow what I had found earlier, which means that I will have to do more research to find out when and why GNUC returns objects in memory. It will probably result in yet another flag for RegisterObjectType().

Share this post


Link to post
Share on other sites
void AssignString (std::string &other, std::string *thisPointer)
{
new(thisPointer) std::string (other);
}

Ignoring the comment about how this should return the reference to thisPointer, I am not familiar with this syntax of new. Whats going on here?

J

Share this post


Link to post
Share on other sites
Sign in to follow this