MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Proper way to free memory?

  • To: mathgroup at
  • Subject: [mg43597] Re: [mg43576] Proper way to free memory?
  • From: John Fultz <jfultz at>
  • Date: Tue, 23 Sep 2003 04:01:41 -0400 (EDT)
  • Reply-to: jfultz at
  • Sender: owner-wri-mathgroup at

No, MathLink is finished with your memory as soon as the MLPut call 
finishes.  There's not enough information in this email to debug what your 
problem might be.  The fact that it crashes on a free() would lead me to 
believe that the problem is probably one of the following...

* Memory wasn't allocated by malloc/calloc/realloc (e.g. could have been 
allocated by MathLink in an MLGet call).
* You free'd multiple times.
* The value of the pointer variable changed between time of allocation and 
time of free() callo.
* A memory boundary was overrun while writing to memory (possibly when 
filling the arrays).

But crashes (particularly with optimizations turned on) can be fickle, and 
so I can't say anything for certain.

I wouldn't be at all be surprised if you still see the crash if you 
comment out the calls to MLPutIntegerList and MLPutRealList.


John Fultz
jfultz at
User Interface Group
Wolfram Research, Inc.

On Sun, 21 Sep 2003 05:42:16 -0400 (EDT), Bob Buchanan wrote:
>I've written an external program to be called from Mathematica using
>mathlink. Inside one of the C functions I dynamically allocate an
>array of integers and another array of doubles. I fill them with data
>and then  I pass the results back to Mathematica. The ReturnType of
>the function is set to Manual. In the C program I use statements like
>MLPutFunction(stdlink,"List",2)        ;
>MLPutIntegerList(stdlink,nn,numneigh)        ;
>MLPutRealList(stdlink,dx,numneigh)        ;
>Since dx and nn are dynamically allocated, before the function returns
>I would like to free the space previously allocated. But the following
>statements create a segmentation violation at run time.
>free((void *)dx)        ;
>free((void *)nn)        ;
>Am I not supposed to free memory that I'm passing back to Mathematica?
>What am I doing wrong?
>Bob Buchanan
>Bob.Buchanan at

  • Prev by Date: Re: Solve real roots only ?
  • Next by Date: display memory errors.. maybe it's the postcript file format??
  • Previous by thread: Re: Proper way to free memory?
  • Next by thread: Simplify doesn't factor out 16 :-(