tgc icon indicating copy to clipboard operation
tgc copied to clipboard

Constructing `gc<T>` from a raw pointer

Open Flone-dnb opened this issue 1 year ago • 0 comments

Hi, I've noticed that constructing gc<T> from a raw pointer does not make it freed, here is a simple example:

#if defined(WIN32)
#include <Windows.h>
#include <crtdbg.h>
#endif

#include <iostream>
#define TGC_MULTI_THREADED
#include "tgc.h"
using namespace tgc;

class Foo {
public:
    Foo() { std::cout<<"foo created\n"; }
    virtual ~Foo() { std::cout<<"foo destroyed\n"; }
    int test = 42;
};

int main() {
    // Enable run-time memory check for debug builds (on Windows).
#if defined(WIN32) && defined(DEBUG)
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#elif defined(WIN32) && !defined(DEBUG)
    OutputDebugStringA("Using release build configuration, memory checks are disabled.");
#endif

    Foo* pRaw = new Foo();
    const auto pGc = gc<Foo>(pRaw);
    std::cout<< pGc->test <<std::endl; // crash with `tgc2` since `meta` is `nullptr`.

    return 0; // leaks Foo
}

I've tried tgc2 and it also leaks because meta member of PtrBase is nullptr upon gc<T> construction.

Due to lack of the documentation I have a question: is it even supported?

Flone-dnb avatar Oct 01 '22 11:10 Flone-dnb