Detecting leaks on demand

In a continuously running application, it's often useful to gather information on all memory leaks at particular points during execution.
To detect leaks on demand from the IDE:
  1. Find a location in the code where you want to check for memory leaks and insert a breakpoint.
  2. Launch the application in Debug mode with the Memory Analysis tool enabled.
    For instructions on launching in Debug mode, see Launching a program with the debugger attached. For instructions on enabling Memory Analysis, see Enabling leak detection.
  3. Change to the Memory Analysis perspective by clicking its icon () in the toolbar.
  4. Open the Debug view so it's available in the current perspective.
  5. When the application encounters the breakpoint you specified, double-click the new Memory Analysis session (in the Session View) to open the Memory Analysis editor.
  6. Click the Settings tab at the bottom of the editor to show the settings for the running program.
  7. Click the Collect Memory Leaks () button at the top.
    You should see leaks begin to appear in the Memory Problems view. No new data will be available in the editor, because the memory analysis control thread and application threads are stopped while the process is suspended by the debugger.
    Note: You should give the control thread a reasonable amount of time (i.e., a few seconds) to collect the leak data before resuming the application. If leaks still don't appear in the Memory Problems view, it's possible there are no leaks at that particular breakpoint and you can therefore repeat these steps with different breakpoints.
  8. Click Resume in the Debug view to resume the application.
  9. Switch to the Memory Errors view to review information about the collected memory leaks.
The Memory Analysis tool can detect apparent leaks, but not some other types of leaks, such as cyclic references, accidental pointer matches, and leftover heap references. If the heap keeps growing after you eliminate the apparent leaks, you should manually inspect your allocations. You can do this after the program terminates or you can suspend the program and examine the heap with the debugger.