808 Chapter 18 Object-Oriented Infrastructure
Node* pkNode = new Node;
MyFunction(pkNode);
// pkNode now points to invalid memory
On allocation pkNode has zero references. The call to MyFunction creates an instance
of a
NodePtr on the stack via the copy constructor for that class. That call increments
the reference count of
pkNode to one. On return from the function, the instance of
NodePtr is destroyed and, in the process, pkNode has zero references and it too is
destroyed. However, the following code is safe:
Node* pkNode = new Node; // pkNode references = 0
NodePtr spkNode = pkNode; // pkNode references = 1;
MyFunction(spkNode); // pkNode references increase to 2,
// then decrease to 1
// pkNode references=1atthis point
A related problem is
NodePtr MyFunction ...