0

EDIT: Issue was resolved by commenters.

I have a template class called CSerializeable which takes in a default value of the datatype T and a string which specifies the name of the data (This is supposed to be used to loop through all CSerializeable objects and automatically store them under their attribute name in a json file).

The constructor of the CSerializeable class then allocates the default value of the datatype T on the heap using the "new" operator. This is stored on the heap to make sizeof(CSerializeable) constant so you can iterate over structs filled with CSerializeables (If the data is stored in the CSerializeable directly the size of the CSerializeable changes depending on the size of T).

The structures filled with CSerializeable's are then defined as extern global variables because i need to access them in different files because i use the data that is stored as settings for my program.

This is how my .cpp and .h files look like (Simplified version of my CSerializeable class which replicates this issue):

CSerializeable.h

template <class T>
class CSerializeable
{

public:

    CSerializeable(T DefaultValue, const char* szAttributeName)
    {
        m_pData = new T;
        *m_pData = DefaultValue;
        m_szAttributeName = szAttributeName;
    }

    ~CSerializeable()
    {
        delete m_pData;
    }

    T* GetData()
    {
        return m_pData;
    }

private:

    T* m_pData;
    const char* m_szAttributeName;
};

struct COption
{
    CSerializeable<int> m_Attribute = CSerializeable<int>(1, "m_Attribute");
};

extern CSerializeable<COption> g_Serializeable;

CSerializeable.cpp

#include "CSerializeable.h"

CSerializeable<COption> g_Serializeable(COption(), "g_Serializeable");

Main.cpp

#include "CSerializeable.h"

int main()
{
    g_Serializeable.GetData();
}

Unfortunately this program immediately reaches a halting point when i execute it. The crash seems to originate from ntdll.dll which leaves me guessing that i'm doing something which is not allowed. The crash also seems to happen before anything is executed.

Can somebody point out my mistake because i really can't seem to fix it on my own.

PimpX
  • 1
  • 1
  • 3
    Rule of 3 (or Rule of 5 with C++11)... looks like you're missing a copy constructor. – Eljay Aug 25 '20 at 13:36
  • 1
    [https://en.cppreference.com/w/cpp/language/rule_of_three](https://en.cppreference.com/w/cpp/language/rule_of_three) – drescherjm Aug 25 '20 at 13:40

0 Answers0