mxCreateDoubleMatrix returns NULL when called from C program on a system using MCR

Hi,
mxCreateDoubleMatrix returns NULL when called from a C/C++ console program? My system setup as such:
Win 7
Matlab 4.16
MCR 1.16
C shared library compiled by MATLAB Compiler 4.16 (R2011b)
I've linked the mclmcrrt.lib and <myCompiled m file>.lib and dll--not sure if more are required?
Thanks for your input.

Answers (2)

What arguments do you pass into mxCreateDoubleMatrix? If your applications compiles and links property, and doesn't have other run-time errors, I wouldn't worry about the .lib and .dll files. On the other hand mxCreateDoubleMatrix allocates memory on the heap, so one situation where it may return NULL is when your application is out of memory. You are trying to create a very large matrix perhaps? I would recommend try to use "new" to allocate the same amount of memory and see if it throws the bad_alloc exception.

4 Comments

Thanks for the quick response. The relevant code for mxCreateDeoubleMatrix is:
mxArray* t;
double myArray[5]={10.2, 3, 6.3, 5.4, 5.9};
double *myArrayPtr;
t = mxCreateDoubleMatrix(1, 5, mxREAL); //returns NULL
myArrayPtr = (double*)mxGetPr(t);
memcpy(myArrayPtr, myArray, 5*sizeof(double)); //Crashes here
The arrays are small so I think we're okay with the heap. Another interesting issue is when I uncomment
the calls to the function which is exposed by the m-file dll. Here, the program also crashes. Although the the mentioned libs link upon building the application, it may be that the MCR and m-file dll's are not loading at runtime since the mxCreateDoubleMatriix and m file functions are having issues?
Thank you for help; I look forward to your reply.
If t is NULL then you shouldn't do any downstream processing using it. E.g., mxGetPr(t) should not be called if t is NULL, etc.
Yes, I'm not really doing any error checking at this point; this is just a test program to establish connection with the MCR and my compiled m-file. This is very new to me but I have feeling that the issue lies in the application not being able to connect to the MCR and m-file dll's. When I run the debugger without any break points, the debugger points to the gs_support.c file on the line:
#else /* defined (_WIN64) */
cookie = systime.ft_struct.dwLowDateTime; //Points to this line
cookie ^= systime.ft_struct.dwHighDateTime;
#endif /* defined (_WIN64) */
My c program, Matlab, and MCR are 32 bit? I'm really not sure what is causing any 64 bit issue? I would greatly appreciate any insight--Thanks.

Sign in to comment.

Try to call mclInitializeApplication(NULL,0); at the beginning of your app.

1 Comment

So I added mclInitializeApplication(NULL,0); as the first function in main and the gs_support error still exists. I've narrowed the issue to the m-file which I compiled to a dll is not linking at run time. Here are some details:
1. The compiler chose at the Matlab R2011b-32bit commandline was Lcc-win32 C 2.4.1; however, the corresponding h and c files generated with the dll and lib have metadata stating that this is a MATLAB Compiler: 4.16 (R2011b). Looking through the header file, I notice LCC compiler switches. The reason I bring this up is that the literature states the the MCR and the Matlab compiler have to coordinate. In my case the MCR is 7.16 and the compiler should be 4.16.
2. Once this m-file based dll is built, I place it in the working directory and run the program. The program immediately crashes; the call stack indicates that the main function was not reached.
3. The debugger points to the line in gc_support.c:
#else /* defined (_WIN64) */
cookie = systime.ft_struct.dwLowDateTime;
cookie ^= systime.ft_struct.dwHighDateTime;
#endif /* defined (_WIN64) */
So my thoughts on this is that the Matlab compiler is not the correct one? Or, Microsoft VC 2010 is not set up correctly for linking in this m-file based dll?
Here is my general setup for VC 2110 project; hopefully this is helpful:
1. System path has MCR directory before Matlab.
2. Statically link in mclmcrrt.lib, and libut.lib from root\R2011b\extern\lib\win32\microsoft\ and LinearFitModelFnLib.lib (created from m-file) located in the working directory.
3. LinearFitModelFnLib.dll (created from m-file) located in working directory
3. All programs are 32 bit.
I hope this helps; please let me know if you need any more information.

Sign in to comment.

Categories

Asked:

KME
on 24 May 2012

Community Treasure Hunt

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

Start Hunting!