NO

Author Topic: memory leak  (Read 4069 times)

thadmiller

  • Guest
memory leak
« on: January 23, 2007, 04:22:03 am »
I'm nearing completion of my first C program (a personal woohoo), but I'm having a problem with a nasty memory leak.  I have threads, pointers, arrays, structures, and functions, so I'm not even sure where to start looking.  I did notice if I comment out all the listview updates, the leak gets smaller, but memory usage still climbs after running a few traces.

I'd post the code in a code window, but I don't know where the problem is, and I think 550+ lines is a bit difficult to read in a browser, so I'll just attach the project.  I'm sure I'm making a newbie mistake, but I'd appreciate any help.

To see the leak in action, just set the Trace Number to something around 100, and click the Trace button - on my system, it climbs from around 884k to 3,184k.

-Thad


EDIT: New version posted 2007/01/27

JohnF

  • Guest
memory leak
« Reply #1 on: January 23, 2007, 06:33:56 am »
You could try using my TMalloc to help find the problem.

http://www.johnfindlay.plus.com/pellesc/libs/libs.html

John

severach

  • Guest
memory leak
« Reply #2 on: January 24, 2007, 12:31:18 am »
Your first step is to throw that ZIP program in the trash or report a bug. RoutePing.ppj and RoutePing.tag are listed twice and the later version of main.h didn't make it in which is blocking compile. After I made up some constants the next problems are that main.ico and icmp.lib are missing. icmp.lib is hard to get and according to some LCC docs it has been replaced by iphlpapi.lib which is available.

Your memory leak is because CStr/iFmt/fFmt create new buffers with calloc() and nothing free's them. Here is the crude but easy fix:

Code: [Select]
char* CStr(int i)
{
  //register char* strtmp = (char*)calloc(255,sizeof(char));
  static char strtmp[255];
  sprintf(strtmp,"%d",i);
  return strtmp;
}

thadmiller

  • Guest
memory leak
« Reply #3 on: January 27, 2007, 06:59:39 am »
(Attachment uploaded to first post in this thread)

Sorry about the zip file - I never had any issues with ZipGenius before, so I'm sure I botched something up.  Thanks for taking a look at it, and for the suggestions.  I changed the couple functions, and I think that cleared up the memory leak.

I don't know why, but the Win2000 computer I was using for testing did not have iphlpapi.dll on it, so it's still using icmp.dll (I just used polib /OUT to created icmp.lib) - I'll change it to iphlpapi.dll once I figure out why it's missing from that system.

I also completed the reverse lookup, so I think I have my first fully functional app written in C.  I want to add some error trapping in case non-numeric characters are entered where they shouldn't be, and also prevent the Trace button from being pressed while a trace is already running.  One more thing - I want to add an aggression selection - right now, it will ping as fast as the network will allow - I want to allow the user to have the ability to slow things down a bit.

So other than poor commenting in my code, did I miss any C shortcuts?  Any comments or suggestions?

Thanks again...
-Thad

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2079
    • http://www.smorgasbordet.com
memory leak
« Reply #4 on: January 28, 2007, 02:38:28 pm »
I think it's always a very good idea to check the return value from malloc(). Since the function can fail, and return a null pointer, it usually will - at some point. The you will be really happy you checked it.

Either add a check after each call to malloc(), or write your own wrapper ('my_alloc', 'checked_malloc', 'im_so_happy_i_did_this_malloc'...) that calls malloc(), checks the pointer, and either returns it or complains and dies...

Apart from this, any comments would be about the 'style' - which is really a personal thing...
/Pelle