3.Smart Pointer

ProudNet includes Smart Pointer class.

Smart Pointer is a functionality that guarantees the existence of object as long as its variables that refer to the created objects exist. And if they cease to exist, then the object simply gets destroyed. Smart Pointer also solves the issues of referring to already destroyed objects (dangling) due to a bug made by developer or not destroying the objects (leak) at all.

The object reference counter of Smart Pointer increases by one at each copy of variable. In the below diagram, Object Instance exists until there is no more Smart Pointer variable or the reference counter becomes 0.

그림 3-1Relationship between Smart Pointer and Object

ProudNet's Smart Pointer is Proud.RefCount.

Here is the practical usage of Smart Pointer.

class A {...};

void Foo()
{
    // A object is created.
    Proud::RefCount<A> a(new A);
    
    // variable b shares with variable a. A's counter becomes 2.
    Proud::RefCount<A> b = a;
    
    // release variable a. But it doesn't get destroyed since the counter is still 1.
    a = Proud::RefCount<A>();
    
    // release variable b. There is no variable referring A, thus A gets destroyed (delete gets called.)
    b = Proud::RefCount<A>();
}

There would cases where you simply want to destroy object when there are Smart Pointers that refer to object in several locations. For instance, if you have an object containing open file handle and Smart Pointer is referring to it, then you may want to destroy that object. But if Smart Pointers here and there are referring to the object, you could run into a trouble since you don't explicitly know when the object needs to be destroyed.

In the case like this, you can do 3.1 Dispose Pattern Through 3.1 Dispose Pattern, you can explicitly destroy the object being referred by several Smart Pointers.

3.1Dispose Pattern

Dispose Pattern helps explicitly destroying an object when you don't know exactly know when it needs to be destroyed due to several Smart Pointers referring to that object.

If you want to apply Dispose Pattern to an object managed by smart class, you need to add ‘Status' as member variable of object. ‘Status' should mean the object is in useless state since it already has been destroyed. If ‘Status' is in ‘already destroyed state', then it should prompt an error if anything tries referring to it or let it perform properly if otherwise.

Here is the sample sourcecode for Dispose Pattern.

class A
{
    // When it is true, it means this object is in disposed state.
    bool m_disposed;
    public:
    
    A()
    {
        // The newly generated object is not in disposed state.
        m_disposed = false;
    }

    ~A()
    {
        Dispose();
    }

    void Dispose()
    {
        if(m_disposed == false)
        {
            // Perform operation related to the destroyed object.
            // For example, close all file handles included in that objects.            ...
        
            m_disposed = true;
        }
    }
};

typedef Proud::RefCount<A> APtr;

void Foo()
{
    APtr a(new A);     // Create an object
    APtr b = a;     // Two Smart Pointer variables share the same object.
    
    a->Dispose();   // Forcibly destroy the object.
    
    // Now both a and b objects are in disposed state.
    // So no approach should be made to the object that a and b are referring to.    ...
}

For your note, Dispose Pattern is being used in Java, C#, or other programming lanugages that have Smart Pointer and Garbage Collector.