Re: Problems with mex file (allocating memory? )
- From: "James Tursa" <aclassyguywithaknotac@xxxxxxxxxxx>
- Date: Sun, 7 Jun 2009 21:11:01 +0000 (UTC)
"Dominic " <dom_awf@xxxxxxxxxxx> wrote in message <h0g5ne$h7q$1@xxxxxxxxxxxxxxxxxx>...
ok, I found out how to debug and he code crashs in this line
lineData = mxMalloc(*Records/500*sizeof(*lineData));
Because you haven't set the value of *Records yet. You are putting the cart before the horse. Here is a section of your original code you posted:
mwSize dims[2] = {1, *maxNoOfPhotons };
const char *fieldNames[] = {"chan","dtime","truetime","x","line","frame"};
// preallocate lineData array of structures
Data *lineData;
lineData = mxMalloc(*Records/500*sizeof(*lineData));
// passed parameters from matlab
fidIn = (FILE*)mxGetPr(prhs[0]);
Records = (int *)mxGetPr(prhs[1]);
syncperiod = (double*)mxGetPr(prhs[2]);
maxNoOfPhotons = (int*)mxGetPr(prhs[3]);
Resolution = (double*)mxGetPr(prhs[4]);
The first line above has the same problem. You are trying to dereference the maxNoOfPhotons variable before you have set it. So dims[1] will contain garbage, assuming that the dereferencing itself did not bomb the program. Same problem with your *Records dereferencing in the mxMalloc call above. You are using Records before it is set to anything.
2nd problem is that your use of prhs[1] and prhs[3] assumes that they are int32 class on the MATLAB side. I would suggest that you make things easier on yourself and use mxGetScalar generously here. e.g.,
change this:
int *Records;
int *maxNoOfPhotons;
double *syncperiod;
double *Resolution;
to this (i.e., don't use pointers):
int Records;
int maxNoOfPhotons;
double syncperiod;
double Resolution;
and then change these lines:
Records = (int *)mxGetPr(prhs[1]);
syncperiod = (double*)mxGetPr(prhs[2]);
maxNoOfPhotons = (int*)mxGetPr(prhs[3]);
Resolution = (double*)mxGetPr(prhs[4]);
to these lines:
Records = mxGetScalar(prhs[1]);
syncperiod = mxGetScalar(prhs[2]);
maxNoOfPhotons = mxGetScalar(prhs[3]);
Resolution = mxGetScalar(prhs[4]);
Then for all of the downstream stuff get rid of the dereferencing operator for these variables and just use them directly. e.g.,
change this line:
mwSize dims[2] = {1, *maxNoOfPhotons };
to this line:
mwSize dims[2] = {1, 0 }; // the 0 is an arbitrary placeholder number
and then right after getting the value of maxNoOfPhotons, do this line:
dims[1] = maxNoOfPhotons; // Note that you don't use *maxNoOfPhotons
That way you will have the dims array set properly for downstream use. Also, be sure to move this line:
lineData = mxMalloc(*Records/500*sizeof(*lineData));
to a point *after* you have set the value of Records, and then don't use the dereferencing operator because Records is no longer a pointer. e.g.,
lineData = mxMalloc(Records/500*sizeof(*lineData));
Just glancing through your code I also notice this:
if (result!= sizeof(Record))
{
mexPrintf("\nUnexpected end of input file!");
break;
}
Did you mean this?
if (result!= sizeof(Record))
{
mexErrMsgTxt("\nUnexpected end of input file!");
}
i.e., it you got an error or EOF trying to read your data, do you really want to continue the routine and use the invalid data? Or should you just return to MATLAB at that point with an error message?
James Tursa
.
- References:
- Problems with mex file (allocating memory? )
- From: Dominic
- Re: Problems with mex file (allocating memory? )
- From: James Tursa
- Re: Problems with mex file (allocating memory? )
- From: Dominic
- Re: Problems with mex file (allocating memory? )
- From: Dominic
- Problems with mex file (allocating memory? )
- Prev by Date: Passing a variable between 2 functions
- Next by Date: Re: Need help with fsolve for use in M file
- Previous by thread: Re: Problems with mex file (allocating memory? )
- Next by thread: Re: Problems with mex file (allocating memory? )
- Index(es):
Relevant Pages
|
Loading