Destruction of partially constructed class?

4 views (last 30 days)
Chaitanya Jha
Chaitanya Jha on 13 Nov 2019
Commented: Adam on 14 Nov 2019
Hi, I want to delete a partially constructed class in case of an error. For example, the class which I want to destroy is:
classdef MainClass < handle
properties (AbortSet,SetObservable,GetObservable)
Stop = 0;
Formed = 0;
end
methods
function obj = MainClass()
addlistener(obj,'Stop','PostSet',@MainClass.Stop_Li);
obj = StopFcn(obj);
obj.Formed = 1;
end
end
methods (Static)
function Stop_Li(src,e)
obj = e.AffectedObject;
if obj.Stop == 1
delete(obj);
end
end
end
end
The StopFcn function is:
function S = StopFcn(S)
S.Stop = 1;
end
When I run the above code, this gives me an error because it destroys MainClass object before it is fully constructed, giving an error:
Invalid or deleted object.
Error in MainClass (line 10)
obj.Formed = 1;
How can I make this error go and delete a partially completed class without producing any errors? I tried adding a handle class destructor method but it doesn't help.
  4 Comments
Adam
Adam on 13 Nov 2019
If you know the name of the Meta Data file at the time you launch the GUI, i.e. create the class, then I would suggest launching the GUI from a wrapper function instead. Check the file exists in the wrapper function and don't even start to create the GUI if it doesn't. I have used this approach for a few things, including copying files to where they need to be, opening the parallel pool, or other things that I want to happen before I actually start the process of launching a GUI.
Chaitanya Jha
Chaitanya Jha on 13 Nov 2019
Thanks, I am also using a wrapper function I will check for the meta data file there.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 13 Nov 2019
In this case you probably want to define a destructor (delete) method that supports destruction of partially constructed objects and just let the class constructor error if the the file is not present where the class expects it to be. Process the properties in the destructor in the same order as they're processed in the constructor and you may be able to leave the destructor as soon as you reach a property that hasn't been initialized yet.
  2 Comments
Chaitanya Jha
Chaitanya Jha on 13 Nov 2019
Sounds good if letting the class constructor error is not a bad programming habit, to me it seems it is. Is there any negative effects that it can have if I let the class constructor error? I am going to construct an exe file for my software to be installed on multiple systems, that's why I want to make it as much error free as possible.
Adam
Adam on 14 Nov 2019
It's not ideal in that you are left with a handle to a deleted object if your class quietly deletes itself during construction, so then theoretically any time you use an object of this class you would need to check after creation that it does actually exist before you start calling functions on it, etc.
If it is a very specific class, as it sounds, where you will probably only create it in one place, and in a wrapper class that can gracefully handle the deleted object, it isn't too bad I suppose.
If it were a commonly used class then it would not be good at all to have to expect any code that creates one of these objects to have to deal with the possibility of a deleted object being created.

Sign in to comment.

Categories

Find more on Construct and Work with Object Arrays in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!