Mathlink memory preservation
- To: mathgroup at smc.vnet.net
- Subject: [mg56455] Mathlink memory preservation
- From: Bart Janssen <b.j.janssen at tue.nl>
- Date: Tue, 26 Apr 2005 01:33:00 -0400 (EDT)
- Organization: Eindhoven University of Technology, The Netherlands
- Sender: owner-wri-mathgroup at wolfram.com
Hello,
I'm writing a C++ mathlink program that should do some image processing.
The main idea is:
1. Load the image into the memory of the mathlink module by
PreLoadImage[Image__?(MatrixQ[#, NumberQ]&)]
2. Obtain a filter response from a filter parameterised by a couple of
Reals (This way we don't have to send the entire image over the link
over and over and thus obtain higher evaluation speeds).
Problem:
After the preload function is called, the destructor of my image is
magically called. Because of this the data is not available to the
filter function.
Question:
How can I make sure my data stays in memory?
(Suggestions on improving the code are welcome too :) )
Source Code looks like this:
//-----------------------------------------
#include <mathlink and other stuff>
// global variable that holds the image that is processed
PixelMap<double> *image;
//
// Load the image data from mathematica into
// the global variable image
//
void preloadimage(){
double* points;
long *pdims;
long prank;
MLGetRealArray(stdlink, &points, &pdims,NULL,&prank);
// Remove the old image from memory
// Appearantly mathlink does this for me...
/* if(image!=NULL)
delete image;
*/
image = new PixelMap<double>(pdims[1],pdims[0]);
for(int y=0;y<pdims[0];y++)
for(int x=0;x<pdims[1];x++)
image->SetPixel(x,y,points[pdims[1]*y+x]);
#ifdef DEBUG
std::ofstream file ("/home/TUE/bjanssen/debug.txt");
file << *image << std::endl;
file.close();
#endif
// some junk that for "some" reason has to be here
MLNewPacket(stdlink);
MLPutInteger(stdlink,1);
}
//
// Return a filter response from a filter
// applied on global image
// NOTE: preloadimage has to be called on forehand
//
void filter()
{
// ... same trickery as above, but then for other arguments
double retval = filter(args,image);
MLNewPacket(stdlink);
MLPutReal(stdlink,retval);
}
//-----------------------------------------
Thanks in advance,
Kind Regards,
Bart Janssen
- Follow-Ups:
- Re: Mathlink memory preservation
- From: Zhengji Li <zhengji.li@gmail.com>
- Re: Mathlink memory preservation