[comp.sys.amiga.tech] Memory Freed Twice guru

ejkst@cisunx.UUCP (Eric J. Kennedy) (09/24/88)

I've got a very persistant problem with a number crunching program I'm
working on.  Essentially, I have several (double *) pointers, plus a
number of integer and double variables.  I open a file, read some
integers, and use malloc() to allocate space for arrays for the
pointers, read data into the arrays, and close the file.  I then call
the main curve fitting routine, it does its thing (it does everything
with static and automatic variables, plus the arrays whose pointers are
passed to it) and returns the result.  The main program prints the
result, frees the space, and quits.

Simple, no?

Every time, the program does all of the above, prints out the *right*
*answer*, and crashes.  Guru 81000009, memory freed twice.  Using Manx db,
it always crashes in __FreeMem, with some impossibly huge value being in
D0, which is supposed to be the amount of memory to free.  (Gosh, I sure
wish I had 1.2 Gigabytes of ram so I could free it!)

I'm using Manx 3.4a.  It does this under all memory models, 16 or 32 bit
ints, manx or amiga ieee libraries, with or without explicitly freeing
the memory in the program, and using malloc() and free() or AllocMem()
and FreeMem().

Ok, you say.  I've got a bug in the program.  _But_ the program comes
up with the right answer!!  And it works perfectly on an MS-DOS
machine.  So what's going on?

I seem to remember a discussion about the memory freed twice guru, but I
don't remember the details.  (Just that I wanted to remember for future
reference!)

If I switch to ffp instead of ieee, it doesn't do this, but it still
crashes a lot.  Usually 00000003 or 00000004.  I need double precision,
though.


Any help would be greatly appreciated, because it pisses me off that
after a week I finally get it working on the IBM, but it crashes my
Amiga!!


-- 
------------
Eric Kennedy
ejkst@cisunx.UUCP

cmcmanis%pepper@Sun.COM (Chuck McManis) (09/26/88)

In article <12741@cisunx.UUCP> (Eric J. Kennedy) writes:
>Every time, the program does all of the above, prints out the *right*
>*answer*, and crashes.  Guru 81000009, memory freed twice.  Using Manx db,
>it always crashes in __FreeMem, with some impossibly huge value being in
>D0, which is supposed to be the amount of memory to free.  (Gosh, I sure
>wish I had 1.2 Gigabytes of ram so I could free it!)
>
>I'm using Manx 3.4a.  It does this under all memory models, 16 or 32 bit
>ints, manx or amiga ieee libraries, with or without explicitly freeing
>the memory in the program, and using malloc() and free() or AllocMem()
>and FreeMem().
>
>Ok, you say.  I've got a bug in the program.  _But_ the program comes
>up with the right answer!!  And it works perfectly on an MS-DOS
>machine.  So what's going on?

Welcome to the Amiga! Where real program bugs cause real program crashes!
(No, I'm not being cruel its one of those things that is nice about the 
Amiga) You see, your memory allocation bug is in the MS-DOS version too
but MS-DOS does the equivalent of a reboot when a program exits. This 
restores memory and you are never the wiser! This behaviour can cover up
a lot of sins and of course on the Amiga you can't get away with it
because these kinds of errors cause other programs to crash. 

Anyway, to track it, there was a program on the developers disk called
"snoop" that used to print out memory allocations and deallocations,
combined with some printfs() it can be used to track this problem.
It was a really simple SetFunction of the exec vectors. Anyway,
the fact that you get the correct answer can only be used to identify 
that the memory problem happens sometime past the point where it generates
the results. You might also check the memlist before you call free to
see if it has been stomped on.


--Chuck McManis
uucp: {anywhere}!sun!cmcmanis   BIX: cmcmanis  ARPAnet: cmcmanis@sun.com
These opinions are my own and no one elses, but you knew that didn't you.

dan-hankins@cup.portal.com (09/29/88)

Pmsn (Poor man's snoop) will show you all the allocate and deallocate
calls in detail.  It's in comp.binaries.amiga.


Dan Hankins