Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

Copying Objects

Since simple object instances are immutable, there should be no need to create a copy of a simple object, ever. However, the way reference counting is done for simple objects may sometimes cause problems in multi-threaded applications.

Consider sobj_env::so_vref method in a multi-threaded program. The method returns a simple object with a flat reference counter. This means that the caller must either retain a reference to the object or drop the object. The reference counter of the object returned by the sobj_env::so_vref method may be zero, but does not have to. The advantage of these so called ``flat references'' is that it is much easier to check if reference counter operations are matched propperly. The SOBJ_RETAIN() operation is typically on the same logical level as the SOBJ_RELEASE() operation.

In a multi-threaded environment returning flat references may be problematic. The object returned might be destroyed before the caller has a chance to retain the object. This problem could be avoided by having the problematic functions and methods return new references instead of flat references. However, instead of screwing up the entire API and letting some functions return new references while other functions return flat references, we'll solve the problem locally -- by creating a copy of the problematic objects.

A copy of a simple object can be created using the function sobj_copy(). The copied object is completely eqivalent to the original object. Example:

        LOCK_SHARED_RESOUCE();
        x = GET_OBJECT_FROM_SHARED_RESOURCE();
        result = sobj_copy(x);
        UNLOCK_SHARED_RESOURCE();
        return result;

Generated on Sat Jul 23 16:07:32 2005 for sobject by  doxygen 1.3.9.1