segmentation viloation detected in MATLAB

Hi all,

I am trying to implement the code give in Nvidia white paperclick for white paper.I have written following code:

#include "mex.h" 

void mexFunction(int nlhs, mxArray *plhs[], 

int nrhs, const mxArray *prhs[]) 

{ 

int i, j, m, n; 

double *data1, *data2; 

/* Error checking */ 

if (nrhs != nlhs) mexErrMsgTxt 

("The number of input and output arguments must be the same.");

for (i = 0; i < nrhs; i++) 

/* Find the dimensions of the data */ 

 m = mxGetM(prhs[i]); 

 n = mxGetN(prhs[i]); 

/* Create an mxArray for the output data */ 

 plhs[i] = mxCreateDoubleMatrix(m, n, mxREAL); 

/* Retrieve the input data */ 

 data1 = mxGetPr(prhs[i]); 

/* Create a pointer to the output data */ 

 data2 = mxGetPr(plhs[i]); 

/* Put data in the output array after squaring them */ 

 for (j = 0; j < m*n; j++) 

 { 

  data2[j] = data1[j] * data1[j]; 

 } 

}

But It is given the error(------------------------------------------------------------------------

   Segmentation violation detected at Fri Apr 09 17:41:21 2010

Configuration:

MATLAB Version: 7.1.0.246 (R14) Service Pack 3

MATLAB License: 161051

Operating System: Microsoft Windows XP

Window System: Version 6.1 (Build 7600)

Processor ID: x86 Family 6 Model 15 Stepping 13, GenuineIntel

Virtual Machine: Java 1.5.0 with Sun Microsystems Inc. Java HotSpot™ Client VM mixed mode

Default Charset: windows-1252

Register State:

EAX = 00000001 EBX = 047177f0

ECX = 04764b20 EDX = 00000001

ESI = 0000003c EDI = 00000001

EBP = 00cdde80 ESP = 00cdde7c

EIP = 78503387 FLG = 00010246

Stack Trace:

[0] libmx.dll:_mxGetN(60, 0x00cde628, 0, 0x047177f0) + 7 bytes

[1] Square.mexw32:0x04a113b5(1, 0x00cde628, 1, 0x00cde688)

[2] libmex.dll:_mexRunMexFile(1, 0x00cde628, 1, 0x00cde688) + 103 bytes

[3] libmex.dll:private: void __thiscall Mfh_mex::runMexFileWithSignalProtection(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(1, 0x00cde628, 1, 0x00cde688) + 73 bytes

[4] libmex.dll:public: virtual void __thiscall Mfh_mex::dispatch_file(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(1, 0x00cde628, 1, 0x00cde688) + 169 bytes

[5] m_dispatcher.dll:public: virtual void __thiscall Mfh_file::dispatch_fh(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(1, 0x00cde628, 1, 0x00cde688) + 271 bytes

[6] m_interpreter.dll:int __cdecl mdDispatch(int,char const *,int,struct mxArray_tag * *,int,struct mxArray_tag * *,class Mfh_MATLAB_fn * *)(537, 0x049f6c40 “Square”, 1, 0x00cde628) + 88 bytes

[7] m_interpreter.dll:_inDispatchFromStack(537, 0x049f6c40 “Square”, 1, 1) + 800 bytes

[8] m_interpreter.dll:enum opcodes __cdecl inDispatchCall(char const *,int,int,int,int *,int *)(0x049f6c40 “Square”, 537, 1, 1) + 145 bytes

[9] m_interpreter.dll:int __cdecl inInterp(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag volatile *,int *)(2, 0, 0, 0) + 2534 bytes

[10] m_interpreter.dll:int __cdecl inInterPcodeSJ(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag *,int *)(2, 0, 0, 0) + 282 bytes

[11] m_interpreter.dll:_inInterPcode(2, 0x784d6e7c, 0, 0) + 81 bytes

[12] m_interpreter.dll:enum inExecutionStatus __cdecl in_local_call_eval_function(int *,struct _pcodeheader *,int *,struct mxArray_tag * * const,enum inDebugCheck)(0x00cdf240, 0x00cdf338, 2, 1) + 153 bytes

[13] m_interpreter.dll:$L74683(0x784d6e7c, 0x046dbb20 “b=Square(a);\n”, 0, 0) + 184 bytes

[14] m_interpreter.dll:enum inExecutionStatus __cdecl inEvalCmdWithLocalReturnandtype(char const *,int *,enum inDebugCheck)(0x046dbb20 “b=Square(a);\n”, 0, 2, 0x00cdf384) + 69 bytes

[15] m_interpreter.dll:_inEvalCmdNoEnd(0x046dbb20 “b=Square(a);\n”, 1, 255, 0) + 16 bytes

[16] bridge.dll:enum inExecutionStatus __cdecl ThrowSignal(char const *)(0x046dbb20 “b=Square(a);\n”, 0x76961225, 0x00e31e20, 0x00e31e20) + 60 bytes

[17] bridge.dll:$L86781(0, 0, 0x784d6e58, 0x7c36b71f) + 432 bytes

[18] mcr.dll:public: void __thiscall mcrInstance::mnParser(void)(2895959, 0x795c3a44, 0x76616461 "¯°ÿzrrÿzrrÿ½¸¹ÿØÖÕÿåÙØÿåÙØÿåÙØÃ

¿Ã¥…", 0x74616d20) + 93 bytes

[19] MATLAB.exe:0x00401ca9(4194304, 0, 2895959, 0x00e31e20)

[20] MATLAB.exe:0x00403d9f(0xfffde000, 0x00cdffd4 “ìÿÍ”, 0x77239d72, 0xfffde000)

[21] kernel32.dll:0x76963677(0xfffde000, 0x77ec80a3, 0, 0)

[22] ntdll.dll:0x77239d72(0x00403c1a, 0xfffde000, 0, 0)

[23] ntdll.dll:0x77239d45(0x00403c1a, 0xfffde000, 0, 0)

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 in reporting this problem to The MathWorks so

that we have the best chance of correcting it:

  1. Send this crash report to segv@mathworks.com for automated analysis.

    For your convenience, this information has been recorded in:

    C:\Users\HBEONG~1\AppData\Local\Temp\matlab_crash_dump.1236
    
  2. Also, if the problem is reproducible, send the crash report to

    support@mathworks.com along with:

    - A specific list of steps that will reproduce the problem
    
    - Any M, MEX, MDL or other files required to reproduce the problem
    
    - Any error messages displayed to the command window
    

    A technical support engineer will contact you with further information.

Thank you for your assistance. Please save your workspace and restart

MATLAB before continuing your work.).Can you tell me what is wrong in this code and how can I solve this problem.

Thanks in advance

Monica,

you have no opening curly bracket in or immediately after `for (i = 0; i < nrhs; i++)’ line. Is this by design? The value of variable i after this loop would be nrhs, which might crash the line just past that loop, i.e. n = mxGetN(prhs[i]) due to out of array bound access: I presume the dimensionality of prhs array is nrhs.

Thanks for your Help.Now It is working fine.

But I am facing problems with using nvmex ,when I am trying to run Szeta.cu in Matlab ,It is being given this Error:

nvcc fatal : Cannot find compiler ‘cl.exe’ in PATH

C:\PROGRA~1\MATLAB71\BIN\NVMEX.PL: Error: Compile of ‘Szeta.cu’ failed.

??? Error using ==> nvmex

Unable to complete successfully.

I have Already set the variable of nvmexopts in a following way:

@echo off

rem MSVC80OPTS.BAT

rem

rem Compile and link options used for building MEX-files

rem using the Microsoft Visual C++ compiler version 8.0

rem

rem $Revision: 1.1.10.2 $ $Date: 2006/06/23 19:04:53 $

rem

rem ************************************************************


rem General parameters

rem ************************************************************


set MATLAB=%MATLAB%

set VS80COMNTOOLS=%VS80COMNTOOLS%

set VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 9.0\VC\bin

set VCINSTALLDIR=%VSINSTALLDIR%\VC

set PATH=%VCINSTALLDIR%\BIN;%VCINSTALLDIR%\PlatformSDK\bin;%VSINSTALLDIR%\Common7\IDE;%VSINSTALLDIR%\SDK\v2.0\bin;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\Tools\bin;%VCINSTALLDIR%\VCPackages;%MATLAB_BIN%;%PATH%

set INCLUDE=%VCINSTALLDIR%\ATLMFC\INCLUDE;%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\PlatformSDK\INCLUDE;%VSINSTALLDIR%\SDK\v2.0\include;%INCLUDE%

set LIB=%VCINSTALLDIR%\ATLMFC\LIB;%VCINSTALLDIR%\LIB;%VCINSTALLDIR%\PlatformSDK\lib;%VSINSTALLDIR%\SDK\v2.0\lib;%MATLAB%\extern\lib\win32;%LIB%

set MW_TARGET_ARCH=win32

rem ************************************************************


rem Compiler parameters

rem ************************************************************


set COMPILER=nvcc

set COMPFLAGS=-c -D_WCHAR_T_DEFINED -Xcompiler “/c /Zp8 /GR /W3 /EHsc- /Zc:wchar_t- /DMATLAB_MEX_FILE /nologo”

set OPTIMFLAGS=-D_WCHAR_T_DEFINED -Xcompiler “/MD /O2 /Oy- /DNDEBUG”

set DEBUGFLAGS=-D_WCHAR_T_DEFINED -Xcompiler “/MD /Zi /Fd”%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb"

set NAME_OBJECT=-o

rem ************************************************************


rem Linker parameters

rem ************************************************************


set LIBLOC=%MATLAB%\extern\lib\win32\microsoft

set LINKER=link

rem set LINKER=nvcc

set LINKFLAGS=/dll /export:%ENTRYPOINT% /MAP /LIBPATH:“%LIBLOC%” libmx.lib libmex.lib libmat.lib /implib:%LIB_NAME%.x /MACHINE:X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib

set LINKOPTIMFLAGS=

set LINKDEBUGFLAGS=/DEBUG /PDB:“%OUTDIR%%MEX_NAME%%MEX_EXT%.pdb”

set LINK_FILE=

set LINK_LIB=

set NAME_OUTPUT=/out:“%OUTDIR%%MEX_NAME%%MEX_EXT%”

set RSP_FILE_INDICATOR=@

rem ************************************************************


rem Resource compiler parameters

rem ************************************************************


set RC_COMPILER=rc /fo “%OUTDIR%mexversion.res”

set RC_LINKER=

set POSTLINK_CMDS=del “%OUTDIR%%MEX_NAME%.map”

set POSTLINK_CMDS1=del %LIB_NAME%.x

set POSTLINK_CMDS2=mt -outputresource:“%OUTDIR%%MEX_NAME%%MEX_EXT%”;2 -manifest “%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest”

set POSTLINK_CMDS3=del “%OUTDIR%%MEX_NAME%%MEX_EXT%.manifest”

Can you tell me the solution of this problem?

Thanks in Advance

Monica,

Sorry, I’m cheap, that being one of the reasons girls don’t like me too much. I hence use free software: linux instead of windows and octave instead of matlab.

Cannot help you with this one. Good luck!

Monica,

Sorry, I’m cheap, that being one of the reasons girls don’t like me too much. I hence use free software: linux instead of windows and octave instead of matlab.

Cannot help you with this one. Good luck!