Advertisement

opEquals now doen't work

Started by November 14, 2012 09:17 PM
12 comments, last by WitchLord 12 years ago
Hi.
I've got one question and one suggestion (not related).

The questions is: What happened with opEquals operator?
In earlier versions this code worked properly:
r |= ase->RegisterObjectMethod("Color", "bool opEquals(const Color &in) const", asMETHODPR(Color, operator==, (const Color &in) const, bool), asCALL_THISCALL);
r |= ase->RegisterObjectMethod("Color", "bool opEquals(const Color &in) const", asMETHODPR(Color, operator!=, (const Color &in) const, bool), asCALL_THISCALL);

But in 2.25.1 I get r = -1 after execution of the second line.
How can I get working operator!= ?

the suggestion:
I use angelscript within marmalade sdk and upon the whole all works fine...excluding one moment:
The marmalade memory manager doesn't like static non-POD (plain of data) objects. Often in that case It detects that there are some memory leaks. Are there really memory leaks or not - I can't judge, but those assertions are annoying.
In particular, within StringFactory function (scriptstdstring.cpp) there is such line:
static string dummy;
Marmalade memory manager considers that here is memory leak.
My workaround is take out this line from StringFactory body:
static string dummy;
static const string &StringFactory(asUINT length, const char *s)

I can't say why this workaround works but it works.
Now marmalade doesn't see any memory leaks.
You shouldn't register the operator!=() method. AngelScript handles both == and != with the same opEquals() method. In previous versions AngelScript wasn't detecting that you registered the same method multiple times but this doesn't mean that it worked the way you think. In fact it only worked for you because AngelScript ignored the registration of the operator!=() method, since it only expected a single method to be found.

About the warning in Marmalade. I think you should take this up with the Marmalade developers. It's not a memory leak to return a static local variable. In fact a static local variable is identical to a global variable, except that it is only visible from within the function, and only initialized upon first entry. The variable will be uninitialized upon exiting the application just like any other global variable.

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

Advertisement
Andreas, thank you for your answer.

operator!= is thrown now from the registration list )

About memory leak...actually there is no real  memory leak there.
But if some memory deallocations on the heap occur after the marmalade memory manager finalization - it considers that there is a memory leak.
Many of static objects (on the heap) will be deallocated after last line of code (where memory manager will be finalized).
The order of their allocation/deallocation may be a bit confusing.
And Marmalade developers cannot do anything with such static objects behaviour.
The only thing that they can do is to recommend don't use static non-POD objects.

So I'm happy that I managed to find the workaround that allows to shut up that annoying assertions.
I am sure that the windows with assertions about memory leaks irritate not only me (someone can  even get scared).
The problem is that Marmalade tries to detect the memory leaks before the program has even finished. The memory manager needs to replace the default memory manager in the runtime library itself so it is there even before the main function is invoked, and still there after the main function ends. It seems to me that Marmalade already uses their own runtime library so it doesn't seem like it should be that difficult to keep the memory manager until the very end.

Still, I don't mean to question them. I just don't like being forced not to use proper C++, i.e. static local variables, due to limitations in a third party library.

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

Yes, you are right.
With marmalade we need to initialize graph unit via IwGxInit invoking and finalize it before the completion of the application (via IwGxTerminate).
and the marmalade memory manager is initialized and finalized inside this functions.
We cannot affect it and static objects are freed after the memory manager has completed its work.
But my workaround for some reason works.
You are not forced don't use static string variable. My suggestion is just declare it a bit in another place.
Thank you anyway.
My guess is that it works because by moving the static variable out of the function it will be initialized before you call lwGxInit, and thus won't be accounted for when lwGxTerminate is called.

My problem with this is that it restricts the use of static local variables which is a common optimization technique. Declaring the variables globally works, but clutters the global namespace with variables that shouldn't be seen there.

Still, I'll probably change it. After all, my goal with AngelScript is to keep it as portable as possible.

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

Advertisement
what's about namespace and/or unique names?

namespace AngelScript
{
ststic string dummyStringForStringFactoryRoutineToAvoidProblemsWithMarmaladeMemoryManager; // :)
}

static const string &StringFactory(asUINT length, const char *s)
{
...
AngelScript::dummyStringForStringFactoryRoutineToAvoidProblemsWithMarmaladeMemoryManager = "";
...
}

I think you'll agree with me that it is not a very nice solution. :)

But don't worry this. I'd prefer using static local variables but as these don't work, static global variables are the next best thing.

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 think you'll agree with me that it is not a very nice solution. smile.png


Yes, of course. )

And advantages of AngelScript are so strong that we can close our eyes even if there will be real memory leaks.
Thank for your work.
Have fun with your creativity.
I've changed the static variable, and the comment style in the assembler code.

Would you mind testing revision 1474 with Marmalade to see if there is anything else that needs changes?

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