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. Strange memory leak

Strange memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studiodata-structuresperformance
11 Posts 6 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
    miniman06
    wrote on last edited by
    #1

    //process.h
    private:
    PROCESSENTRY32 * m_proc;
    char * m_filename;
    bool m_stringupdated;

    //process.cpp
    Process::Process()
    {
    m_proc = new PROCESSENTRY32;
    m_proc->dwSize = sizeof(PROCESSENTRY32);
    m_stringupdated = false;
    m_filename = NULL;
    }
    //process.cpp
    Process::~Process()
    {
    delete m_proc;
    if(m_filename)
    delete [] m_filename;
    }

    //main
    Process * allo = new Process();
    allo->~Process();

    VLD reports:

    Visual Leak Detector Version 2.2.3 installed.
    WARNING: Visual Leak Detector detected memory leaks!
    ---------- Block 1 at 0x005A2100: 12 bytes ----------
    Call Stack:
    c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
    0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
    Data:
    48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........

    Visual Leak Detector detected 1 memory leak (48 bytes).
    Largest number used: 640 bytes.
    Total allocations: 640 bytes.
    Visual Leak Detector is now exiting.
    The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).

    I'm freaking out guys I just don't see the leak ,Thanks in advance :) .

    S R S J B 5 Replies Last reply
    0
    • M miniman06

      //process.h
      private:
      PROCESSENTRY32 * m_proc;
      char * m_filename;
      bool m_stringupdated;

      //process.cpp
      Process::Process()
      {
      m_proc = new PROCESSENTRY32;
      m_proc->dwSize = sizeof(PROCESSENTRY32);
      m_stringupdated = false;
      m_filename = NULL;
      }
      //process.cpp
      Process::~Process()
      {
      delete m_proc;
      if(m_filename)
      delete [] m_filename;
      }

      //main
      Process * allo = new Process();
      allo->~Process();

      VLD reports:

      Visual Leak Detector Version 2.2.3 installed.
      WARNING: Visual Leak Detector detected memory leaks!
      ---------- Block 1 at 0x005A2100: 12 bytes ----------
      Call Stack:
      c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
      f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
      f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
      0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
      0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
      0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
      Data:
      48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........

      Visual Leak Detector detected 1 memory leak (48 bytes).
      Largest number used: 640 bytes.
      Total allocations: 640 bytes.
      Visual Leak Detector is now exiting.
      The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).

      I'm freaking out guys I just don't see the leak ,Thanks in advance :) .

      S Offline
      S Offline
      Sivaraman Dhamodharan
      wrote on last edited by
      #2

      How many bytes allocated and pointed by m_filename?

      Programming Article

      M 1 Reply Last reply
      0
      • M miniman06

        //process.h
        private:
        PROCESSENTRY32 * m_proc;
        char * m_filename;
        bool m_stringupdated;

        //process.cpp
        Process::Process()
        {
        m_proc = new PROCESSENTRY32;
        m_proc->dwSize = sizeof(PROCESSENTRY32);
        m_stringupdated = false;
        m_filename = NULL;
        }
        //process.cpp
        Process::~Process()
        {
        delete m_proc;
        if(m_filename)
        delete [] m_filename;
        }

        //main
        Process * allo = new Process();
        allo->~Process();

        VLD reports:

        Visual Leak Detector Version 2.2.3 installed.
        WARNING: Visual Leak Detector detected memory leaks!
        ---------- Block 1 at 0x005A2100: 12 bytes ----------
        Call Stack:
        c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
        f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
        f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
        0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
        0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
        0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
        Data:
        48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........

        Visual Leak Detector detected 1 memory leak (48 bytes).
        Largest number used: 640 bytes.
        Total allocations: 640 bytes.
        Visual Leak Detector is now exiting.
        The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).

        I'm freaking out guys I just don't see the leak ,Thanks in advance :) .

        R Offline
        R Offline
        Rolf Kristensen
        wrote on last edited by
        #3

        If the sizeof(PROCESSENTRY32) == 48, then I have found your leak.

        M 1 Reply Last reply
        0
        • S Sivaraman Dhamodharan

          How many bytes allocated and pointed by m_filename?

          Programming Article

          M Offline
          M Offline
          miniman06
          wrote on last edited by
          #4

          it's not used ,its deallocating it anyway...

          1 Reply Last reply
          0
          • R Rolf Kristensen

            If the sizeof(PROCESSENTRY32) == 48, then I have found your leak.

            M Offline
            M Offline
            miniman06
            wrote on last edited by
            #5

            not that either it's around 500~ bytes and I'm deallocating that too :/

            1 Reply Last reply
            0
            • M miniman06

              //process.h
              private:
              PROCESSENTRY32 * m_proc;
              char * m_filename;
              bool m_stringupdated;

              //process.cpp
              Process::Process()
              {
              m_proc = new PROCESSENTRY32;
              m_proc->dwSize = sizeof(PROCESSENTRY32);
              m_stringupdated = false;
              m_filename = NULL;
              }
              //process.cpp
              Process::~Process()
              {
              delete m_proc;
              if(m_filename)
              delete [] m_filename;
              }

              //main
              Process * allo = new Process();
              allo->~Process();

              VLD reports:

              Visual Leak Detector Version 2.2.3 installed.
              WARNING: Visual Leak Detector detected memory leaks!
              ---------- Block 1 at 0x005A2100: 12 bytes ----------
              Call Stack:
              c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
              f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
              f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
              0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
              0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
              0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
              Data:
              48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........

              Visual Leak Detector detected 1 memory leak (48 bytes).
              Largest number used: 640 bytes.
              Total allocations: 640 bytes.
              Visual Leak Detector is now exiting.
              The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).

              I'm freaking out guys I just don't see the leak ,Thanks in advance :) .

              S Offline
              S Offline
              Santhosh G_
              wrote on last edited by
              #6

              I cant see any memory leak in the given code.

              //main.. comment out the below code
              // Process * allo = new Process();
              //allo->~Process();

              Please check memory leak without allocating memory Process.

              M 1 Reply Last reply
              0
              • S Santhosh G_

                I cant see any memory leak in the given code.

                //main.. comment out the below code
                // Process * allo = new Process();
                //allo->~Process();

                Please check memory leak without allocating memory Process.

                M Offline
                M Offline
                miniman06
                wrote on last edited by
                #7

                Visual Leak Detector Version 2.2.3 installed.
                No memory leaks detected.
                Visual Leak Detector is now exiting.
                The program '[9248] ProcEnum.exe' has exited with code 0 (0x0).

                Not a false positive.

                1 Reply Last reply
                0
                • M miniman06

                  //process.h
                  private:
                  PROCESSENTRY32 * m_proc;
                  char * m_filename;
                  bool m_stringupdated;

                  //process.cpp
                  Process::Process()
                  {
                  m_proc = new PROCESSENTRY32;
                  m_proc->dwSize = sizeof(PROCESSENTRY32);
                  m_stringupdated = false;
                  m_filename = NULL;
                  }
                  //process.cpp
                  Process::~Process()
                  {
                  delete m_proc;
                  if(m_filename)
                  delete [] m_filename;
                  }

                  //main
                  Process * allo = new Process();
                  allo->~Process();

                  VLD reports:

                  Visual Leak Detector Version 2.2.3 installed.
                  WARNING: Visual Leak Detector detected memory leaks!
                  ---------- Block 1 at 0x005A2100: 12 bytes ----------
                  Call Stack:
                  c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
                  f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
                  f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
                  0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
                  0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
                  0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
                  Data:
                  48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........

                  Visual Leak Detector detected 1 memory leak (48 bytes).
                  Largest number used: 640 bytes.
                  Total allocations: 640 bytes.
                  Visual Leak Detector is now exiting.
                  The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).

                  I'm freaking out guys I just don't see the leak ,Thanks in advance :) .

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #8

                  You are explicitly calling the destructor. This will free the allocated memory for the members but not the object itself. You should use delete instead:

                  delete allo;

                  Or you may use auto delete (using an additional member var):

                  Process::~Process()
                  {
                  delete m_proc;
                  if(m_filename) // this is not necessary
                  delete [] m_filename;
                  if (m_bAutoDelete)
                  delete this;
                  }

                  M 1 Reply Last reply
                  0
                  • J Jochen Arndt

                    You are explicitly calling the destructor. This will free the allocated memory for the members but not the object itself. You should use delete instead:

                    delete allo;

                    Or you may use auto delete (using an additional member var):

                    Process::~Process()
                    {
                    delete m_proc;
                    if(m_filename) // this is not necessary
                    delete [] m_filename;
                    if (m_bAutoDelete)
                    delete this;
                    }

                    M Offline
                    M Offline
                    miniman06
                    wrote on last edited by
                    #9

                    that pretty much solved me the problem something to note delete still calls the destructor. so the auto delete code that you provided will cause an exception.

                    Process::~Process()
                    {
                    if(m_bDeallocated)
                    return;

                    delete m\_proc;
                    delete \[\] m\_filename;
                    m\_bDeallocated = true;
                    if (m\_bAutoDelete)
                        delete this; //will call this destructor again and cause trouble
                    

                    }

                    J 1 Reply Last reply
                    0
                    • M miniman06

                      that pretty much solved me the problem something to note delete still calls the destructor. so the auto delete code that you provided will cause an exception.

                      Process::~Process()
                      {
                      if(m_bDeallocated)
                      return;

                      delete m\_proc;
                      delete \[\] m\_filename;
                      m\_bDeallocated = true;
                      if (m\_bAutoDelete)
                          delete this; //will call this destructor again and cause trouble
                      

                      }

                      J Offline
                      J Offline
                      Jochen Arndt
                      wrote on last edited by
                      #10

                      Yes, you are right. That would result in a recursion. My code lacks resetting the m_bAutoDelete variable. It must be:

                      Process::~Process()
                      {
                      if (m_bAutoDelete)
                      {
                      m_bAutoDelete = false;
                      delete this;
                      }
                      else
                      {
                      delete m_proc;
                      delete [] m_filename;
                      m_proc = NULL;
                      m_filename = NULL;
                      }
                      }

                      However, this is not a good solution because the destructor is called twice and this will fail if the object wasn't allocated using new.

                      1 Reply Last reply
                      0
                      • M miniman06

                        //process.h
                        private:
                        PROCESSENTRY32 * m_proc;
                        char * m_filename;
                        bool m_stringupdated;

                        //process.cpp
                        Process::Process()
                        {
                        m_proc = new PROCESSENTRY32;
                        m_proc->dwSize = sizeof(PROCESSENTRY32);
                        m_stringupdated = false;
                        m_filename = NULL;
                        }
                        //process.cpp
                        Process::~Process()
                        {
                        delete m_proc;
                        if(m_filename)
                        delete [] m_filename;
                        }

                        //main
                        Process * allo = new Process();
                        allo->~Process();

                        VLD reports:

                        Visual Leak Detector Version 2.2.3 installed.
                        WARNING: Visual Leak Detector detected memory leaks!
                        ---------- Block 1 at 0x005A2100: 12 bytes ----------
                        Call Stack:
                        c:\users\jean\documents\visual studio 2012\projects\procenum\procenum\procenum.cpp (26): ProcEnum.exe!wmain + 0x7 bytes
                        f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ProcEnum.exe!__tmainCRTStartup + 0x19 bytes
                        f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ProcEnum.exe!wmainCRTStartup
                        0x760A33AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
                        0x76CF9EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
                        0x76CF9EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
                        Data:
                        48 21 5A 00 00 00 00 00 00 CD CD CD H!Z..... ........

                        Visual Leak Detector detected 1 memory leak (48 bytes).
                        Largest number used: 640 bytes.
                        Total allocations: 640 bytes.
                        Visual Leak Detector is now exiting.
                        The program '[10680] ProcEnum.exe' has exited with code 0 (0x0).

                        I'm freaking out guys I just don't see the leak ,Thanks in advance :) .

                        B Offline
                        B Offline
                        BCN 163
                        wrote on last edited by
                        #11

                        thank you and the reply, I learned something for this question. thanks again!

                        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