Destruction of partially constructed class?

조회 수: 4 (최근 30일)
Chaitanya Jha
Chaitanya Jha 2019년 11월 13일
댓글: Adam 2019년 11월 14일
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
Adam
Adam 2019년 11월 13일
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 2019년 11월 13일
Thanks, I am also using a wrapper function I will check for the meta data file there.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2019년 11월 13일
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
Chaitanya Jha
Chaitanya Jha 2019년 11월 13일
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 2019년 11월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by