Leak Checking with the Sun C Compiler


Whilst developing POSIX *env API: unleaking portable version, it was necessary to lean on a useful component of the Sun C Compiler. And how – I made plenty of mistakes!

When coding tricksy conditional-deallocation based on tracked pointers where the tracking-array is also in allocated heap memory, and needs to be frequently resized, the possibility of an accidental memory-leak or duplicate-free() is high.

The Sun C Compiler (since version 3.01, approx 1993) comes with a debugger (dbx) capable of automatically detecting these kinds of memory-access/usage mistakes – at runtime.

In particular, even the 1993-vintage dbx detects:

  1. Deplicate Free – passing the address of an already-freed heap block to free()
  2. Bad Free – passing the address of a non-heap object to free()
  3. Read From Unallocated – trying to access through a pointer that does not point to a valid part of the address-space (NULL pointer, wild pointer).
  4. Read From Unitialised – accessing an object that has not been initialised.
  5. Write to Unallocated Memory – trying to update through a pointer that does not point to a valid part of the address-space (NULL pointer, wild pointer).
  6. Heap Memory Leak – there are no (surviving) references to any part of an allocated block.
  7. Lonely Heap Address in Middle of Block(extremely probable memory leak) – there is no reference to the start of an allocated block, but there is at least one reference to an address within the block.
  8. Lonely Heap Address in Register (probable memory leak) – an allocated block has not been freed, and no reference to the block exists anywhere in program memory, but a reference exists in a register.
  9. The newer versions of dbx can detect other less-likely errors (mainly misaligned pointers).

    These checks can be enabled with the “check -all ; check -access” command in either the GUI-debugger command window or to the command-prompt of the character-mode debugger.

    No special alternate libraries or source-code modifications are necessary – just compile with the “debug” flag (-g) and the resulting binary executable can be used both for real runtime production use and for memory-access checking, if desired.

    Advertisements

3 thoughts on “Leak Checking with the Sun C Compiler

  1. Wow that’s a pretty nifty tool! I’ve not programmed much C/C++, so I don’t know if GDB or other free tools have nice features like that.

    Sun sure made some nice tools for programming, I’ve done alot of Java programming and the amount of debugging possible there is pretty impressive also, although it is an easier language to program for.

    It’s interesting to see someone porting older software to something newer and in a portable way. I liked reading about the changes to the api.

    Cheers
    Sparcie

    1. Yes, the Sun “dbx” and it’s GUI front-end, “debugger”, even the mid-90’s vintage, are darned good, and not just the memory-checking parts – you can watchpoint C expressions, breakpoint on register-loads-of-a-specific-value and all sorts of other malarkey. Of course, until circa 2006 the Sun C/C++ Compilers and tools were paid-for products, thus the popularity of somewhat cheaper tools such as valgrind and Purify. Of course, that’s all moot now that the Sun “dbx” tool is a no-cost option. It has to be said that from my perspective in the 90s that the memory-checking was the best and most-used part of dbx – I don’t know whether that makes me an especially flaky programmer or not!

  2. Cool! I don’t think any of the 90’s tools for PC’s I used had memory checking facilities like that. I used Turbo Pascal (still do sometimes) it had some features that were good but no memory leak detection! The Turbo debugger was one of the better ones for DOS.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s