Custom control deleation from inside another custom control

7 views (last 30 days)
Hello all, I am having a slight issue with a warning that is popping up when I am deleting a custom control from inside another custom control.
Running 2022b on Win11.
In App Designer, I made a custom control called uiDesignType which looks like this:
and is used in another control uiDesignBuilder which looks like this (left) and when running and 'Add Design' is pushed a uiDesignType is added for each press (right):
This is how I currently have it set up, in uiDesignType I have an Event-Callback set up with an event called DesignOrderChanged and it's public calleback called DesignOrderChangeFcn.
When 'Add Design' is pressed this fires:
function cmdAddDesignButtonPushed(comp, event)
comp.DesignsLayout.RowHeight{end+1} = 25;
design = uiDesignType(comp.DesignsLayout); % DesignsLayout is a grid lyout
design.DesignOrderChangeFcn=@(src,event)updateOrder(comp,design);
design.UserData = length(comp.Designs) + 1;
comp.Designs(end+1) = design; % add to array of uiDesignBuilder controls
end
When the delete button (trash can) is pressed this fires:
function cmdDeletePushed(comp, event)
comp.DesignOrder = 0;
notify(comp, 'DesignOrderChange');
end
updateOrder function
function updateOrder(comp,design)
sprintf('%s order change: %i', design.DesignType ,design.DesignOrder)
switch(design.DesignOrder)
case -1 % move down
case 1 % move up
case 0 % delete
design.delete; % breakpoint is set here
end
end
Everything runs fine, the control gets deleted when I press the button but I get the warning:
Warning: Error occurred while executing the listener callback for event PostCallbackExecution defined for class
appdesigner.internal.service.AppManagementService:
Invalid or deleted object.
Error in onCleanup/delete (line 23)
obj.task();
Error in
matlab.apps.createCallbackFcn>@(source,event)executeCallback(appdesigner.internal.service.AppManagementService.instance(),obj,callback,requiresEventData,event)
(line 12)
newCallback = @(source, event)executeCallback(appdesigner.internal.service.AppManagementService.instance(), ...
When I debug, I but a breakpoint here design.delete; and as I step through, the delete happens with no issues, comes out of the switch block fine, exits updateOrder fine, and the very next step it jumps to cmdAddDesignButtonPushed at the line:
design.DesignOrderChangeFcn=@(src,event)updateOrder(comp,design);
then into cmdDeletePushed and comes out of the notify function fine then exits out of the cmdDeletePushed function with no issues.
Next step is into this MATLAB code:
function newCallback = createCallbackFcn(obj, callback, requiresEventData)
%CREATECALLBACKFCN Components authored in App Designer assign callbacks to
%uicomponents using this wrapper, which forwards exceptions to App
%Designer for correct alerts in App Designer.
% Copyright 2021, MathWorks Inc.
if nargin == 2
requiresEventData = false;
end
newCallback = @(source, event)executeCallback(appdesigner.internal.service.AppManagementService.instance(), ...
obj, callback, requiresEventData, event);
end
When it tries to fire off the newCallback = @... line, the warning gets thrown.
I have also tried this and I get the same results:
function cmdDeletePushed(comp, event)
delete(comp);
end
Now, like I said, the control delets fine, and I have not noticed any other issues after the warnings, but it doesn't feel right to just leave it like that or worse add warning('off','MATLAB:callback:error') to hide it.
The warning is thrown when I hit 'Play' in App Designer, but if I run uiDB = uiDesignBuilder at the command prompt in MATLAB and then do uiDB.Designs(1).delete I dont get the warning. Designs is a property in uiDesignBuilder that is an array of uiDesignType.
Any thoughts?
  1 Comment
William
William on 24 Jan 2023
So, one work around I found was to set that warning as an error:
warning('error','MATLAB:callback:error')
Then I through my delete call into a try/catch block. Not ideal, but wont hide other callback issues.
What I believe is happening when I delete from the parent control uiBuilder using the Event-Callback of uiDesignType is when the notify function returns, it's trying to return to a deleted control. Also, I guess a control can't delete itself.
If possible I would like to figure out why it is hppening.

Sign in to comment.

Answers (1)

Manoj Mirge
Manoj Mirge on 24 Mar 2023
Hi William,
CreateCallbackFcn is a wrapper in App designer and is used to assign callbacks to UiComponents, which forwards exceptions to App designer for correct alerts in App designer. If you assign callback to component comp1 in your component comp using below syntax.
comp.comp1.Button_pushedFcn = matlab.apps.createCallbackFcn(comp, @comp1Button_pushed2, true);
So, at the time of execution of Button_PushedFcn callback App designer’s createCallbackFcn executes some code before and after execution of your callback and to execute that code it might need your comp object as argument. If you delete your comp in your callabck then you might get the warning you are getting.
In your case it might be occurring in uiDesignType component. It seems like in uiDesignType component you have attached callback to delete button (trash can) using createCallbackFcn. If you have assigned callback using below syntax.
UiDesignType.deleteButton.ButtonPushedFcn= matlab.apps.createCallbackFcn(UiDesignType, @cmdDeletePushed, true);
In that case if you press delete button then your UiDesignType object will get deleted from uiDesignBuilder’s updateOrder function but createCallbackFcn will need to execute some code after execution of cmdDeletePushed function and for executing that code it might need UiDesignType object and since it is deleted by that time the code which needs to be executed after callback execution might generate error you are getting.
One workaround for your issue can be:
You can assign callback to delete button (trash can) without using matlab.apps.createCallbackFcn. You can assign callback to delete button in postSetupFcn function of uiDesignType component.
Below I have attached code for above explained solution.
% In your UiDesignType’s postSetupFcn you can assign callback to delete button.
function postSetupFcn(comp)
Comp.DeleteButton.ButtonPushedFcn=@comp.cmdDeletePushed;
end
% In cmdDeletePushed function you will need to change arguments because now your function will receive three arguments.
function cmdDeletePushed(comp, src,event)
comp.DesignOrder = 0;
notify(comp, 'DesignOrderChange');
end
Hope this helps.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!