Memory leak!
-
Here is a code for create and delete a 3D array! when i try to catch memory leak by the following code, It alerts that i have exactly 1600 bytes memory leak! Where is the problem of my Code?
#define VAR_TYPE double
VAR_TYPE ** Create2Darray(int n, int l)
{
VAR_TYPE **temp = new VAR_TYPE*[n];
for (int i =0 ; i< n ; i++) temp[i] = new VAR_TYPE[l];
return temp;
}
VAR_TYPE *** Create3DArray(int x,int y,int z)
{
VAR_TYPE *** arrayd;
arrayd = new VAR_TYPE**[x];
for (int i =0 ; i< x ; i++) arrayd[i] = Create2Darray(y,z);
return arrayd;
}void Delete3DArray(VAR_TYPE *** darray,int Xdim,int Ydim,int Zdim)
{
for (int k =0; k<Xdim;k++)
{
for (int j=0; j<Ydim;j++)
{
delete [] darray[k][j];
darray[k][j]=NULL;
}
delete [] darray[k];
darray[k] = NULL;
}
delete [] darray;
darray = NULL;
}//code for detect memory leak
#ifdef _DEBUG
CMemoryState msOld,msnew, diffMemState;
msOld.Checkpoint();
double *** Sample_Temp = Create3DArray(182,218,182);
Delete3DArray(Sample_Temp,182, 218, 182);msnew.Checkpoint(); if( diffMemState.Difference(msOld, msnew) ) { TRACE( "Memory leaked!\\n" ); diffMemState.DumpStatistics(); }
#endif
the output ofter running diffMemState.DumpStatistics() : Memory leaked! 0 bytes in 0 Free Blocks. 120 bytes in 2 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 57659956 bytes. Total allocations: 57661556 bytes. Best, MJM
-
Here is a code for create and delete a 3D array! when i try to catch memory leak by the following code, It alerts that i have exactly 1600 bytes memory leak! Where is the problem of my Code?
#define VAR_TYPE double
VAR_TYPE ** Create2Darray(int n, int l)
{
VAR_TYPE **temp = new VAR_TYPE*[n];
for (int i =0 ; i< n ; i++) temp[i] = new VAR_TYPE[l];
return temp;
}
VAR_TYPE *** Create3DArray(int x,int y,int z)
{
VAR_TYPE *** arrayd;
arrayd = new VAR_TYPE**[x];
for (int i =0 ; i< x ; i++) arrayd[i] = Create2Darray(y,z);
return arrayd;
}void Delete3DArray(VAR_TYPE *** darray,int Xdim,int Ydim,int Zdim)
{
for (int k =0; k<Xdim;k++)
{
for (int j=0; j<Ydim;j++)
{
delete [] darray[k][j];
darray[k][j]=NULL;
}
delete [] darray[k];
darray[k] = NULL;
}
delete [] darray;
darray = NULL;
}//code for detect memory leak
#ifdef _DEBUG
CMemoryState msOld,msnew, diffMemState;
msOld.Checkpoint();
double *** Sample_Temp = Create3DArray(182,218,182);
Delete3DArray(Sample_Temp,182, 218, 182);msnew.Checkpoint(); if( diffMemState.Difference(msOld, msnew) ) { TRACE( "Memory leaked!\\n" ); diffMemState.DumpStatistics(); }
#endif
the output ofter running diffMemState.DumpStatistics() : Memory leaked! 0 bytes in 0 Free Blocks. 120 bytes in 2 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 57659956 bytes. Total allocations: 57661556 bytes. Best, MJM
I got no memory leaks with the code you provided (I'm using VS2010). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I got no memory leaks with the code you provided (I'm using VS2010). :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]But i got memory leak in VS2008 SP1!!! Is it a bug? or there is something wrong in my code? Best, MJM
-
But i got memory leak in VS2008 SP1!!! Is it a bug? or there is something wrong in my code? Best, MJM
I didn't scrutinize your code. How did you test it (I used,of course, just the code provided. Do you test it inside a larger application?)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Here is a code for create and delete a 3D array! when i try to catch memory leak by the following code, It alerts that i have exactly 1600 bytes memory leak! Where is the problem of my Code?
#define VAR_TYPE double
VAR_TYPE ** Create2Darray(int n, int l)
{
VAR_TYPE **temp = new VAR_TYPE*[n];
for (int i =0 ; i< n ; i++) temp[i] = new VAR_TYPE[l];
return temp;
}
VAR_TYPE *** Create3DArray(int x,int y,int z)
{
VAR_TYPE *** arrayd;
arrayd = new VAR_TYPE**[x];
for (int i =0 ; i< x ; i++) arrayd[i] = Create2Darray(y,z);
return arrayd;
}void Delete3DArray(VAR_TYPE *** darray,int Xdim,int Ydim,int Zdim)
{
for (int k =0; k<Xdim;k++)
{
for (int j=0; j<Ydim;j++)
{
delete [] darray[k][j];
darray[k][j]=NULL;
}
delete [] darray[k];
darray[k] = NULL;
}
delete [] darray;
darray = NULL;
}//code for detect memory leak
#ifdef _DEBUG
CMemoryState msOld,msnew, diffMemState;
msOld.Checkpoint();
double *** Sample_Temp = Create3DArray(182,218,182);
Delete3DArray(Sample_Temp,182, 218, 182);msnew.Checkpoint(); if( diffMemState.Difference(msOld, msnew) ) { TRACE( "Memory leaked!\\n" ); diffMemState.DumpStatistics(); }
#endif
the output ofter running diffMemState.DumpStatistics() : Memory leaked! 0 bytes in 0 Free Blocks. 120 bytes in 2 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 57659956 bytes. Total allocations: 57661556 bytes. Best, MJM
Use Boost
MultiArray
instead of trying to allocate memory for this. http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/user.html[^]«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
But i got memory leak in VS2008 SP1!!! Is it a bug? or there is something wrong in my code? Best, MJM
I tested with VS2008 SP1 - no leak.
...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack
-
Here is a code for create and delete a 3D array! when i try to catch memory leak by the following code, It alerts that i have exactly 1600 bytes memory leak! Where is the problem of my Code?
#define VAR_TYPE double
VAR_TYPE ** Create2Darray(int n, int l)
{
VAR_TYPE **temp = new VAR_TYPE*[n];
for (int i =0 ; i< n ; i++) temp[i] = new VAR_TYPE[l];
return temp;
}
VAR_TYPE *** Create3DArray(int x,int y,int z)
{
VAR_TYPE *** arrayd;
arrayd = new VAR_TYPE**[x];
for (int i =0 ; i< x ; i++) arrayd[i] = Create2Darray(y,z);
return arrayd;
}void Delete3DArray(VAR_TYPE *** darray,int Xdim,int Ydim,int Zdim)
{
for (int k =0; k<Xdim;k++)
{
for (int j=0; j<Ydim;j++)
{
delete [] darray[k][j];
darray[k][j]=NULL;
}
delete [] darray[k];
darray[k] = NULL;
}
delete [] darray;
darray = NULL;
}//code for detect memory leak
#ifdef _DEBUG
CMemoryState msOld,msnew, diffMemState;
msOld.Checkpoint();
double *** Sample_Temp = Create3DArray(182,218,182);
Delete3DArray(Sample_Temp,182, 218, 182);msnew.Checkpoint(); if( diffMemState.Difference(msOld, msnew) ) { TRACE( "Memory leaked!\\n" ); diffMemState.DumpStatistics(); }
#endif
the output ofter running diffMemState.DumpStatistics() : Memory leaked! 0 bytes in 0 Free Blocks. 120 bytes in 2 Normal Blocks. 0 bytes in 0 CRT Blocks. 0 bytes in 0 Ignore Blocks. 0 bytes in 0 Client Blocks. Largest number used: 57659956 bytes. Total allocations: 57661556 bytes. Best, MJM
Why don't you check with WinDbg? It is essential for a programmer like u.
Величие не Бога может быть недооценена.
-
I didn't scrutinize your code. How did you test it (I used,of course, just the code provided. Do you test it inside a larger application?)? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Yes, I test it inside a large application. But Does it depend?