Hi,
Today I'm facing a weird issue with AngelScript.
I have a buffer class as following :
class asBuffer
{
// Buffer Implementation
public:
// Constructors/Destroyer
asBuffer() { bufferUID = GenerateUniqueID(); }
~asBuffer() { /*if (bufferPTR) free(bufferPTR);*/ } // Crash
...
static void ConstructorVoid(void* memory)
{
new(memory) asBuffer();
}
static void Destructor(void* memory)
{
((asBuffer*)memory)->~asBuffer();
}
}
engine->RegisterObjectType("asBuffer", sizeof(asBuffer), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS);
engine->RegisterObjectBehaviour("asBuffer", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(asBuffer::ConstructorVoid), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("asBuffer", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(asBuffer::Destructor), asCALL_CDECL_OBJLAST);
And here is the function which use asBuffer
bool CreateFileFromBuffer(asBuffer& fileBuffer, std::string& filePath)
{
if (filePath.empty()) return false;
if (fileBuffer.bufferSize == 0) return false;
std::ofstream file(filePath, std::ios::binary);
if (file.is_open())
{
file.write(reinterpret_cast<char*>(fileBuffer.bufferPTR), fileBuffer.bufferSize);
file.close();
}
else
{
return false;
}
return true;
}
engine->RegisterGlobalFunction("bool CreateFileFromBuffer(asBuffer& in, string& in)", asFUNCTION(CreateFileFromBuffer), asCALL_CDECL);
Now when I do this in my angelscript :
asBuffer buffer;
...
CreateFileFromBuffer(buffer, "../Temp/test.bin");
After CreateFileFromBuffer
returns AngelScript calls asBuffer::Destructor
, It's a reference In C++ this behavior is not happening on references, Why it calls the Destructor
? I also tried with bool CreateFileFromBuffer(asBuffer* fileBuffer, std::string& filePath)
and passed asBuffer as pointer but same thing happens, Any solution to this?