I am having lots of problems deleting m_Array. The program segfaults at the end when it is doing the clean-up portion. I have two class A objects with different data in m_Array, and at some point in the program the data from one object starts to "wrap around" into the other array, causing incorrect data. T represents my templated data type.
There is also a class B, which just creates two class A objects
Declared publicly in class declaration A like:
template <typename T> class A
{
public:
pair<T, int> *m_Array; // Array of type pair
A(int size=1); // constructor
~A(); // A destructor
// ... all other definitions //
};
Defined in class A's constructor definition like:
template <typename T>
A<T>::A(int size) {
// Set array size
m_Array = new pair<T, int>[size];
// Initialize values
for (int i = 0; i < size; i++) {
m_Array[i] = make_pair(-1, -1);
}
//... other things defined and initialized...//
}
In class A's destructor:
template <typename T>
A<T>::~A() {
delete [] m_Array; // Not working as it should
}
Overloaded assignment operator
template <typename T>
const A<T>& A<T>::operator=(const A<T>& rhs) {
m_AArraySize = rhs.m_AArraySize;
m_currASize = rhs.m_currASize;
for (int i = 0; i < m_currASize; i++) {
m_Array[i].first = rhs.m_Array[i].first;
m_Array[i].second = rhs.m_Array[i].second;
}
_ptr = rhs._ptr;
return *this;
}
Copy constructor
template <typename T>
A<T>::A(const A<T>& other) {
m_AArraySize = other.m_AArraySize;
m_AHeapSize = other.m_AHeapSize;
for (int i = 0; i < m_currASize; i++) {
m_Array[i].first = other.m_Array[i].first;
m_Array[i].second = other.m_Array[i].second;
}
_ptr = other._ptr;
}
Class B declaration
template <typename T> class B{
public:
//B constructor
B(int size);
int m_currBSize; // spots used in array
int m_BSize; // size of array
A <T> oneAHolder;
A <T> twoAHolder;
};
Class B constructor
template <typename T>
b<T>::b(int size){
A<T>(size);
m_BArraySize = size;
m_currBSize = 1;
// Create two A objects
A<T> oneA(size);
A<T> twoA(size);
// oneA and twoA go out of scope
oneAHolder = oneA;
twoAHolder = twoA;
}
In my main function all that is being done is I am creating a class B object, and using it's insert function to insert the data into both of its A objects.
I have tried several different ways of deleting the data from the array, and stopping the data overflowing into the other array, but to no avail.
I appreciate any help!
P.S.: Please no "Just use std::vector"
EDIT: Added more of my code