Jump to content


realloc fixes program crash


  • You cannot reply to this topic
7 replies to this topic

#1 hunguptodry

    New Member

  • Members
  • PipPip
  • 31 posts

Posted 20 March 2007 - 07:59 AM

1) i allocate some memory in my program with a call to malloc ...
cn->cp = (cinst *) malloc(n*sizeof(cinst));
program crashes in fscanf some time later.

2) i replace the single malloc call with 2 calls ...
cn->cp = (cinst *) malloc(100*sizeof(cinst));
cn->cp = realloc(cn->cp,n*sizeof(cinst));
the problem goes away.

3) the debugger is no help here.

4) r there any tools i can use to track down such problems?

#2 hovermonkey

    Member

  • Members
  • PipPip
  • 38 posts

Posted 20 March 2007 - 01:24 PM

Try posting the code around the fscanf as this is probably the culprit.

#3 SigKILL

    Valued Member

  • Members
  • PipPipPip
  • 200 posts

Posted 20 March 2007 - 06:10 PM

3) A debugger always helps.
4) A debugger is 'the' tool for these things. Or you could use something like boundsChecker, which would be overkill IMHO.

The problem is obviously not in the code you posted, so you should provide us with more details. AFAIK, realloc() calls malloc() on most compilers so your 2) is unnecessary. If you tend to write outside allocated memory in debug mode, the heap check tends to assert() at seemingly random positions (which could be your problem).

EDIT: Fixed bad sentence ( I do not think boundsChecker is the worst....).

#4 Sol_HSA

    Senior Member

  • Members
  • PipPipPipPip
  • 482 posts
  • LocationNowhere whenever

Posted 20 March 2007 - 07:15 PM

Yes, sounds like a memory trashing problem. If you have money to throw at the problem, get boundschecker or purify - if you're on linux, electric fence is a free library that might help.

If the above is not a solution, you can try allocating, say, 2k more than you need, fill it with a known value (say, 0xfc) and use the memory in the middle. Track this in debugger memory view, and you should see if something writes outside the allocated range. You can also set memory modify breakpoints.
http://iki.fi/sol - my schtuphh

#5 Nick

    Senior Member

  • Members
  • PipPipPipPip
  • 1225 posts

Posted 21 March 2007 - 12:11 AM

If you're using C++, look for Paul Nettle's Memory Manager (MMGR). Saved me a couple headaches...

#6 juhnu

    Valued Member

  • Members
  • PipPipPip
  • 292 posts

Posted 21 March 2007 - 03:14 AM

Sol_HSA said:

Yes, sounds like a memory trashing problem. If you have money to throw at the problem, get boundschecker or purify
...and GlowCode

#7 monjardin

    Senior Member

  • Members
  • PipPipPipPip
  • 1033 posts

Posted 21 March 2007 - 04:47 AM

Is there another source for the mmgr.zip file? The link from Nettle's flipcode article seems to be dead.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

#8 Dia

    DevMaster Staff

  • Administrators
  • 1097 posts

Posted 21 March 2007 - 07:07 AM

Here's the official source:

http://www.paulnettl...ory_Manager.zip

I've personally used it as well in the past, and it does a pretty good job.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users