Prevent mlint warning for onCleanup like return value
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I wrote a function, similar to onCleanup. I noticed that Matlab does not give a mlint warning for the following code.
dummy = onCleanup( @() some_func );
But for my own function
dummy = MyOwnCleanup( @() some_func );
I get the warning Value assigned to variable might be unused, which I need to silence with %#ok<NASGU>. Obviously, Matlab recognizes the onCleanup call and does not emit a warning. How can I acchieve similar behaviour for my own MyOwnCleanup function?
4 Commenti
Umar
il 24 Ago 2024
Hi @ tommsch,
You mentioned,” You just repeated my question and *all the stuff I know* “
Now, in your posted comments, you mentioned,
“I wrote a function, similar to onCleanup. I noticed that Matlab does not give a mlint warning for the following code. dummy = onCleanup( @() some_func );”
Could you please click the mathworks mlint documentation link below and tell me what does it say about mlint
Risposta accettata
Steven Lord
il 23 Ago 2024
Modificato: Steven Lord
il 24 Ago 2024
So I wrote a cleanup function which accepts parameters, so you can write
cleandir = onCleanup_lazy( @(x) cd( x ), pwd );
Be careful with how you implement this if some of the parameters are state-dependent. What you've written here is fine, as pwd will be evaluated when onCleanup_lazy is called and the value returned by that function is what would be captured. I assume onCleanup_lazy is implemented something like:
function oc = onCleanup_lazy(fh, varargin)
fh2 = @() fh(varargin{:});
oc = onCleanup(fh2);
end
But consider if you'd written your onCleanup_lazy call slightly differently.
cleandir = onCleanup_lazy( @() cd(pwd));
This wouldn't do what you wanted, as pwd would only get called when the onCleanup object returned by onCleanup_lazy was being destroyed. This is likely well after you changed away from the directory that was pwd when you called onCleanup_lazy, and so that onCleanup object would have done nothing.
Getting back to your original question, I believe onCleanup is handled specially by Code Analyzer. Code Analyzer "knows" (aka we told Code Analyzer) that onCleanup objects generally aren't going to be interacted with in code after they were created. [Occasionally, if you want to control exactly when their tasks are executed, you may want to delete them, but that's about it.] Therefore we exempted it from the "This variable may be unused" check. I don't believe there is a way for users to instruct Code Analyzer that it should exempt their objects from this check as well. You could contact Technical Support and request a way to specify in the definition of a function or class that uses/instances of this function or class should not issue this Code Analyzer warning.
5 Commenti
Umar
il 1 Set 2024
Hi @tommsch,
I do admit not answering your question directly and I am aware of inheriting from handle does allow for automatic resource management which can introduce overhead due to object reference counting. But exchanging ideas back and forth and finding out mlint is not recommended by mathworks documentation will help others when they will try to figure out the same problem as you encountered now in the future. End of discussion.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!