MATLAB: Stack overflow in mex

mex

Hi Friends,
I have one confusion regarding a mex code. The code is giving me a segmentation fault.
#include<math.h>
#include<stdio.h>
#include<mex.h>
#include <complex.h>
void mexFunction(int nlhs,mxArray *outputs[],int nrhs,const mxArray *prhs[])
{
const mxArray *R_locpr,*Nbpscpr,*mlpr,*Sym_x3_searchpr,*kmodpr,*fxpr,*fp_rsym_fmt_prodpr;
int R_loc_rowLen,R_loc_colLen,Sym_x3_search_rowLen,Sym_x3_search_colLen,i,j;
double *R_locr,*R_loci,*dmp_fmt_values,resolution,R_23_loc_in_cncr,R_23_loc_in_cnci,R_13_loc_in_cncr,R_13_loc_in_cnci,R_12_loc_in_cncr,R_12_loc_in_cnci;
double *Nbpsc,*kmod,*fxp,*fp_rsym_fmt_prod,*Sym_x3_searchr,*Sym_x3_searchi,*dummyr,*dummyi,*SYM_locr,*SYM_loci;
double complex *SYM_loc,*Sym_x3_search,*R_loc,R_23_loc_in_cnc,R_13_loc_in_cnc,R_12_loc_in_cnc,c;
mxArray *dmp_fmt,*rsym_prod,*xData[2],*array_ptr ;
int *ml;
R_locpr=prhs[5];
R_locr=mxGetPr(R_locpr);
R_loci=mxGetPi(R_locpr);
R_loc_rowLen=mxGetN(R_locpr);
R_loc_colLen=mxGetM(R_locpr);
for(i=0;i<R_loc_colLen;i++)
{
for(j=0;j<R_loc_rowLen;j++)
{
R_loc[i*R_loc_rowLen+j]=R_locr[i*R_loc_rowLen+j]+R_loc[i*R_loc_rowLen+j]* _Complex_I;
}
}
dmp_fmt = mexGetVariable( "caller","fp_x_dmp_fmt");
if(dmp_fmt==NULL)
{
mexErrMsgTxt("Could not get fp_x_dmp_fmt from MATLAB workspace.");
}
else
{
dmp_fmt_values=mxGetPr(dmp_fmt);
resolution = pow(0.5,dmp_fmt_values[1]);
}
if(R_loc_colLen == 3)
{
R_23_loc_in_cnc=R_loc[7];
R_13_loc_in_cnc=R_loc[6];
}
R_12_loc_in_cnc=R_loc[3];
mlpr=prhs[14];
Nbpscpr=prhs[3];
ml=mxGetPr(mlpr);
Nbpsc=mxGetPr(Nbpscpr);
Sym_x3_searchpr=prhs[1];
Sym_x3_searchr=mxGetPr(Sym_x3_searchpr);
Sym_x3_searchi=mxGetPi(Sym_x3_searchpr);
Sym_x3_search_rowLen=mxGetN(Sym_x3_searchpr);
Sym_x3_search_colLen=mxGetM(Sym_x3_searchpr);
for(i=0;i<Sym_x3_search_colLen;i++)
{
for(j=0;j<Sym_x3_search_rowLen;j++)
{
Sym_x3_search[i*Sym_x3_search_rowLen+j]=Sym_x3_searchr[i*Sym_x3_search_rowLen+j]+Sym_x3_searchi[i*Sym_x3_search_rowLen+j]* _Complex_I;
}
}
kmodpr=prhs[4];
printf("%d\n",mxGetN(kmodpr));
printf("%d",mxGetM(kmodpr));
kmod=mxGetPr(kmodpr);
if(kmodpr==NULL)
{
mexErrMsgTxt("Could not get kmod from MATLAB workspace.");
}
fxpr=prhs[13];
fxp=mxGetPr(fxpr);
fp_rsym_fmt_prodpr= mexGetVariable( "caller","fp_rsym_prod_fmt");
if(fp_rsym_fmt_prodpr==NULL)
{
mexErrMsgTxt("Could not get fp_rsym_prod_fmt from MATLAB workspace.");
}
else
{
fp_rsym_fmt_prod=mxGetPr(fp_rsym_fmt_prodpr);
}
if(R_loc_colLen == 3)
{
for (i=0;i<Sym_x3_search_colLen;i++)
{
for(j=0;j<Sym_x3_search_rowLen;j++)
{
SYM_loc[i*Sym_x3_search_rowLen+j]=0;
}
}
}
}
Here is the log file:
-----------------------------------------------------------------------
Segmentation violation detected at Fri Sep 28 17:51:08 2012
------------------------------------------------------------------------
Configuration:
MATLAB Version: 7.5.0.338 (R2007b)
MATLAB License: XXXXXX
Operating System: Linux 2.6.20-1.2962.fc6 #1 SMP Tue Jun 19 19:27:14 EDT 2007 i686
GNU C Library: 2.5 stable
Window System: The X.Org Foundation (70101000), display :0.0
Current Visual: 0x23 (class 4, depth 24)
Processor ID: x86 Family 15 Model 4 Stepping 7, GenuineIntel
Virtual Machine: Java 1.6.0 with Sun Microsystems Inc. Java HotSpot(TM) Client VM mixed mode, sharing
Default Charset: UTF-8
Register State:
eax = 00000400 ebx = 00afdbd0
ecx = 08b5a5f0 edx = 00000000
esi = 08b5a3f0 edi = 00000040
ebp = 054ca198 esp = 054ca120
eip = 00afc80e flg = 00010206
Stack Trace:
[0] list_decode.mexglx:mexFunction~(0, 0x054caa44, 15, 0x054caaa4) + 370 bytes
[1] libmex.so:mexRunMexFile(0, 0x054caa44, 15, 0x054caaa4) + 111 bytes
[2] libmex.so:Mfh_mex::runMexFileWithSignalProtection(int, mxArray_tag**, int, mxArray_tag**)(0x08e887e0, 0, 0x054caa44, 15) + 120 bytes
[3] libmex.so:Mfh_mex::dispatch_file(int, mxArray_tag**, int, mxArray_tag**)(0x08e887e0, 0, 0x054caa44, 15) + 254 bytes
[4] libmwm_dispatcher.so:Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**)(0x08e887e0, 0, 0x054caa44, 15) + 217 bytes
[5] libmwm_interpreter.so:ResolverFunctionDesc::CallFunction(int, mxArray_tag**, int, mxArray_tag**)(0x054cb024, 0, 0x054caa44, 15) + 808 bytes
[6] libmwm_interpreter.so:Resolver::CallMFunction(int, int, _m_operand*, m_operand_storage*, int, _m_operand*, m_operand_storage*, int*)(0x054cac00, 0, 1, 0xb27ed090) + 1530 bytes
[7] libmwm_interpreter.so:inResolveMFunctionCall(_m_function_desc*, int, int, _m_operand*, m_operand_storage*, int, _m_operand*, m_operand_storage*, int*, inMarshalType*, int, mpsTypeSequenceNlhs const*, mxArray_tag* (*)(int))(0xb26e50b0, 0, 1, 0xb27ed090) + 461 bytes
[8] libmwm_interpreter.so:accelImpl::MFunctionCall(_accelOp**)(0x054cb148, 0x054cb160, 0, 0x08d3cb68) + 216 bytes
[9] libmwm_interpreter.so:accelImpl::Exec()(0x054cb148, 0xb26d9a88, 0x054cb158, 0xb2600000) + 215 bytes
[10] libmwm_interpreter.so:accelCode::Call(inMarshalType*, int*) const(0xb2a36f80, 0x054cb320, 0x054cb31c, 0x015149a0) + 105 bytes
[11] libmwm_interpreter.so:inJit::ExecuteHotSegment(_inJitAccelInfo*, opcodes*, int*, int*)(0x054cb4a4, 0x054cb4ec, 0x054cb4bc, 0x054cb94c) + 1393 bytes
[12] libmwm_interpreter.so:.L752(1, 0, 6, 0) + 158 bytes
[13] libmwm_interpreter.so:protected_inInterp(inDebugCheck, int, int, opcodes, inPcodeNest_tag*, int*)(1, 0, 1, 0) + 121 bytes
[14] libmwm_interpreter.so:inInterPcodeSJ(inDebugCheck, int, int, opcodes, inPcodeNest_tag*, int*)(0, 0xb2bc6690, 0x054cb94c, 0x054cb94c) + 297 bytes
[15] libmwm_interpreter.so:inExecuteMFunctionOrScript(Mfh_mp*, bool)(0x08d76a20, 1, 0x054cbc6c, 0) + 625 bytes
[16] libmwm_interpreter.so:inRunMfile(int, mxArray_tag**, int, mxArray_tag**, Mfh_mp*, inWorkSpace_tag*)(0, 0x054cbc6c, 0, 0x054cbccc) + 640 bytes
[17] libmwm_interpreter.so:Mfh_mp::dispatch_file(_mdUnknown_workspace*, int, mxArray_tag**, int, mxArray_tag**)(0x08d76a20, 0, 0, 0x054cbc6c) + 64 bytes
[18] libmwm_interpreter.so:Mfh_mp::dispatch_file(int, mxArray_tag**, int, mxArray_tag**)(0x08d76a20, 0, 0x054cbc6c, 0) + 54 bytes
[19] libmwm_dispatcher.so:Mfh_file::dispatch_fh(int, mxArray_tag**, int, mxArray_tag**)(0x08d76a20, 0, 0x054cbc6c, 0) + 217 bytes
[20] libmwm_interpreter.so:inDispatchFromStack(int, char const*, int, int)(620, 0x08e91034 "mexcallfunctionmatlab", 0, 0) + 1078 bytes
[21] libmwm_interpreter.so:inDispatchCall(char const*, int, int, int, int*, int*)(0, 0x054cbfb8, 0x08e91028, 0x00b270f9) + 160 bytes
[22] libmwm_interpreter.so:.L777(2, 0, 0, 0) + 165 bytes
[23] libmwm_interpreter.so:protected_inInterp(inDebugCheck, int, int, opcodes, inPcodeNest_tag*, int*)(2, 0, 0, 0) + 121 bytes
[24] libmwm_interpreter.so:inInterPcodeSJ(inDebugCheck, int, int, opcodes, inPcodeNest_tag*, int*)(0, 0xb2bc48e0, 0x054cc3e8, 0x00f4ebf0) + 297 bytes
[25] libmwm_interpreter.so:inInterPcode(2, 0, 0x01063090, 0x01062310) + 158 bytes
[26] libmwm_interpreter.so:inEvalStringWithIsVarFcn(_memory_context*, char const*, EvalType, int, mxArray_tag**, inDebugCheck, _pcodeheader*, int*, bool (*)(void*, char const*), void*, bool)(0x00fc4648 ", 0x08e145a0 "mexcallfunctionmatlab\n", 0, 0) + 2892 bytes
[27] libmwm_interpreter.so:inEvalCmdWithLocalReturnandtype(char const*, int*, inDebugCheck, bool)(0, 0, 0xb3242d80, 0x00d6ba28) + 149 bytes
[28] libmwbridge.so:ThrowSignal(char const*)(0x08e145a0 "mexcallfunctionmatlab\n", 2, 0x054cd06c ", 0x008237c4) + 99 bytes
[29] libmwbridge.so:mnParser(0x054cd0f0, 0x08a8fc48, 1, 0x00559bc2) + 277 bytes
[30] libmwmcr.so:mcrInstance::mnParser()(0x08a8fc48, 0x054cf388, 0x08a8fc38, 0x08a42b90) + 52 bytes
This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.
If it is an official MathWorks function, please
follow these steps to report this problem to The MathWorks so we
have the best chance of correcting it:
The next time MATLAB is launched under typical usage, a dialog box will
open to help you send the error log to The MathWorks. Alternatively, you
can send an e-mail to segv@mathworks.com with the following file attached:
matlab_log
If the problem is reproducible, please submit a Service Request via:
http://www.mathworks.com/support/contact_us/ts/help_request_1.html
A technical support engineer might contact you with further information.
Thank you for your help. Save your workspace and restart MATLAB.
Anyone having idea about the problem or rather how to look stack file.
Thanks.

Best Answer

You can use the debugger of the C-Compiler to find the line, which causes the problems. Another idea would be to insert some mexPrintf() commands until you find the error.
The code is really hard to read, even if it would be formatted properly. E.g. you obtain inputs by mexGetVariable("caller", "fp_x_dmp_fmt");, which is ugly. Some spaces would increase the readability substantially. Therefore I do not expect, that a forum user will analyze the cause of your troubles.