app object not being passed to App GUI function, intermittently

56 visualizzazioni (ultimi 30 giorni)
Corey
Corey il 10 Set 2025 alle 14:35
Risposto: dpb circa 13 ore fa
In my App code, I am creating a timer object with a callback function.
The timer looks something like this:
app.tenMinuteTimer = timer('ExecutionMode', 'fixedRate', 'Period', 600, 'TimerFcn', @(~,~)app.createNewLogFile())
start(app.tenMinuteTimer);
The callback looks something like this:
function createNewLogFile(app)
try
% Explicit null checks
if isempty(app) || ~isvalid(app)
error('WARNING: App object is invalid in createNewLogFile context');
end
...
I am seeing the error message intermittently with this code, but only intermittently (about ~5% of the time over long durations of running the App). I would really like to understand what could be causing this to happen intermittently, how to confirm a root cause, and/or how to do this in a more robust way altogether.
I believe that memory issues can be ruled out, or any other issues relating to long-duration testing. I can observe this problem within a relatively short test. I suspect it may have to do with task scheduling on the host running the App, since the App is doing multiple things.
Heavy apologies for editing this post multiple times.. I will not change it again from now.
  4 Commenti
Steven Lord
Steven Lord il 29 Set 2025 alle 13:20
I am seeing the error message
Please show us all the text displayed in red in the Command Window (and if there are any warning messages displayed in orange, please show us those too.) The exact text may be useful and/or necessary to determine what's going on and how to avoid the warning and/or error.
Corey
Corey 4 minuti fa
Hi Steven, when I said that I see the error message, I meant the message that I created in the try/catch code that I wrote and you see above in the post. "error('WARNING: App object is invalid in createNewLogFile context');"
The code does not fail otherwise - the app continues running and this timer callback fails silently unless I put in that null check myself.

Accedi per commentare.

Risposte (2)

Vedant Shah
Vedant Shah il 26 Set 2025 alle 12:05
Hi @Corey,
An intermittent error occurs in MATLAB App Designer app because a timer is set to execute a callback createNewLogFile every 10 minutes. Intermittently, when the timer fires, the app object is either in the process of closing or has already been deleted, causing the callback to detect that the app object is invalid and it throws an error.
This happens because timers in MATLAB run independently from the main app thread and can attempt to execute their callbacks even as the app is shutting down. This issue is a classic race condition between the timer's execution and the app's lifecycle.
The solution is to ensure that you always stop and delete your timer when the app is closing. To achieve this, add the below code to your existing app designer code snippet:
function UIFigureCloseRequest(app, event)
try
if isprop(app, 'tenMinuteTimer') && isa(app.tenMinuteTimer, 'timer') && isvalid(app.tenMinuteTimer)
stop(app.tenMinuteTimer);
delete(app.tenMinuteTimer);
end
catch
disp("Error Occurred!!");
end
delete(app)
end
By doing so, the timer is prevented from running after the app has been closed, which eliminates the possibility of the callback trying to access an invalid app object.
For more information, refer to the following documentation links:
  1 Commento
Corey
Corey il 29 Set 2025 alle 13:13
Hi, thank you for your response but this is not the correct answer. I have tested this thoroughly and run the app timer with a fresh MATLAB instance, fresh app instance, not closing the app, etc. And the callback still fails.

Accedi per commentare.


dpb
dpb circa 13 ore fa
app.tenMinuteTimer = timer('ExecutionMode', 'fixedRate', 'Period', 600, 'TimerFcn', @(~,~)app.createNewLogFile())
function createNewLogFile(app)
try
% Explicit null checks
if isempty(app) || ~isvalid(app)
error('WARNING: App object is invalid in createNewLogFile context');
end
...
Your prototype function has no arguments in the line that assigns the callback function, but the function itself expects an argument. Those two are out of whack with each other.
I suspect if you change to
app.tenMinuteTimer=timer('ExecutionMode','fixedRate','Period', 600,'TimerFcn', @app.createNewLogFile);
function createNewLogFile(app,~)
...
the symptoms will disappear. I've never tried to use the tilde syntax in the assignment, so not sure about the syntax there for just the one argument, maybe
app.tenMinuteTimer=timer('ExecutionMode','fixedRate','Period', 600,'TimerFcn', @(x,~)app.createNewLogFile);
maybe?

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by