f



MEX File Crashes MATLAB on repeated usage

I'm using a MEX file in another M-file. The MEX file gets called
several times inside the M-file with no crash.

If I rerun the M-file however, MATLAB crashes due to the MEX file.

If I clear the MEX file from memory(via "clear functions"), everything
works fine.

Any idea on what might be causing this problem?

Kind regards,

Ustun
0
ustunozgur (24)
5/21/2009 5:53:06 PM
comp.soft-sys.matlab 211266 articles. 24 followers. lunamoonmoon (257) is leader. Post Follow

5 Replies
551 Views

Similar Articles

[PageSpeed] 42

ustunozgur <ustunozgur@gmail.com> wrote in message <8ddcc8cc-c4f5-49e9-ab73-5dce98468e7a@m19g2000yqk.googlegroups.com>...
> I'm using a MEX file in another M-file. The MEX file gets called
> several times inside the M-file with no crash.
> 
> If I rerun the M-file however, MATLAB crashes due to the MEX file.
> 
> If I clear the MEX file from memory(via "clear functions"), everything
> works fine.
> 
> Any idea on what might be causing this problem?
> 

Yes. The mex file has a bug and is corrupting memory. Do you have the source code?

James Tursa
0
5/21/2009 9:57:01 PM
"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <gv4ind$9f0$1@fred.mathworks.com>...
> ustunozgur <ustunozgur@gmail.com> wrote in message <8ddcc8cc-c4f5-49e9-ab73-5dce98468e7a@m19g2000yqk.googlegroups.com>...
> > I'm using a MEX file in another M-file. The MEX file gets called
> > several times inside the M-file with no crash.
> > 
> > If I rerun the M-file however, MATLAB crashes due to the MEX file.
> > 
> > If I clear the MEX file from memory(via "clear functions"), everything
> > works fine.
> > 
> > Any idea on what might be causing this problem?
> > 
> 
> Yes. The mex file has a bug and is corrupting memory. Do you have the source code?
> 
> James Tursa

Strangely, the workaround no longer works if I close the GUI which calls the MEX file, and reopen it, calling clear all causes MATLAB to crash, saying

" Segmentation violation occurred within signal handler."
 "An error occured while running the atExit function for the MEX-file"

The file is at:
http://matlab.pastebin.com/m6f41ecce

Any ideas?
0
ustunozgur (24)
6/17/2009 5:54:01 PM
On 17 Jun, 19:54, "Ustun Ozgur" <ustunoz...@gmail.com> wrote:
> "James Tursa" <aclassyguywithakno...@hotmail.com> wrote in message <gv4in=
d$9f...@fred.mathworks.com>...
> > ustunozgur <ustunoz...@gmail.com> wrote in message <8ddcc8cc-c4f5-49e9-=
ab73-5dce98468...@m19g2000yqk.googlegroups.com>...
> > > I'm using a MEX file in another M-file. The MEX file gets called
> > > several times inside the M-file with no crash.
>
> > > If I rerun the M-file however, MATLAB crashes due to the MEX file.
>
> > > If I clear the MEX file from memory(via "clear functions"), everythin=
g
> > > works fine.
>
> > > Any idea on what might be causing this problem?
>
> > Yes. The mex file has a bug and is corrupting memory. Do you have the s=
ource code?
>
> > James Tursa
>
> Strangely, the workaround no longer works if I close the GUI which calls =
the MEX file, and reopen it, calling clear all causes MATLAB to crash, sayi=
ng
>
> " Segmentation violation occurred within signal handler."
> =A0"An error occured while running the atExit function for the MEX-file"
>
> The file is at:http://matlab.pastebin.com/m6f41ecce
>
> Any ideas?

First of all, it would be a lot easier to comment if you
posted the code here.

That being said, there are several issues with your code.
In line 89 you assign an imaginary part to an input varable.
That's a very bad idea. If you need the imaginary part (I
can't see that you use it in your code), assign a local
variable and copy the contents to this local variable.

Second, in the nested loops in lines 116-127, the first
inner loop (lines 118-121) manipulate the index variable,
while it is used in the loop in lines 123-126.

So there are a combination of technical and logical flaws
and blundres here.

Trow the whole thing away and start over. Do the business
logic in 'pure' C, test and debug this in a C IDE. Once
the business logic works, focus on the interface between
matlab and C.

Divide and conquer.

Rune
0
allnor (8509)
6/17/2009 9:14:10 PM
"Ustun Ozgur" <ustunozgur@gmail.com> wrote in message <h1bajp$iat$1@fred.mathworks.com>...
> 
> The file is at:
> http://matlab.pastebin.com/m6f41ecce
> 
> Any ideas?

I compiled it and ran it with random data and it seg faulted for me as well. Could you provide a small reasonable data set for the inputs?

James Tursa
0
6/18/2009 8:12:01 AM
"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <h1cssh$qpu$1@fred.mathworks.com>...
> "Ustun Ozgur" <ustunozgur@gmail.com> wrote in message <h1bajp$iat$1@fred.mathworks.com>...
> > 
> > The file is at:
> > http://matlab.pastebin.com/m6f41ecce
> > 
> > Any ideas?
> 
> I compiled it and ran it with random data and it seg faulted for me as well. Could you provide a small reasonable data set for the inputs?
> 
> James Tursa

Hi, thanks for your answers. Actually the code was written by another person (CML library); and I changed the calloc calls there to mxCalloc to see if it would solve the issue, but to no avail.

A sample input would be a "column" vector of random bits, and a constellation vector. The input length must be a multiple of M, constellation size.

data = randint(1,96);
constellation = [1 i -i -1];
symbols = Modulate(data, constellation);

The interesting thing is the code runs without a hitch in command window, and in GUI. But the problem occurs when I debug the code, then quit the debugger, or quit the GUI so on. 

One thing to note is also that clearing the MEX functions in the exit function of the GUI causes a crash, but if I quit the GUI, then clear the MEX files, then restart the GUI, it works fine.

Maybe I should rewrite as suggested, but I 

The code was written with Visual Studio compiler in mind I guess, so my gcc could be responsible for this, maybe. I'm running Debian Sid by the way, but I changed the mex options for gcc-4.1 just to be safe.

Ustun


PS: Here is the code for those who didn't see it in the pastebin:

/* File: Modulate.c

   Description: Complex K-dimensional M-ary modulator

   The calling syntax is:
      [output] = Modulate( input, S_matrix )

   Where:
      output   = K by N vector of modulated symbols

      input    = 1 by N*log2(M) vector of data bits 
	  S_Matrix = K by M complex matrix containing the constellation signals
	             Each Column is one signal, each signal is K-dimensional

      Note: For legacy purposes, S_matrix can be a M by 1 matrix when signal
	  set can be characterized by a one-dimensional complex value (QAM, etc).

   Copyright (C) 2005-2006, Matthew C. Valenti

   Last updated on May 6, 2006

   Function Modulate is part of the Iterative Solutions 
   Coded Modulation Library. The Iterative Solutions Coded Modulation 
   Library is free software; you can redistribute it and/or modify it 
   under the terms of the GNU Lesser General Public License as published 
   by the Free Software Foundation; either version 2.1 of the License, 
   or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
  
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
#include <math.h>
#include <mex.h>
#include <matrix.h>
#include <stdlib.h>

/* Input Arguments
prhs[0] is input
prhs[1] is S_matrix

/* Output Arguments
plhs[0] is output

/* main function that interfaces with MATLAB */
void mexFunction(
				 int            nlhs,
				 mxArray       *plhs[],
				 int            nrhs,
				 const mxArray *prhs[] )
{
  int    M, K;
  int    number_symbols, number_bits, bits_per_symbol;
  double *input, *Sr, *Si, *output_r, *output_i;
  int    i,j,temp_int,index;
  int    *data_int;

  /* make sure there are enough inputs */
  if (nrhs<2)
	  mexErrMsgTxt("Usage: [output] = Modulate( input, S_matrix )");

  /* read in input bits */
  number_bits = mxGetN(prhs[0]);
  input = mxGetPr(prhs[0]);

  /* read in the constellation matrix */
  
  /* for legacy purposes, see if it is a single column */
  if ( mxGetN( prhs[1] ) == 1 ) {
	  M = mxGetM( prhs[1] );
	  K = 1;
  } else {
	  M = mxGetN( prhs[1] );
	  K = mxGetM( prhs[1] );
  }  

  /* get the real part */
  Sr = mxGetPr(prhs[1]);

  /* if not complex, set imagainary part to zero */
  if (!mxIsComplex(prhs[1]) )
	  Si = mxCalloc( M*K, sizeof(double) );
  else
	  Si = mxGetPi(prhs[1]);

  /* determine the number of bits per symbol 
     for future development: check to make sure M is a power of 2 */
  bits_per_symbol = 0;
  temp_int = M;
  while (temp_int>1) {
	 temp_int = temp_int/2;
	 bits_per_symbol++;
  }
  
  /* determine the number of output symbols */
  number_symbols = number_bits/bits_per_symbol + (number_bits%bits_per_symbol>0);

  /* read in the input data and cast to int */
  data_int = mxCalloc( number_symbols*bits_per_symbol, sizeof(int) );
  for (i=0;i<number_bits;i++) 
	  data_int[i] = input[i];

  /* create the complex output matrix */
  plhs[0]=mxCreateDoubleMatrix( K, number_symbols, mxCOMPLEX);
  output_r = mxGetPr( plhs[0] );
  output_i = mxGetPi( plhs[0] );

  /* determine output */
  for (i=0;i<number_symbols;i++) { /* create each modulated symbol */
	  index = 0;
	  for (j=0;j<bits_per_symbol;j++) { /* go through each associated bit */
		  index = index << 1; /* shift to the left (multiply by 2) */
		  index += data_int[i*bits_per_symbol+j];
	  }
	  /* assign the K-dimensional output symbol */
	  for (j=0;j<K;j++) {
		  output_r[K*i+j] = Sr[K*index+j];
		  output_i[K*i+j] = Si[K*index+j];
	  }
  }

  /* free memory 
  free( data_int );
  
  if(!mxIsComplex(prhs[1]) )
     free(Si);
*/
}
0
ustunozgur (24)
6/18/2009 8:34:01 AM
Reply: