Prevent mlint warning for onCleanup like return value

4 次查看(过去 30 天)
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 个评论
tommsch
tommsch 2024-8-23
What is the worth of that answer. You just repeated my question and all the stuff I know. Are you a ChatGPT bot? You may consider to delete your answer to reduce spam - I will delete my reply then too.
Umar
Umar 2024-8-24

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

https://www.mathworks.com/help/matlab/ref/checkcode.html

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2024-8-23
编辑:Steven Lord 2024-8-24
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 个评论
tommsch
tommsch 2024-8-31
You still did not answer my question. So, lets just close this discussion.
Umar
Umar 2024-9-1
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by