Error "Access violation executing location 0x00000000" when using mwArray in Visual-C++

I am trying to use Matlab function in VSC++ mfc 2012 to edit image. I got an this error:
Unhandled exception at 0x74E9C9F1 in pa2sample.exe: 0xC0000005: Access violation executing location 0x00000000
when I use mwArray. This is my code in C++:
BYTE* im=new BYTE();
ImgProc(1,mwArray (im),mwArray(img));
This code will use the Matlab function to convert image into grayscale image. img variable is the BYTE pointer to the buffer where image places. im variable is the output image.
ImgProc is a Matlab function:
function im=ImgProc(x)
im=rgb2gray(x);
Here is the ImgProc C++ function after converted from Matlab
ImgProc(int nargout, mwArray& im, const mwArray& x);
I appreciate if someone could help me. I have got stuck for 2 days.

Answers (1)

Hi,
you don't need to allocate memory for the output. MATLAB will do this for you:
mwArray out;
ImgProc(1,out,mwArray(img));
Also are you sure the call to mwArray(img) succeeds? What is img (data type and size)? AFAIK there is no constructor which would match your usecase here

12 Comments

Im not sure if it succeeds or not. but I used GetCurrentImage() function to get an image. it returns a pointer (here is img variable) to a buffer where the complete image will be stored in device-independent bitmap (DIB) format. Cast the pointer to a long pointer type.
You cannot simply convert this in an mwArray by calling mwArray(img). You need to convert that manually.
You can easily check if the call succeeds or not by split that call into two lines:
mwArray tmp = mwArray(img);
mwArray out;
ImgProc(1,out,tmp);
Set a breakpoint and take a look at the "value" of tmp. I guess its a NULL pointer.
However after what you discribed the call can't work out. Convert that device-independent bitmap (DIB) format to a mwArray manually.
Yes, you're right. But I tried deleting the first and third lines, leaving
mwArray out;
it still showed an error.
Then you are still doing something wrong. I would suggest starting with an example from the doc. After that try again getting your code working.
I cant access the documentation, I dont have licence. I am using Matlab in school. Anyway, what I did with Matlab is all the code above. If I delete mwArray, it work, but then I cannot use Matlab function.
Or maybe I have to construct mwArray in both input and output, but I dont know how to construct mwArray to store an image as I cannot access the documentation. Someone suggested me using the following constructor:
mwArray(mwSize num_rows, mwSize num_cols, mxClassID mxID, mxComplexitycmplx)
but I dont understand clearly the arguments inside it. I am so confused.
Edit: I found out that maybe I missed to call
mclInitializeApplication(Null,0)
but it failed. I used the code below to compile DLL file in Matlab.
mcc -v -W cpplib:ImgProc -T link:lib ImgProc
If the call to mclInitializeApplication(Null,0) fails then the whole app cant work.
In the case you try to run the code on a machine without MATLAB: do you have installed the MCR?
In the case you are working on the same machine as you compiled the code:
  1. Check if you can run a simple MATLAB Compiler generated exe
  2. Check if the runtime\win** folder is on your environment variable PATH. E.g. for 13b 64bit you should have C:\Program Files\MATLAB\R2013b\runtime\win64 in your PATH environment variable. If not add the needed entry w.r.t to your installation folder.
  3. Use the shipped documentation in school to get the example. Open MATLAB and type
web([docroot '/compiler/c-shared-library-target-1.html'])
I just created the whole project and copied all of code. Now, it returned true. it's so weird.
Can you help me with a conversion from img to mxArray in the function:
mwArray tmp = mwArray(img);
I never have done this. But this is more a C/C++ programmaing question rather then a MATLAB related question.
It seems like its good documented here and here. So you need to transform the DIB into a 3xMxN matrix.
It is kind of hard. So I changed the way to open and edit Image in Matlab. In VC++ program, I passed the path of file to the Matlab function, so it can read it. However, I got crashed again. Here what I did:
in Matlab:
function ImgProc(pathname)
mov=VideoReader(pathname);
% edit image
in C++:
bool a=mclInitializeApplication(NULL,0);
ImgProcInitialize();
mwArray out(sFileName);
ImgProc(out);
ImgProcTerminate();
mclTerminateApplication();
ImgProc() in C++:
ImgProc(const mwArray& pathname);
I set a breakpoint at
mwArray out(sFileName);
and it's fine. So I guess the problem is
ImgProc(out);
It is kind of hard. So I changed the way to open and edit Image in Matlab. In VC++ program, I passed the path of file to the Matlab function, so it can read it. However, I got crashed again. Here what I did:
in Matlab:
function ImgProc(pathname)
mov=VideoReader(pathname);
% edit image
in C++:
bool a=mclInitializeApplication(NULL,0);
ImgProcInitialize();
mwArray out(sFileName);
ImgProc(out);
ImgProcTerminate();
mclTerminateApplication();
ImgProc() in C++:
ImgProc(const mwArray& pathname);
I set a breakpoint at
mwArray out(sFileName);
and it's fine. So I guess the problem is
ImgProc(out);
Does the call to ImgProcInitialize succeed? It also has a return value? What datatype dies sFileName have?
sFileName is CString which contains the video file path.
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("Movie Files (*.avi;*.mpg;*.mp4)|*.avi;*.mpg;*.mp4||"), this);
CString sFileName = dlgFile.GetPathName();
bool a=mclInitializeApplication(NULL,0);
bool b= ImgProcInitialize();
mwArray out(sFileName);
ImgProc(out);
ImgProcTerminate();
mclTerminateApplication();
ImgProcInitialize() returns true which is fine. Im wondering if the lines are true because I dont know how to check value of it:
mwArray out(sFileName);
ImgProc(out);
Edit: The error looks like:
Microsoft C++ exception: mwException at memory location

Sign in to comment.

Asked:

on 22 Oct 2013

Edited:

on 13 Feb 2016

Community Treasure Hunt

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

Start Hunting!