🎉 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!

RegisterScriptFactory problem

Started by
6 comments, last by WitchLord 17 years, 4 months ago
I think either I don't quite understand the workings of RegisterScriptFactory, or I'm just using it wrong (in which case I also don't understand it :)) Anyway... I do something like this: engine->RegisterStringFactory ("String", asFUNCTION(StringFactory), asCALL_CDECL); Where my StringFactory just returns a regular string, no reference or pointer. This seems to work in most cases, but sometimes it crashes in my string destructor (the one I've got registered for asBEHAVE_DESTRUCT). So... I started printing pointers from CONSTRUCT and DESTRUCT, and noticed that they don't match up very well. I get more DESTRUCT than CONSTRUCT, which I guess isn't really what it should be. The example scriptstring.h/cpp uses refcounting so that the factory can return a handle, but I'm not using refcounting (I just wrap a plain std::string) Any ideas? Am I stupid? :)
Advertisement
Just noticed that I've written RegisterScriptFactory in some places. I meant of course RegisterStringFactory. :)
You know you can edit the posts, don't you? :)

Anyway, take a look at stdstring.cpp in tests/test_feature/source. It is probably doing almost the exact same thing as you're doing. As far as I know that code is fully working.

When comparing the number of strings constructed against the ones destructed you must use the following formula:

CONSTRUCT + FACTORY = DESTRUCT

AngelScript doesn't call the construct behaviour when it calls the factory, because the factory function already returns the constructed string.

You may want to take a look at the memory management as well. If you're using AngelScript from a dll you must register the memory management functions, otherwise AngelScript may allocate the memory from one heap while you're deleting it from another.

Also, did you register the assignment operator? If not, AngelScript will do a byte by byte copy of the string objects.

Regards,
Andreas

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
You know you can edit the posts, don't you? :)


Yeah, just for some reason didn't find the button. Don't know how I missed it though :D

Quote: Original post by WitchLord
Anyway, take a look at stdstring.cpp in tests/test_feature/source. It is probably doing almost the exact same thing as you're doing. As far as I know that code is fully working.


Quote: Original post by WitchLord
When comparing the number of strings constructed against the ones destructed you must use the following formula:

CONSTRUCT + FACTORY = DESTRUCT

AngelScript doesn't call the construct behaviour when it calls the factory, because the factory function already returns the constructed string.



Yeah I realized that when thinking some more about how it all works. :)

Quote: Original post by WitchLord
You may want to take a look at the memory management as well. If you're using AngelScript from a dll you must register the memory management functions, otherwise AngelScript may allocate the memory from one heap while you're deleting it from another.


Nope, all in same heap (unless I'm doing something very strange, I guess I should check to be sure).

Quote: Original post by WitchLord
Also, did you register the assignment operator? If not, AngelScript will do a byte by byte copy of the string objects.


I have now copied the whole thing from that stdstring.cpp (thanks for the tip), and I still have the same problem. In one case I get a string destructor run on something that apparantly isn't a valid string anymore. I'll have to investigate more closely when this is happening (the callstack of the script etc) though. So... Either the script engine is doing something weird when cleaning up some string, or I'm corrupting the data somehow (which very well is possible).

I'll try to dig more whenever I get time.

First note is that it happens when an exception is thrown in the script (null handle access in this case), so I guess it's some cleanup that goes wrong...

EDIT: It's in the section of CleanStackFrame that has this comment, to be somewhat more exact
// Clean object parameters sent by reference


Look, I can use edit even more :)
The script function it's cleaning looks like this:
bool onCustomEvent (String event)
Hmm, it sounds like it may be a bug in AngelScript after all.

Is it possible for you to write a tiny test app that reproduces the problem? Perhaps modify tests/test_feature/source/test_exception.cpp? That would be of great help to me because I'm currently very short on time. If you don't have the time either I'll try to reproduce the problem myself, but it will take more time.

Regards,
Andreas

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
Hmm, it sounds like it may be a bug in AngelScript after all.

Is it possible for you to write a tiny test app that reproduces the problem? Perhaps modify tests/test_feature/source/test_exception.cpp? That would be of great help to me because I'm currently very short on time. If you don't have the time either I'll try to reproduce the problem myself, but it will take more time.


Yeah, that was my plan. Just didn't have time at the moment. Probably will do this tomorrow or tonight (GMT+1) or something.


Just to let everyone else know. This bug has been fixed already, and is available in the SVN.

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