Skip to content

Constructing gc<T> from a raw pointer #4

Description

@Flone-dnb

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions