Dear Vince,
thank you very much for the explanation. The C example was very
helpful and illustrative.
The question that I asked arose during my investigation of the
following strange behavior
(simplified for illustration, see trace below ending in error 9999
"assigment of a reference
is not allowed").
Here a new object is created within each iteration of the loop. In the
constructor, certain
references adjustment is done to bind to other objects, here for
example to the object
itself. Then some actions are performed on the object (omitted) and the program
proceeds to the next iteration. The first iteration works fine, but
the program fails in the
constructor section of the object at the time of the second iteration.
If Stata indeed works as illustrated in the C code, why would it break
at the second
iteration?
Currently I observe that explicitly dropping the object before
creating it helps (adding
"capture classutil drop .fb" before calling ".fb=.foobar.new").
The class manual (http://www.stata.com/help.cgi?classman) specifically mentions
(section 4.9):
===QUOTE================================
We rarely need to concern ourselves with objects being removed when they
are deleted or replaced.
When we type
.a = .classname.new
.b = .classname.new
.a = .b
The last command causes the original object .a to be destroyed and
replaces it with .b. The class system handles this task, which is
usually all we want done.
===END OF QUOTE ================================
Which, as I understand means that explicit call to a destructor is not
necessary (here
in the quoted text "destroyed" really means "destructor is called and
memory released",
right?). But I don't think Stata fully follows this behavior. The
reason for this doubt is:
1) if that was true, then adding capture classutil drop .LHS_object
would not change
anything in the program, but it does.
2) Stata calls the constructor of the RHS before calling the
destructor of the LHS (why?).
As this, perhaps, is not directly related I will illustrate it in a
separate thread.
Thank you,
Sergiy Radyakin
below: foobar.class, test.do and trace of execution of test.do in
Stata 11 for Windows.
----- FILE foobar.class BEGINS -------------------------------------------------
class foobar {
class foobar NXT
}
program define .new
.NXT.ref = .ref
end
----- FILE foobar.class ENDS -------------------------------------------------
----- FILE test.do BEGINS -------------------------------------------------
version 11
clear all
forvalues i=1/3 {
display `"> > > `i'"'
.fb=.foobar.new
// ... do something with the .fb object ...
}
----- FILE test.do ENDS -------------------------------------------------
----- TRACE BEGINS --------------------------------------------------
. forvalues i=1/3 {
2. display `"> > > `i'"'
3. .fb=.foobar.new
4. // ... do something with the .fb object ...
. }
- forvalues i=1/3 {
- display `"> > > `i'"'
= display `"> > > 1"'
> > > 1
- .fb=.foobar.new
-class foobar {
- class foobar NXT
-}
---------------------------------------------------------------------------------
begin foobar.new ---
- version 11
- .NXT.ref = .ref
-----------------------------------------------------------------------------------
end foobar.new ---
- }
- display `"> > > `i'"'
= display `"> > > 2"'
> > > 2
- .fb=.foobar.new
---------------------------------------------------------------------------------
begin foobar.new ---
- version 11
- .NXT.ref = .ref
-----------------------------------------------------------------------------------
end foobar.new ---
= assigment of a reference is not allowed
}
r(9999);
end of do-file
r(9999);
----- TRACE ENDS -------------------------------------------------
On Thu, Dec 31, 2009 at 3:17 PM, Austin Nichols <[email protected]> wrote:
> Vince Wiggins <[email protected]> :
>
> I vote for more C analogies, not fewer. Excellent post!
>
> On Thu, Dec 31, 2009 at 3:05 PM, Vince Wiggins, StataCorp
> <[email protected]> wrote:
> <snip>
>> I fear that this reply may lead to a statalist ban on including C source code
>> in postings that do not involve plugins.
>>
>>
>> -- Vince
>> [email protected]
>
> *
> * 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/
>
*
* 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/