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 detection

Memory leak detection

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
3 Posts 3 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.
  • T Offline
    T Offline
    the pink jedi
    wrote on last edited by
    #1

    Can someone provide me with a simple set of code to show heap memory allocation and deallocation? Thank you very much

    K T 2 Replies Last reply
    0
    • T the pink jedi

      Can someone provide me with a simple set of code to show heap memory allocation and deallocation? Thank you very much

      K Offline
      K Offline
      kdehairy
      wrote on last edited by
      #2

      to allocate memory for an object of type OBJECT OBJECT* obj = new OBJECT; to allocate memory for an array of objects of type OBJECT OBJECT* obj = new OBJECT[array_size]; to deallocate memory for an object of type OBJECT delete obj; to deallocate memory for an array of objects of type OBJECT delete [] obj; k_dehairy

      1 Reply Last reply
      0
      • T the pink jedi

        Can someone provide me with a simple set of code to show heap memory allocation and deallocation? Thank you very much

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        the pink jedi wrote:

        Memory leak detection

        Here what MSDN say about this :- To detect a memory leak Create a CMemoryState object and call the Checkpoint member function. This creates the first memory snapshot. After your program performs its memory allocation and deallocation operations, create another CMemoryState object and call Checkpoint for that object. This gets a second snapshot of memory usage. Create a third CMemoryState object and call its Difference member function, supplying as arguments the two previous CMemoryState objects. If there is a difference between the two memory states, the Difference function returns a nonzero value. This indicates that some memory blocks have not been deallocated. This example shows what the code looks like: // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif // Do your memory allocations and deallocations. CString s = "This is a frame variable"; // The next object is a heap object. CPerson* p = new CPerson( "Smith", "Alan", "581-0215" ); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif Notice that the memory-checking statements are bracketed by #ifdef _DEBUG / #endif blocks so that they are compiled only in Win32 Debug versions of your program.

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        cheers, Alok Gupta VC Forum Q&A :- I/ IV

        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