clearing all variables crashes matlab, clearing dll memory?
7 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
James Tursa
2014 年 4 月 16 日
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?
回答 (2 件)
José-Luis
2014 年 4 月 16 日
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.
Tete Tam
2018 年 1 月 18 日
編集済み: Tete Tam
2018 年 1 月 18 日
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.
0 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!