PTypes: variant: object references

Top: Basic types: variant: Object references

Variants can hold references to objects derived from component. PTypes performs reference counting, so that assigning variants containing object references can be considered safe: an object will only be destroyed when there are no more references left to it. If you want to control the destruction of an object 'manually', you can increment the reference count with addref() before assigning a reference to a variant (see unknown & component). In this case, each call to addref() should be balanced with a call to release().

It should be noted that the reference counting mechanism has a potential flaw which may lead to memory leaks. Consider two objects containing variants with cross-referencing pointers to each other. The library can not keep track of such circular references, and an attempt to free one of the objects will result in destroying only one of them and leaving the other in the memory without any references to it. This is a known problem and can be solved by either eliminating circular references when designing data structures or by providing a complex memory 'garbage collector' which keeps track of all object references in the program.

See also: unknown & component, Assignments and typecasts, Arrays, Utilities

PTypes home