Best way to detect memory leaks 
Author Message
 Best way to detect memory leaks

Could someone tell me the best way on how to detect if your program has
memory leaks?

Secondly, I don't think I understand how to use try & catch blocks (or
finally & except) as well as I should.  Does any know of some good urls
with some tutorials on this subject?

Thanks



Sat, 15 Jun 2002 03:00:00 GMT  
 Best way to detect memory leaks
http://www.numega.com/devcenter/bc.shtml


Quote:
> Could someone tell me the best way on how to detect if your program has
> memory leaks?

> Secondly, I don't think I understand how to use try & catch blocks (or
> finally & except) as well as I should.  Does any know of some good urls
> with some tutorials on this subject?

> Thanks



Sun, 16 Jun 2002 03:00:00 GMT  
 Best way to detect memory leaks

Quote:

> Could someone tell me the best way on how to detect if your
program has
> memory leaks?

Purify or BoundsChecker wedge every OS function.

MFC and the CRT in VC++ have a middle-weight system that reports all
leaked blocks when a program ends. Open VC++ on an MFC project and
add this line:

    new char [1999];

When the program ends the Output window will stream with info
detecting the unfree heap object.

The lower-level the heap checker the better, because high-level ones
must be wrought in the same system as the bugs, so they can have the
same blind spots. However, I use the following to provide a run-time
assertion if a function did not leave the heap _approximately_ the
way it found it. But this has a blind spot  - a function could free
a block and then leak a block of the same size! More aggressive
checks are possible...

    unsigned long
GetHeapUsage ()
  //  return the total of the allocated blocks on the heap
{

    unsigned long total (0);
   _HEAPINFO hinfo;
    int heapstatus;
    hinfo._pentry = NULL;

    while( ( heapstatus = _heapwalk( &hinfo ) ) == _HEAPOK )
        {
        /* printf( "%6s block at %Fp of size %4.4X\n",
            ( hinfo._useflag == _USEDENTRY ? "USED" : "FREE" ),
              hinfo._pentry, hinfo._size );*/

        if (hinfo._useflag == _USEDENTRY)
            total += hinfo._size;
        }

   switch( heapstatus )
   {
   case _HEAPEMPTY:
      cerr << ( "OK - empty heap\n" );
      break;
   case _HEAPEND:
      cerr << ( "OK - end of heap\n" );
      break;
   case _HEAPBADPTR:
      cerr << ( "ERROR - bad pointer to heap\n" );
      assert (false);
      break;
   case _HEAPBADBEGIN:
      cerr << ( "ERROR - bad start of heap\n" );
      assert (false);
      break;
   case _HEAPBADNODE:
      cerr << ( "ERROR - bad node in heap\n" );
      assert (false);
      break;
   }
    return total;

Quote:
}

Now use it:

    int
main (int argc, char* argv[])
{

    struct finally {
        long heap;
        ~finally ()
            {
            assert (heap == GetHeapUsage ());  //  no leaks?
            }                                  //  even if we
THROW??
        } f;

    f.heap = GetHeapUsage ();

    Maine (argc, argv);

Quote:
}

'Maine()' is the function getting checked. When that 'f' thing
destructs (even if it destructs during stack unwinding due to a
'throw') the destructor of 'f' checks the heap's use count's the
same as it was going in.

Quote:
> Secondly, I don't think I understand how to use try & catch blocks
(or
> finally & except) as well as I should.  Does any know of some good
urls
> with some tutorials on this subject?

Don't use 'finally' and 'except'. They are for system-level things.
Don't try to catch exceptions thrown by the hardware (like a GPF) -
just prevent them in the first place.

For 'try' and 'catch', inspect the Guru of the Week series on

them, the science of using them right is still in its infancy.

--
 Phlip
======= http://users.deltanet.com/~tegan/home.html =======
  --  Happiness is a warm Pentium  --



Sun, 16 Jun 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Any good ways to trace memory leaks?

2. Summary: How to detect memory leaks better?

3. Summary: How to detect memory leaks better?

4. Best way to detect mem leaks...

5. detecting memory leaks in vc7

6. Detecting memory leaks

7. HELP:Detecting memory leaks

8. How to detect memory leak in Linux?

9. Detecting memory leaks

10. How to detect memory leaks in an ATL COM server with MFC

11. How to detect Memory Leaks ?

12. Detecting Memory Leaks efficiently

 

 
Powered by phpBB® Forum Software