You need to read about the rule of three.
What's happening is that when you do
nodes.append(LevenshteinDBNode(size));
you create a temporary LevenshteinDBNode
objectm which is then copied, leading to two objects with two pointers to the same memory. The temporary object is then destroyed which causes your destructor to be called, and which deletes the memory you've allocated. Now you have a copy of the object, with a pointer to the deleted memory.
You need to implement a copy constructor which does a so called deep copy of the memory allocated.
You also have a much more subtle error in your code, in that you don't initialize the pointer in the LevenshteinDBNode
default constructor. This means that if you have a default-constructed instance, the pointer will have an indeterminate value, in reality it will point to a random location. This will lead to undefined behavior if a default-constructed instance is then destructed when you try to delete
this random pointer. You need to initialize the pointer to nullptr
in the default constructor.