Re: Mathlink memory preservation
- To: mathgroup at smc.vnet.net
- Subject: [mg56510] Re: [mg56455] Mathlink memory preservation
- From: Zhengji Li <zhengji.li at gmail.com>
- Date: Tue, 26 Apr 2005 23:00:19 -0400 (EDT)
- References: <200504260533.BAA14376@smc.vnet.net>
- Reply-to: Zhengji Li <zhengji.li at gmail.com>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, The global variable "image" is allocated in the heap by "new" operator in "preloadimage", so I am sure that when calling "filter" "image" should not be NULL. By the way, your mathlink program is a stand-alone program and communicating with Mathematica via MathLink (over TCP/IP), and it is impossible for Mathematica to free the memory allocated in it. On 4/26/05, Bart Janssen <b.j.janssen at tue.nl> wrote: > 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 > >
- References:
- Mathlink memory preservation
- From: Bart Janssen <b.j.janssen@tue.nl>
- Mathlink memory preservation