Destruction of partially constructed class?

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 comentarios

It's really not obvious why you would want to do this, since all you will end up with is a handle to a deleted object in whatever variable you were assigning the object to, which would presumably still have to be handle in some way or other, just the same as if your class still existed with a boolean saying it is invalid or somesuch.
What is the purpose of this class, as from what I can see it would always destroy itself after partial creation as there is no path in which Stop would remain false?
You could just re-order the lines in the constructor to:
function obj = MainClass()
obj.Formed = 1;
addlistener(obj,'Stop','PostSet',@MainClass.Stop_Li);
obj = StopFcn(obj);
end
to remove the specific error you are encountering, but it still wouldn't make any sense to me.
Chaitanya Jha
Chaitanya Jha el 13 de Nov. de 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 el 13 de Nov. de 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 el 13 de Nov. de 2019
Thanks, I am also using a wrapper function I will check for the meta data file there.

Iniciar sesión para comentar.

Respuestas (1)

Steven Lord
Steven Lord el 13 de Nov. de 2019

0 votos

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 comentarios

Chaitanya Jha
Chaitanya Jha el 13 de Nov. de 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 el 14 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Class Introspection and Metadata en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Preguntada:

el 13 de Nov. de 2019

Comentada:

el 14 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by