clearing all variables crashes matlab, clearing dll memory?

13 vues (au cours des 30 derniers jours)
Jim Hokanson
Jim Hokanson le 16 Avr 2014
Modifié(e) : Tete Tam le 18 Jan 2018
I am working with a 3rd party dll via a mex interface. The dll opens files and returns a handle to the open file. The file handle is a pointer whose value I pass out to Matlab. When making a dll call, I pass the pointer value back into the mex file, which gets cast to being of the file handle type, and then gets used by the dll.
There also exists a close method for the file. In Matlab the pointer value gets placed in a class (pointer object) with a delete method. When the pointer object is deleted, the call is made to the dll (via the mex interface) to close the file.
Things seem to work fine unless I run "clear all" and then Matlab crashes.
Some additional notes:
  • Multiple objects hold the pointer object as a property
  • There is only one pointer object which holds the pointer value
  • The root object, which holds other objects, does not hold the pointer object as a property
  • Deleting the root (when no other variables are present in the workspace), which should delete the other objects, does not cause the system to crash
  • If I recompile the mex file, then clear the root object, the system crashes
These last few points seem to suggest to me that the crash is coming from Matlab somehow clearing the dll memory, and then me passing in a now obsolete file handle pointer. Does that make any sense? If this is the case, how would I work around it?
Thanks, Jim
  2 commentaires
James Tursa
James Tursa le 16 Avr 2014
Tough to answer without seeing the code. Do you have multiple files open simultaneously, or only one file open at a time? Do you have a mexAtExit function defined in your mex routine? Do you keep track of valid file pointers inside your mex routine, or are you completely dependent on these values being passed in from the MATLAB object?
Jim Hokanson
Jim Hokanson le 16 Avr 2014
Modifié(e) : Jim Hokanson le 16 Avr 2014
All the code is at: https://github.com/JimHokanson/adinstruments_sdk_matlab
I am currently only opening one file at a time although I would imagine wanting to be able to open more than one at a time.
I do not have a mexAtExit function defined.
I do not keep track of valid pointers inside the mex routine, they are only in the pointer objects.
Thanks, Jim

Connectez-vous pour commenter.

Réponses (2)

José-Luis
José-Luis le 16 Avr 2014
clear all pretty much kills everything (including whatever functions you might have loaded). You could try clearvars if you want to free up some space instead.
  1 commentaire
Jim Hokanson
Jim Hokanson le 16 Avr 2014
Thanks, but the goal is not just to free memory but rather to understand and prevent the program from crashing.

Connectez-vous pour commenter.


Tete Tam
Tete Tam le 18 Jan 2018
Modifié(e) : Tete Tam le 18 Jan 2018
Hi,
I also run into similar problems lately. I don't know if my cause is the same as OP, but in my case, the error is caused by freeing the same resource twice. When MATLAB calls 'clear all', it will call the destructor of the object. If the file handle has been freed already (e.g. in a close() function), then the destructor may attempt to free the resource again and lead to error.
The external DLL functions may do more than closing the file when it tries to free the resource (e.g. doing an integrity check of the file), so freeing it twice may lead to problems.
The solution is to double check that the resource is not already freed before proceeding.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by