-1

I'm testing out a class representing an dynamic array data structure I made for myself as practice with the language, but I ran into a problem where the destructor is called twice over, causing a heap corruption error.

So far, I have attempted to comment out some of the delete words. However, this leads to undefined behavior.

#include <iostream>
#include "windows.h"
#include <vector>

template<typename T> class Spider {
private:

    T** pointer;

    int maxSize;
    int lengthFilled;
public:
    //default constructor
    Spider()
    {
        pointer = new T * [1];
        maxSize = 1;
        lengthFilled = 0;
    }
    //destructor
    ~Spider()
    {
        for (int i = 0; i < lengthFilled; i++)
        {
            pop();
        }
        delete[] pointer;
    }
    //Pushes an object in
    void push(T thing)
    {
        if (lengthFilled == maxSize)
        {
            increaseSize();
        }

        T* thinggummy = &thing;

        //then save its pointer in the functional array
        pointer[lengthFilled] = thinggummy;
        lengthFilled++;
    }

    //pops the array
    void pop()
    {
        delete pointer[lengthFilled-1];
        setSize(lengthFilled - 1);
        lengthFilled--;
    }
}

int main()
{

    Spider<Spider<int>> test((long long)1);

    for (int i = 0; i < 2; i++)
    {
        test.push(Spider<int>());
        test.get(i).push(2);//this is implemented in the actual code, just omitted here
        std::cout << test.get(i).get(0);
        std::cout << "push complete\n";
    }

    system("pause");

    return 0;
}

The expected results for this program should be:

2
push complete
2
push complete

Press any key to continue...

Instead, I get an critical error code in the debug log of "Critical error detected c0000374".

XnossisX
  • 57
  • 6

1 Answers1

2

There are two issues here:

  1. Like WhiteSword already mentioned, you are taking the address of a local variable when you do T *thinggummy = &thing. That is going to cause trouble since that address will be invalid as soon as you leave scope (unless maybe T resolves to a reference type).
  2. You call delete on the things in the pointer array. However, these were not allocated via new. Instead they are just addresses of something. So you are trying to free something that was never allocted.
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
  • Thanks! Would I need to create a new T in the heap, and then deep copy the contents of thing over? – XnossisX May 28 '19 at 06:55
  • What to do depends on what type `T` actually is and/or for which types this `Spider` class should work. For example, if `T` is a primitive type then there is no need to allocate/deallocate anything. If `T` is a pointer then maybe it is ok to just store the pointer, maybe you want a deep copy. In general you probably want to rely on the copy constructor and assignment operators of `T` to do the right thing. You may want to check some template tutorials/courses. These sort of things are usually explained in depth there. – Daniel Junglas May 28 '19 at 07:05