Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Constructor/destructor sequence in Stata classes


From   Sergiy Radyakin <[email protected]>
To   [email protected]
Subject   st: Constructor/destructor sequence in Stata classes
Date   Fri, 1 Jan 2010 19:23:11 -0500

Dear All,

suppose I do an assignment

.a = .class_a.new
.a = .class_a.new

Here an instance of the class class_a is created and assigned to the object .a.
Then the action is repeated, causing the first instance to be
automatically destroyed by
the Stata class managing system. This is perfectly fine. I would like
to point at the
sequence of events, as countrary to the expectation, the second
instance of class_a is
created before the first instance is disposed of.

To illustrate the point, let's create a custom class:

=== BEGIN OF FILE seq.class =========================================
class seq {
	string object_name
}

program .new
	display `"CONSTRUCTOR IS CALLED `.uname' [`0']"'
	.object_name=`"`0'"'
end

program .destructor
	display `"DESTRUCTOR IS CALLED `.uname' [`.object_name']"'
end

=== END OF FILE seq.class ===========================================

and now try:
.a=.seq.new first
.a=.seq.new second

Here is what we get:

. .a=.seq.new first
CONSTRUCTOR IS CALLED K3db23e8 [first]

. .a=.seq.new second
CONSTRUCTOR IS CALLED K3db2488 [second]
DESTRUCTOR IS CALLED K3db23e8 [first]

Here the constructor and destructor report on which instances they are
being executed (uname)
and constructor code receives a friendly name as a parameter, so that
we know which instance
is first and which one is second.

Suppose the class is doing some heavy-lifting and allocates some
scarce resources for it's work.
Such sequencing causes a double load on the system because in-between
the moment the
constructor of the second instance is called and the destructor of the
first instance is called, both
instances will co-exist in memory simultaneously.

In my opinion rescheduling the sequence to call the destructor of the
LHS first is a better approach.

One reason why rescheduling may not be desirable is the possibility of
the constructor of the second
instance to fail. In the current implementation of Stata, should that
occur, Stata will automatically call
the destructor of the instance for which the constructor has failed
(second instance, RHS) and keep
the first instance (LHS) intact (instead of having a null pointer, or
no object at all).

Is there any way to control this behavior?

Is there a detailed explanation of how errors are handled by Stata in
constructors/destructors of classes?
In particular, if I have a hierarchical class, e.g.:
class rectangular {
    class point A
    class point B
    class point C
    class point D
}
and error occurs in the destructor of instance (say .r) of
rectangular, would destructors of .r.A, .r.B, .r.cC,
and .r.D be called? (the answer seems to be "yes", but want to be sure).


Thank you, Sergiy Radyakin
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index