IonelBurtan
Programmer
I have a template class made in C++ like this
template <class T> class
TypedArray
{
private:
T* m_pArray;
int m_nSize;
int m_nMaxSize;
const int m_nGrowBy;
public:
TypedArray():m_nGrowBy(4), m_nMaxSize(0)
{
m_nSize = 0;
m_nMaxSize = 0;
m_pArray = 0x0;
}
~TypedArray()
{
RemoveAll();
}
int GetSize(){return m_nSize;};
void RemoveAll()
{
delete[] (unsigned char*)m_pArray; // (unsigned char*)= BYTE* from windows
m_pArray = 0x0;
m_nSize = 0;
m_nMaxSize = 0;
}
long GetAt(int i)
{
return m_pArray;
}
void Add(T value)
{
if(m_nSize == m_nMaxSize) //s-a ajuns la dimensiune maxima
{
T* pTemp = new T[m_nMaxSize];
memcpy(pTemp, m_pArray, m_nMaxSize * sizeof(T));
delete[] m_pArray;
m_pArray = new T[m_nMaxSize + m_nGrowBy];
memcpy(m_pArray, pTemp, m_nMaxSize * sizeof(T));
delete[] pTemp;
m_pArray[m_nSize] = value;
m_nSize++;
m_nMaxSize+=m_nGrowBy;
}
else
{
m_nSize++;
m_pArray[m_nSize-1] = value;
}
}
int Find(T value)
{
return GetPositionInVector(m_pArray, &value, m_nSize);
}
};
In Another class CMother i have a member of type
TypedArray<int>** m_pMember alocated dinamically of size
I have problems in ~CMother() like this
{
for(int i=0; i<m_nLocalitatiDelivCount; i++)
for(int j=0; j<m_nLocalitatiDelivCount; j++)
m_arrOraseMinRoad[j].RemoveAll();
DealocaMatrice(m_arrOraseMinRoad, m_nLocalitatiDelivCount); //<--here the code cracks
}
function DealocaMatrice looks like this:
template <typename T>
void DealocaMatrice(T**& aMatrice, unsigned int nCountX)
{
if (aMatrice!=0x0 /*NULL*/)
{
for(unsigned int i=0; i<nCountX; i++)
delete aMatrice;
delete[] aMatrice; //<-here the codestops
aMatrice = 0x0;// NULL fara windows.h
}
}
TypedArray has no memory leaks and RemoveAll just leaves the heap OK. Also, function DealocaMatrice works fine in many other instances, the code is ok.
The error I get is a an ASSERT:
_BLOCK_TYPE_IS_VALID() coming from dbgdel.cpp
It just looks like I have a problem when I dealloc a heap matrix made of heap arrays. Pretty unusual situasion but I do not see why it does not works.
Tnx,
Any clue really apreciated,
s-)
Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
template <class T> class
TypedArray
{
private:
T* m_pArray;
int m_nSize;
int m_nMaxSize;
const int m_nGrowBy;
public:
TypedArray():m_nGrowBy(4), m_nMaxSize(0)
{
m_nSize = 0;
m_nMaxSize = 0;
m_pArray = 0x0;
}
~TypedArray()
{
RemoveAll();
}
int GetSize(){return m_nSize;};
void RemoveAll()
{
delete[] (unsigned char*)m_pArray; // (unsigned char*)= BYTE* from windows
m_pArray = 0x0;
m_nSize = 0;
m_nMaxSize = 0;
}
long GetAt(int i)
{
return m_pArray;
}
void Add(T value)
{
if(m_nSize == m_nMaxSize) //s-a ajuns la dimensiune maxima
{
T* pTemp = new T[m_nMaxSize];
memcpy(pTemp, m_pArray, m_nMaxSize * sizeof(T));
delete[] m_pArray;
m_pArray = new T[m_nMaxSize + m_nGrowBy];
memcpy(m_pArray, pTemp, m_nMaxSize * sizeof(T));
delete[] pTemp;
m_pArray[m_nSize] = value;
m_nSize++;
m_nMaxSize+=m_nGrowBy;
}
else
{
m_nSize++;
m_pArray[m_nSize-1] = value;
}
}
int Find(T value)
{
return GetPositionInVector(m_pArray, &value, m_nSize);
}
};
In Another class CMother i have a member of type
TypedArray<int>** m_pMember alocated dinamically of size
I have problems in ~CMother() like this
{
for(int i=0; i<m_nLocalitatiDelivCount; i++)
for(int j=0; j<m_nLocalitatiDelivCount; j++)
m_arrOraseMinRoad[j].RemoveAll();
DealocaMatrice(m_arrOraseMinRoad, m_nLocalitatiDelivCount); //<--here the code cracks
}
function DealocaMatrice looks like this:
template <typename T>
void DealocaMatrice(T**& aMatrice, unsigned int nCountX)
{
if (aMatrice!=0x0 /*NULL*/)
{
for(unsigned int i=0; i<nCountX; i++)
delete aMatrice;
delete[] aMatrice; //<-here the codestops
aMatrice = 0x0;// NULL fara windows.h
}
}
TypedArray has no memory leaks and RemoveAll just leaves the heap OK. Also, function DealocaMatrice works fine in many other instances, the code is ok.
The error I get is a an ASSERT:
_BLOCK_TYPE_IS_VALID() coming from dbgdel.cpp
It just looks like I have a problem when I dealloc a heap matrix made of heap arrays. Pretty unusual situasion but I do not see why it does not works.
Tnx,
Any clue really apreciated,
s-)
Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...