Prevent mlint warning for onCleanup like return value

조회 수: 4 (최근 30일)
tommsch
tommsch 2024년 7월 2일
댓글: Umar 2024년 9월 1일
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