Stack overflow in mex

13 views (last 30 days)
Harshit
Harshit on 3 Oct 2012
Commented: Steven Lord on 13 Sep 2019
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.
  4 Comments
JasonLuo
JasonLuo on 26 Jan 2018
Did you find solution to the problem? I encountered the similar case.
Jan
Jan on 26 Jan 2018
@JasonLuo: And the solution will be similar to the posted method: Use a debugger to identify the problem. Obviously your C code contains a bug. As long as you do not show us the code (in a new thread - do not hijack this one), all we can do is to suggest to find and fix it.

Sign in to comment.

Accepted Answer

Jan
Jan on 3 Oct 2012
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.

More Answers (3)

priyadharshini p
priyadharshini p on 13 Sep 2019
clear pcode;
clear classes;
mypi = raspi;
mycam = cameraboard(mypi,'Resolution','1280x720');
for ii = 1:1000
img = snapshot(mycam);
imagesc(img);
drawnow
end
mycam.Rotation = 180;
videoFrame = record(mycam,'myvideo.avi',60);
foreground = step(foregroundDetector,videoFrame);
imwrite(videoFrame,'D:\referenceimage.jpg','jpg');
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
se = strel('square', 3); % morphological filter for noise removal
referenceimage=imread('D:\referenceimage.jpg');
X=zeros(2,121);
Y=zeros(2,121);
Z=zeros;
while ~isDone(videoReader)
frame = step(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);
%-----------------------SPEED ---------------------------%
frame2=((im2double(frame))-(im2double(referenceimage)));
frame1=im2bw(frame2,0.1);
[Labelimage]=bwlabeln(frame1);
stats=regionprops(Labelimage,'basic');
BB=stats.BoundingBox;
i=2; %fblasst for
X(i)=BB(1);
Y(i)=BB(2);
Dist=((X(i)-X(i-1))^2+(Y(i)-Y(i-1))^2)^(1/2);
Z(i)=Dist;
M=median(Z);
%disp(M);
%clc;
%disp('speed=')
Speed=((M)*(120/8))/(4);
%disp(Speed);
%SPEED = M ???????????
i = i + 1;
SE = strel('disk',6);
frame3=imclose(frame1,SE);
step(videoReader);
pause(0.05);
%if(i==121) end; ??
%-----------------------SPEED ---------------------------%
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 150);
%bbox = step(blobAnalysis, filteredForeground);
[areas, centroids, bbox] = step(blobAnalysis, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
% Display the number of cars found in the video frame
%ICI
%disp(centroids); //show...
%disp(size(centroids));//show....
numCars = size(bbox, 1); %cars ...
numCars_str = ['car number:', num2str(size(bbox, 1))];
speed_str = [num2str(Speed),'KM/h'];
result = insertText(result,[10,10],numCars_str, 'BoxOpacity', 1, ...
'FontSize', 14);
for i=1:size(bbox, 1)
result1 = insertText(result,[centroids(i,1),centroids(i,2)], speed_str, 'BoxOpacity', 1, ...
'FontSize', 20);
step(videoPlayer, result); % display the results
disp('numCars');
disp(numCars)
step(videoPlayer, result1);
disp('speed');
disp(speed_str);
% find total number of cars in whole frame
for ii=1:length(videoFrame)%numof frames
totalcount(ii)=videoFrame(ii);
end
totcars=sum(totalcount)/5;
end
end
disp('Total number of cars in entire video')
disp(round(totcars));
stop(mycam);
getFile(mypi,'myvideo.avi','E:\mat');
error
Error using raspi.internal.cameraboard/record
Too many output arguments.
Error in mm (line 11)
videoFrame = record(mycam,'myvideo.avi',60);
  1 Comment
Steven Lord
Steven Lord on 13 Sep 2019
This is not related to the original question. Please post your code in a new question using the Ask link at the top of the page.

Sign in to comment.


priyadharshini p
priyadharshini p on 13 Sep 2019
Error using raspi.internal.cameraboard/record
Too many output arguments.
Error in mm (line 11)
videoFrame = record(mycam,'myvideo.avi',60);

priyadharshini p
priyadharshini p on 13 Sep 2019
How to solve this error

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!