Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory leak!

Memory leak!

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresdebuggingperformancehelpquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mostafa_pasha
    wrote on last edited by
    #1

    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

    C _ A 3 Replies Last reply
    0
    • M mostafa_pasha

      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

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      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]

      M 1 Reply Last reply
      0
      • C CPallini

        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]

        M Offline
        M Offline
        mostafa_pasha
        wrote on last edited by
        #3

        But i got memory leak in VS2008 SP1!!! Is it a bug? or there is something wrong in my code? Best, MJM

        C C 2 Replies Last reply
        0
        • M mostafa_pasha

          But i got memory leak in VS2008 SP1!!! Is it a bug? or there is something wrong in my code? Best, MJM

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          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]

          M 1 Reply Last reply
          0
          • M mostafa_pasha

            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

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            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++)

            1 Reply Last reply
            0
            • M mostafa_pasha

              But i got memory leak in VS2008 SP1!!! Is it a bug? or there is something wrong in my code? Best, MJM

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M mostafa_pasha

                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

                A Offline
                A Offline
                Adam Roderick J
                wrote on last edited by
                #7

                Why don't you check with WinDbg? It is essential for a programmer like u.

                Величие не Бога может быть недооценена.

                1 Reply Last reply
                0
                • C CPallini

                  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]

                  M Offline
                  M Offline
                  mostafa_pasha
                  wrote on last edited by
                  #8

                  Yes, I test it inside a large application. But Does it depend?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups