I have am having a few issues getting strings to work as well as I would like them to.
I am defining the string functions as follows (does it matter if they are not static functions?)
CScriptString StringFactory(asUINT _length, const char* _s)
{
CScriptString string;
// Set the string
string.SetString(_s, _length);
// Return the object
return string;
};
void ConstructString(CScriptString* _thisPointer)
{
_thisPointer = new CScriptString;
}
void DestructString(CScriptString* _thisPointer)
{
_thisPointer->~CScriptString();
}
CScriptString& AssignString(CScriptString& _other, CScriptString& _thisPointer)
{
return _thisPointer = _other;
}
CScriptManager::asEngine->RegisterObjectType ("string", sizeof(CScriptString), asOBJ_CLASS_CDA);
CScriptManager::asEngine->RegisterStringFactory("string", asFUNCTION(StringFactory), asCALL_CDECL);
CScriptManager::asEngine->RegisterObjectBehaviour ("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
CScriptManager::asEngine->RegisterObjectBehaviour ("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
CScriptManager::asEngine->RegisterObjectBehaviour ("string", asBEHAVE_ASSIGNMENT, "string &f(const string&)", asFUNCTION(AssignString), asCALL_CDECL_OBJLAST);
I am having two problems with this as the moment
The first is that I am getting a memory leak from the variable created in ConstructString. I read an other post which suggested that the construction functions should be:
void ConstructString(CString &self)
{
new(&self) CString;
}
but if i apply this the compiler conplains about the code inside the (). I am overridding new for my memory manager, would this be a problem?
The second problem is if I allocate the char array within my CScriptString class as and when it is needed instead of just giving it a default size from the start.
If i new the char array, and then store the string in there, then the string i get out of the script is trashed, whareas if I make it a fixed length, there is no problem. I have made sure that my allocation code copies the string, so its not as if it is only passing a pointer then trashing that...
Hope some one can give me a few pointers on how to fix this
Oh, and the script i am running is simply:
string = "Hello World";
Thanks in advance
Spree