Advertisement

Prevent Destructor On Reference/Pointer Values

Started by October 30, 2023 06:33 AM
1 comment, last by WitchLord 1 year ago

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?

  • The universe is basically an animal. It grazes on the ordinary.

You've registered the asBuffer with asOBJ_POD. This tells AngelScript that is OK to do bitwise copies of the object when in need for a temporary object to guarantee the lifetime of the instance that will be referenced. The destructor is called on the temporary object once it is no longer needed.

Any type that deals with resource allocations and deallocations should not be registered with asOBJ_POD. Instead you'll need to registered explicit opAssign method and/or copy constructor behavior to make sure the copy is done in the correct way (whether that requires copying the internal buffer, or just keeping track of references to a shared pointer is up to you).

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