Hi Alex,
Ø my opinion, the
init() called by the gdd constructors shouldn't place the reference count to 1
(it should be initialized instead to zero)
Hopefully I can explain this situation with some historical background.
Originally, when Jim Kowalkoski wrote gdd he set the reference count to one in
the gdd constructor. It was intended by Jim to be used like this.
Gdd * pDD0 = new gdd;
Gdd * pDD1 = pDD0;
pDD0->unreferenced ();
…
pDD1->unreference ();
After using this for awhile one realizes that manual maintenance
of the reference count is very error prone, and that a smart pointer is a
better way. We still have to un-reference the gdd once when installing it into
the smart pointer, but after that there are no worries.
// be careful to unreference here
Gdd * pDD0 = new gdd;
smartGDDPointer pDD1 (pDD0);
pDD0->unreferenced ();
// from here on down the smart pointer maintains the reference
count
smartGDDPointer pDD2 = pDD1;
smartGDDPointer pDD3 = pDD2;
pDD2 = pDD3;
We have to be careful about changing the original behavior of
GDD because there is legacy code. Changing behavior, so that the reference
count is initialized to zero, and not one, when the gdd is created will break
lots of preexisting code in the worst kind of way where problems might be
discovered by end users at runtime. I don’t mind changing the code that I
maintain, but there is quite a bit of application level code that is maintained
in the community.
Some options
------------------
1)
One option would be to remove reference, and unreference from
GDD’s public interface. These might be replaced by a factory that returns
a smart pointer. This would not be backwards compatible, but at least we would
feel confident that legacy code would not compile until it is adjusted. We
prefer compile time bugs to runtime bugs of course. Probably the only downside is
an unexpected cost for upgrading users who find that they have to recode when
they get a new version.
2)
Do nothing. The current situation isn’t perfect but we
would prefer to use the dataAccess library alternative in the future so perhaps
we should leave GDD unmodified.
What do you think? I am also open to alternative suggestions
that you might have?
I added this comment to smartGDDPointer.h
//
//
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !! WARNING WARNING WARNING WARNING
WARNING WARNING WARNING !!
//
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// // be careful to manually unreference
here
// // (the reference count is
initialized to one by the gdd ctor)
// Gdd * pDD0 = new gdd;
// smartGDDPointer pDD1 (pDD0);
// pDD0->unreference ();
//
// // from here on down the smart
pointer maintains the ref count
// smartGDDPointer pDD2 = pDD1;
// smartGDDPointer pDD3 = pDD2;
// pDD2 = pDD3;
//
PS: In R3.14.11 there are several fixes coming for various issues
in the PCAS library. The details are in the mantis bug tracking system.
Jeff
______________________________________________________
Jeffrey O. Hill
Email [email protected]
LANL
MS
H820
Voice 505 665 1831
Los Alamos NM 87545 USA
FAX 505 665 5107
Message content: TSPA
Hey Jeff,
I think the GDD
smart pointer is a little bit weird, in a common way to use smart pointers as
follows:
{
smartGDDPointer sGDDPtr=new gdd();
}
the newly
allocated gdd object isn't freed when sGDDPtr is out of scope. If you want to
use smartGDDPointer as a normal smart pointer, you must place a line code
immediately below the new operator, like this:
{
smartGDDPointer sGDDPtr=new gdd();
//The reference count of the newly created gdd object is actually 2 at this
time.
//The gdd() places the reference count to 1, and the assignment operator
increase it to 2.
sGDDPtr->unreference();
}
We see that
smartGDDPointer is the intrusive type of smart pointer, the gdd has one
reference count member and two functions reference() and unreference(). In my
opinion, the init() called by the gdd constructors shouldn't place the
reference count to 1, the reference counting shouldn't be carried out by gdd
itself. smartGDDPointer is the manager of the gdd object and the only guy who
can operate the reference() and unreference() on gdd object
Thanks,
Alex