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

Strange effect in 1.10

Started by
1 comment, last by abrken 18 years, 5 months ago
Hello, While cleaning my code for ducmentation purpose I have realized that I was making a mistake in registring an object. the sizeof of the object wasn't the good one. The object referecing was the MFC CString. It's sizeof return 4 while I was declaring it with a sizeof = 16 . After having made the modification, the ADD operator stop working passing a null value as the second parameter ?! I have returned the sizeof to 16 and the ADD operator works again. The ADD operator is declared a CDECL. The difference between the two sizeof was that when sizeof = 4 the CallCDeclFunction is called rather than when it's sizeof = 16 the CallCDeclFunctionRetByRefObjFirst_impl is called. Does anybody can give me a reason ?

	in_pAsEngine->RegisterObjectType("CString", sizeof(CString)+16, asOBJ_CLASS); // If put sizeof(CString) == 4 // < 16 then it bugs on concatenate strings

	RegisterBStr(in_pAsEngine);

	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_CONSTRUCT, "void f()", asFUNCTIONP(CString_Constructor, (CString &)), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_CONSTRUCT, "void f(const bstr &)", asFUNCTIONP(CString_Constructor, (asBSTR &, CString &)), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_CONSTRUCT, "void f(const string &)", asFUNCTIONP(CString_Constructor, (string &, CString &)), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_CONSTRUCT, "void f(const bstr &, int)", asFUNCTIONP(CString_Constructor, (asBSTR &, int, CString &)), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_CONSTRUCT, "void f(const uint8 &)", asFUNCTIONP(CString_Constructor, (BYTE &, CString &)), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(CString_Destructor), asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_ASSIGNMENT, "CString &f(const bstr &)", asFUNCTION(asCStringCopy),      asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_ASSIGNMENT, "CString &f(const CString &)", asFUNCTION(asCStringCopyCString),      asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_ASSIGNMENT, "CString &f(const string &)", asFUNCTION(asCStringCopyString),      asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_ADD_ASSIGN, "CString &f(const bstr &)", asFUNCTION(asCStringAppend),    asCALL_CDECL_OBJLAST);
	in_pAsEngine->RegisterObjectBehaviour("CString", asBEHAVE_ADD_ASSIGN, "CString &f(const CString &)", asFUNCTION(asCStringAppendCString),    asCALL_CDECL_OBJLAST);

// THE crashing function ...
	in_pAsEngine->RegisterGlobalBehaviour(asBEHAVE_ADD,         "CString f(const CString &, const CString &)", asFUNCTION((CString(*)(const CString*,const CString*))asCStringConcatenate),        asCALL_CDECL);

...

CString asCStringConcatenate(const CString *left, const CString *right)
{
	return ((*left) + (*right));
}

Thanks, AbrKen.
Advertisement
You need to register the CString with the correct flags. I believe it should be:

in_pAsEngine->RegisterObjectType("CString", sizeof(CString), asOBJ_CLASS_CDA);


This will tell AngelScript that the CString has a constructor, destructor, and an assignment operator in C++, and will thus use the correct calling convention when calling functions that return a CString by value.

With the flag that you used AngelScript, believes that the CString is a plain data structure, and that C++ will return it in the EAX register just like a primitive value.

It works with size 16, because that is more than fits in the EAX:EDX registers, thus the calling convention is the same as for objects with constructors, destructors, or assignment operators.

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

Simply one word : Excellent !

Regards,

AbrKen.

This topic is closed to new replies.

Advertisement