Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - My C leak finder

Author
Message
NeX the Fairly Fast Ferret
21
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 21st Oct 2009 21:00 Edited at: 21st Oct 2009 21:01
I found that a lot of the leak finders out there, simply sucked. They tried to hard to overcomplicate things or redefined functions (which didn't work here) and their output was hardly concise.

This leak finder I wrote in ~35 minutes including research time tracks all calls to its internal functions iMalloc and iFree, and so can identify memory freed erroneously and also lists all unfreed memory on request. Even if memory leaks aren't a major issue, this can help identify bugs as it presents the file and line on which the unallocated memory was freed and the file and line on which memory was not freed.

To install, include leak.h. Change your functions to iMalloc and iFree, and include a CheckLeaks() on close. To enable the debugging, #define DEBUG. There is also a little xprintf function included which is identical to printf in usage but on C99 systems switches on and off with the memory checker, so debug messages are hidden when the program is not in debug mode.

leak.h


leak.c


Any bugs, mistakes or inconsistencies are welcome.

Shouldn't the dropdown box read C++/C and not C++/C#? C# has its own option!

Athlon64 2.7gHz->OC 3.9gHz, 31C, MSi 9500GT->OC 1gHz core/2gHz memory, 48C, 4Gb DDR2 667, 500Gb Seagate + 80Gb Maxtor + 40Gb Maxtor = 620Gb, XP Home
Air cooled, total cost £160
Jeff032
18
Years of Service
User Offline
Joined: 13th Aug 2007
Location:
Posted: 21st Oct 2009 21:36 Edited at: 21st Oct 2009 21:36
Looks kind of useful. But it would only catch malloc leaks, and not ones resulting from, say new int[256], right?

If you are doing crossplatform c++ dev, I would recommend looking at a tool called dbx. It can be downloaded for free here:

http://developers.sun.com/sunstudio/downloads/index.jsp

for linux and solaris machines. (I had to download the tar version)

You can do memory leak checks like this:


Here is a sample program:


Here is the output:


It has other useful features, too. (can be used for debugging 'read from unallocated', etc. as well)

-Jeff

[EDIT]
Regarding C++/C#, perhaps it means C++/CLI? (in which case maybe it would be best to rename it that)

NeX the Fairly Fast Ferret
21
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 22nd Oct 2009 01:32
Quote: "not ones resulting from, say new int[256]"


Afraid not, I'm using C rather than fully blown C++ and so I don't have access to new. Everything is done using Malloc under unextended C from arrays to UDTs to (emulated) objects.

That Sun thing looks a little interesting; using GCC and Code::Blocks on Windows at the moment because GCC/C::B are very lightweight.

Athlon64 2.7gHz->OC 3.9gHz, 31C, MSi 9500GT->OC 1gHz core/2gHz memory, 48C, 4Gb DDR2 667, 500Gb Seagate + 80Gb Maxtor + 40Gb Maxtor = 620Gb, XP Home
Air cooled, total cost £160
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 22nd Oct 2009 07:22
Off-topic: Nex, have you tried out Visual C++ Express 2008? It has a great IDE and debugger.
NeX the Fairly Fast Ferret
21
Years of Service
User Offline
Joined: 10th Apr 2005
Location: The Fifth Plane of Oblivion
Posted: 23rd Oct 2009 03:20
Yes, it runs horribly on a netbook and takes up loads of space.

Athlon64 2.7gHz->OC 3.9gHz, 31C, MSi 9500GT->OC 1gHz core/2gHz memory, 48C, 4Gb DDR2 667, 500Gb Seagate + 80Gb Maxtor + 40Gb Maxtor = 620Gb, XP Home
Air cooled, total cost £160

Login to post a reply

Server time is: 2026-06-10 10:09:23
Your offset time is: 2026-06-10 10:09:23