Jump to content

  • Log In with Google      Sign In   
  • Create Account

- - - - -

Constructors/Destructors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 Dentoid   Members   -  Reputation: 122

Like
0Likes
Like

Posted 30 July 2004 - 02:21 AM

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

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 30 July 2004 - 06:32 AM

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

#3 Dentoid   Members   -  Reputation: 122

Like
0Likes
Like

Posted 30 July 2004 - 07:55 AM

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);



#4 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 30 July 2004 - 08:01 AM

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

#5 Dentoid   Members   -  Reputation: 122

Like
0Likes
Like

Posted 30 July 2004 - 08:41 AM

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. :)

#6 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 30 July 2004 - 09:06 AM

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

#7 Dentoid   Members   -  Reputation: 122

Like
0Likes
Like

Posted 04 August 2004 - 08:23 AM

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

#8 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 05 August 2004 - 01:28 AM

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

#9 Dentoid   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 August 2004 - 02:23 AM

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.

#10 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 06 August 2004 - 04:40 AM

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

#11 Dentoid   Members   -  Reputation: 122

Like
0Likes
Like

Posted 06 August 2004 - 05:49 AM

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. :)

#12 Andreas Jonsson   Moderators   -  Reputation: 3445

Like
0Likes
Like

Posted 06 August 2004 - 06:48 AM

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().
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#13 Desdemona   Members   -  Reputation: 158

Like
0Likes
Like

Posted 29 August 2004 - 04:41 PM

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

#14 Desdemona   Members   -  Reputation: 158

Like
0Likes
Like

Posted 29 August 2004 - 04:45 PM

Err, Just noticed the 'Constructors...' thread answers this question.

J




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS