I'm trying to create a vector of tensors and I'm representing the tensors in 1D. I created a struct that contains a dynamically allocated array, a row and a column data (for out of bounds checking later).
struct TensorPtrManaged1D
{
double* m_tensorPtr;
unsigned m_row;
unsigned m_column;
TensorPtrManaged1D(unsigned rowNum, unsigned colNum) {
m_row = rowNum;
m_column = colNum;
m_tensorPtr = new double[rowNum * colNum];
for (unsigned i = 0; i < rowNum; ++i){
for (size_t j = 0; j < colNum; j++){
m_tensorPtr[i * colNum + j] = 1;
}
}
}
~TensorPtrManaged1D() { delete[] m_tensorPtr; }
};
If I create a single object it works fine, but if I put the same object in a vector, then whenever the destructor is called, it throws an exception: "*.exe has triggered a breakpoint. occurred"
TensorPtrManaged1D t7(3, 2); //This executes and cleans up the memory without leak
vector < TensorPtrManaged1D > t7v(1,t7); //This produces the exception, once it goes out of scope
Side note: while implementing it I found out that I lose all the speed advantage of direct array access once I put it in a vector, so I could achieve similar speed with a vector of vectors. However I don't understand why the struct doesn't get deleted without a problem when the destructor works otherwise.