Matlab crashes when manipulating an array generated by a mex function
Mostrar comentarios más antiguos
Dear all,
I have written a Mex-function that generates an array. Actually, it is a topological sort algorithm. Now let's say I call this function
A = tsort(B); % contains a call to the mex function
A = otherfunction(A); % other function that modifies A contains only matlab code
then it happens quite often that Matlab crashes when calling otherfunction, even after minutes between my call to tsort and otherfunction. Did anyone experience such problems with his or her code. By the way, I am using Windows 7 64bit, Matlab 2012a and Microsoft Software Development Kit (SDK) 7.1 as compiler.
Thanks for your help, Anon
8 comentarios
Walter Roberson
el 22 de En. de 2013
We will probably need to see your tsort() code.
There are some common errors that people tend to make in mex code that show up the next time that MATLAB tries to allocate or deallocate memory, such as if another function is called.
Anon
el 22 de En. de 2013
Anon
el 23 de En. de 2013
James Tursa
el 23 de En. de 2013
Probably several people have looked at this post and had the same reaction that I had. Basically, it is not obvious from looking at the code where the indexes used would become invalid. So I would suggest putting in a lot of checks in the code to detect a problem and print it out. Everywhere you index into an array, put a check. E.g., check to see that SN, ix, ixc, and MV are not NULL. Check to see that IXN is not too large just before you do SN[IXN]. Check to see that pix1 is not too large just before before you do ixc[pix1] and ix[pix1]. Etc. My guess is that eventually you will get a message to the screen telling you where the problem is.
Anon
el 24 de En. de 2013
Just an idea to check. Your mexFunction gives back Data of type
mxUINT32_CLASS
Matlab use double as standard, as far as I know. I do convert integers, which should be given back to matlab from my mexFunction, to doubles before. Otherwise you have to work with int32() in matlab. This is just an idea...
Anon
el 24 de En. de 2013
James Tursa
el 24 de En. de 2013
@anon: The mex function is likely corrupting memory but that corruption isn't detected until later on after the mex routine has returned control back to MATLAB. E.g., the mex routine might have corrupted another variable in the workspace that has nothing to do with the mex routine, and it isn't until you get back to MATLAB and do downstream stuff that the error has an effect and crashes MATLAB. If you are unfamiliar with debugging then I suggest you follow my advice and put in lots of checks all over your code to detect when you are out-of-bounds or dereferencing a NULL pointer etc.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!