0

I created a dll file from Mathlab .m files using Matlab coder. In matlab, the matlab code runs fine without any error and DLL creation was successful. MV2010 compiles and runs fine. However, when the software calls any function from this DLL in Visual studio, i get "unhandled exception" with memory location and "access violation reading location" with another memory location.

I thought the created dll is depending on another dll that is missing. So, I used dependency Walker to check the dependency. However, dependency walker did not show any missing dependency.

The Matlab functions takes 5 input variables ( four as a double and one as 1D-array of double) and return 4 double values. So I thought it might be the size of the array i am passing to the matlab-generated code is small. So, I re-sized the array. The original size was 750, while new size is 5000. Still getting the same unhandled exception.

Any idea how to debug this unhandled exception? anything else i need to check for?

/* Function Declarations */

extern void mat3(double ValueF, double DesiredValue, double io, double del, emxArray_real_T *a, double *status, double *slope_deg, double *fval, double *SVal);

and I am calling this method in MS2010 as shown below.

 double lValueF = 166.6;
 double lDesiredValue = 42.00;
 double io = 1.0;
 double del = 4.0;

 emxArray_real_T *lInputRoi;
 // a = emxCreate_real_T(height,width);
 lInputRoi = emxCreate_real_T(size_y,size_x);

// Converting 2D image to ID array
  if(!isHorz)
  {
      int lRow = 0;
      int lCol = 0;
      for (int row=roi.y+odd_y_offset;row< roi.y+size_y;row=row+2)
      {
          lCol = 0;
          for (int col=roi.x;col< roi.x+size_x;col++)
          {
              lInputRoi->data[lRow *size_x + lCol] = (real_T)img.at<uchar>(row,col);
              lCol++;
          }
          lRow++;
      }
  }
  else
  { 
      int lRow = 0;
      int lCol = 0;
      for (int col=roi.x+odd_x_offset;col< roi.x+size_x;col=col+2)
      {
          lRow = 0;
          for (int row=roi.y;row< roi.y+size_y;row++)
          {
              lInputRoi->data[lCol *size_y + lRow] = (real_T)img.at<uchar>(row,col);
              lRow++;
          }
          lCol++;
          size_x = roi.height;
          size_y = roi.width;
      }
  }

// initializing matlab generated function
  mat3_initialize();

  double lStatus = 0;

  double lslope_deg = 0.0;
  double lfreqval = 0.0;
  double lsfrvalue = 0.0;

  mat3(lValueF, lDesiredValue, io, del, lInputRoi, &lStatus, &lslope_deg, &lfreqval, &lsfrvalue);

note: 1) img -> is the input image data. 2) roi -> is an object of struct type that has two int variables (x and y) 3) MV2010 application is an application that used "MFC in a shared DLL" 4) I have seen a tutorial on how to integrate the Mathlab generated dll in Microsoft Visual studio. However, the tutorial does not get any error. http://www.mathworks.com/videos/integrate-code-into-visual-studio-77402.html

Thanks you for any help in advance.

ras red2004
  • 169
  • 1
  • 1
  • 10
  • If there is a DLL dependency issue, you usually get a nice window that says Windows can't find DLL foo.dll, so I don't think that's it. Have you tried running your program in the debugger? – Aaron Jan 07 '14 at 22:24
  • I am assuming that "running your program in the debugger" means running the program in debug mode. Yes, I ran it in debug and release. In both cases, I get the same unhnadled exception. – ras red2004 Jan 07 '14 at 22:48
  • Tutorial is terrible as it never shows the contents of the header file. I suspect you need to specify __declspec(dllexport) void mat3(...) in the DLL code and __declspec(dllimport) void mat3(...) in the exe code. – Michael Simbirsky Jan 08 '14 at 03:21
  • the header file is created by Mathlab, as shown below: /* Function Declarations */ #ifdef __cplusplus extern "C" { #endif extern void mat3(double ValueF, double DesiredValue, double io, double del, emxArray_real_T *a, double *status, double *slope_deg, double *fval, double *SVal); #ifdef __cplusplus } #endif #endif – ras red2004 Jan 08 '14 at 16:17
  • @Michael Simbirsky: I tried adding __declspec(dllimport) void mat3(...). I compiled/ran it in VS2010. However, when it calls one of the dll's methods, it give me the same unhandled execption :( – ras red2004 Jan 08 '14 at 16:31
  • @rasred2004 No, I mean running it using the Visual Studio debugger and examining call stack, memory, etc. "Running it in debug mode" to me just means running the debug build, which can be done outside Visual Studio. I suspect the problem has to do with either a calling convention mismatch between your EXE and the DLL (i.e., __stdcall vs __cdecl, etc), or a discrepancy in the expected memory use (e.g., matlab thinks you've given it a buffer that is larger than you really did, etc). – Aaron Jan 09 '14 at 23:03
  • This comment is a bit late but in cases like this generating a MEX function and running it in MATLAB will typically produce a runtime error. There are a few differences between what MATLAB and Coder allow. If one of those limitations is violated in standalone code, it can cause undefined behavior. So testing by generating a MEX function recommended. – Ryan Livingston May 17 '14 at 10:25

0 Answers0