You are here: Appendix A - Technical Notes > Reference Counting

## Reference Counting

LTIImageStage objects are now reference counted.

- Use ClassName::create() to create a new object of type ClassName.
- Use object->retain() when you want to keep an object around (but not if you created it)
- Use object->release() when you are done using the object.

As a rule of thumb, if you create or retain an object then you must issue a corresponding release for it. ClassName::create() and LTIImageStageManager::createImageStage() are the two must common ways to create an LTIImageStage object. When you pass a reader to a filter, the filter calls retain() on the reader which “keeps the object alive”. Before the pointer to the reader goes out of scope or is reassigned you must call release() as shown in the following example:

LTIImageStage *pipeline = NULL;

{

MrSIDImageReader *reader = MrSIDImageReader::create();

sts = reader->initialize(filename);

pipeline = reader;

}

{

MyImageFilter *filter = MyImageFilter::create();

// ‘pipeline’ will be retained by ‘filter’

sts = filter->initialize(pipeline);

// release ‘pipeline’ because we are reusing the variable

pipeline->release();

pipeline = filter;

}

...

... do something with the pipeline

...

// done with the pipeline: releasing ‘pipeline’ will release the

// filter, which will in turn release the reader

pipeline ->release();

pipeline = NULL;