Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Mathlink memory preservation

  • To: mathgroup at
  • Subject: [mg56507] Re: [mg56455] Mathlink memory preservation
  • From: at
  • Date: Tue, 26 Apr 2005 21:54:04 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


  The global variable "image" is allocated in the heap by "new" operator in
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.

  Will this make things a little bit clear ?


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).

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.

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++)

#ifdef DEBUG
         std::ofstream file ("/home/TUE/bjanssen/debug.txt");
         file << *image << std::endl;

             // some junk that for "some" reason has to be here

// 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);


Thanks in advance,
Kind Regards,
Bart Janssen

  • Prev by Date: Re: Mathlink memory preservation
  • Next by Date: Re: can Mathematica be useful for this?
  • Previous by thread: Re: Mathlink memory preservation
  • Next by thread: "large" matrices, Eigenvalues, determinants, characteristic polynomials