MATLAB Answers

Destruction of partially constructed class?

2 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

Show 1 older comment
Chaitanya Jha
Chaitanya Jha on 13 Nov 2019
Hi Adam, thanks for your reply.
I understand your confusion. This was just an example class. The real MainClass that I have is much bigger, the main purpose of the real MainClass is to create a GUI for a software. In the class constructor many functions are clalled and class-objects of different classes are made.
In one of the functions I look for Meta Data file, if this file is not present at a given location then the GUI cannot be created and so I set the property Stop = 1 of the MainClass in that function to destroy main class object and to stop further action in the constructor (which I don't know how to do yet and so I am asking it here). Then I throw an error telling the user that the Meta Data file is missing.
The purpose of Formed property is to indicated that the entire MainClass object is completely formed so that I can start assigning default values to the properties. So, Formed can only be set at the very end of the class constructor.
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.

Sign in to answer this question.