Smart pointer crash on exit

Manda Rajo 141 Reputation points
2023-01-22T10:00:27.62+00:00

The code below is a minimal version of the actual code of the project, it's just a skeleton, but I cannot reproduce the same problem, I cannot reproduce the same crash because this simplified code doesn't crash. What could be the possible reason of the crash on exit? When exit after main(), the crash message is:

Debug Assertion Failed! File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Expression: _CrtIsValidHeapPointer(block)

Crash on exit [Debug] enter image description here

class SceneNode_d; typedef shared_ptr<SceneNode_d> SceneNode;
class Scene_d; typedef shared_ptr<Scene_d> Scene;

enum SceneNodeType {
    SceneNodeType_None,
    SceneNodeType_Light,
    SceneNodeType_Mesh,
    SceneNodeType_Scene,
};

class SceneNode_d : public std::enable_shared_from_this<SceneNode_d> {
private:
    SceneNode _sharedThis = nullptr;
public:
    SceneNode_d(SceneNodeType type) {        
    }
    virtual ~SceneNode_d() {
    };
    void initSharedThis() {
        // This function cannot be called in the constructor because according to ChatGPT: When using std::enable_shared_from_this to manage the lifetime of an object, it is important to create a std::shared_ptr to the object before calling the constructor.
        _sharedThis = shared_from_this();
    }
};

class Scene_d : public SceneNode_d {
public:
    Scene_d() : SceneNode_d(SceneNodeType_Scene) {
    }
    ~Scene_d() {
    };
};

class Data_d {
private:
    std::vector<Scene> _scenes = {};
public:
    static shared_ptr<Data_d> getData() {
        // Idea from ChatGPT, check "2023-01-19 - Smart pointer crash on exit.docx".
        static shared_ptr<Data_d> instance = make_shared<Data_d>();
        return instance;
        // Old wrong method, a global variable: g_data: If it's a shared_ptr then it's impossible to write "Data g_data = nullptr;" in "data.cpp" otherwise it will become 0xcccccccccccccccc.
        //     https://stackoverflow.com/questions/34534927/using-smart-pointers-as-global-variables
        //     https://www.reddit.com/r/cpp_questions/comments/5p7ltq/about_smart_pointers_for_global_object/
    }
    Scene addScene();
};

Scene Data_d::addScene() {
    auto data = Data_d::getData();
    Scene scene = make_shared<Scene_d>();
    scene->initSharedThis();
    data->_scenes.push_back(scene);
    return scene;
}

class Window_d {
public:
    std::weak_ptr<Scene_d> _wptr_backgrondScene = {};
    void enableBackgroundScene(bool isPrimaryContext) {
        auto data = Data_d::getData();
        auto ptr = lock_weak_ptr(_wptr_backgrondScene);
        if (!ptr) {
            _wptr_backgrondScene = data->addScene();
        }
        // ...
    }
};
typedef shared_ptr<Window_d> Window;

int main() {
    Window wnd = make_shared<Window_d>();
    wnd->enableBackgroundScene(true);
    return 0;
} // <-- The crash in the real project happens here.

I asked ChatGPT what could be the reason then it answers:

ChatGPT:

The cause of the problem is that you are returning a raw pointer from the getData() function, which is a static instance of the Data_d class. This pointer is created only once, when the program starts, and it is not deleted when the program exits. The shared_ptr objects that are created in other parts of the code are not aware of the lifetime of this static instance, so they try to delete the objects they are managing even though the static instance is still in use. This leads to memory corruption, as the shared_ptr objects and the static instance are trying to delete the same memory at the same time.

One way to solve this issue is by using smart pointers such as unique_ptr or shared_ptr to manage the lifetime of the static instance, so that it will be deleted automatically when the program exits. Another way to solve this issue is to manually delete the static instance using the "operator delete" in the end of the program.

I apologize for any confusion my previous answers may have caused and for not being clear on the cause of the problem. I hope this explanation helps you to understand the cause of the problem and to find a solution that works for you.


Me: ChatGPT could never fix the crash on exit, maybe because of missing information, the real project is big enough. Can someone help me to fix that crash because I wasted days trying to fix but never fixed? And the information I give is experience from those many attemps.

Developer technologies | C++
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.