Jump to content
  • Advertisement
Sign in to follow this  
Dentoid

RegisterScriptFactory problem

This topic is 4324 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

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

Share this post


Link to post
Share on other sites
Advertisement
Just noticed that I've written RegisterScriptFactory in some places. I meant of course RegisterStringFactory. :)

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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


Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!