Poco "Garbage collection"

Please post support and help requests here.
Posts: 10
Joined: 06 Dec 2007, 22:29
Location: United_States

Poco "Garbage collection"

Postby WITTROCK » 07 Dec 2007, 21:15

Hello All,

First, I have very little experience with POCO, but I really like what I have used so far.

Forgive me if my question is a little off, as I am still trying to get a handle on how some of the "garbage collection" works in POCO. I'll just give one example with the Poco.Util.Option class.

If I want to add a validator to an option, I do something like this:


Option("testint", "i", "test integer property")




.validator(new IntValidator(-100,100) ));

Looking in UtilincludePocoUtilOption.h, I see the declaration

Option& validator(Validator* pValidator);

/// Sets the validator for the given option.


/// The Option takes ownership of the Validator and

/// deletes it when it's no longer needed.

But, when I look in UtilsrcOption.cpp, I see the implementation

Option& Option::validator(Validator* pValidator)


if (_pValidator) _pValidator->release();

_pValidator = pValidator;

if (_pValidator) _pValidator->duplicate();

return *this;



I notice that the duplicate() method is being called. Doesn't this increment the refcount to 2? So when the Option destructor is called, or a new validator is assigned, won't the refcount be decremented to 1 and the object won't be deleted?

Sorry if I am missing something very simple.


Posts: 1268
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: Poco "Garbage collection"

Postby guenter » 08 Dec 2007, 11:29

You are right. The documentation is wrong in this case, and the sample code in SampleApp.cpp produces a memory leak. This will be fixed in the next release.

Posts: 10
Joined: 06 Dec 2007, 22:29
Location: United_States

Re: Poco "Garbage collection"

Postby WITTROCK » 09 Dec 2007, 01:22

Thanks Guenter,

I think I just need to become more familiar with which classes/methods take ownership of an object, and which ones share it.

I will admit to having limited POCO (and c++) experience, so some of the classes which take ownership of a new object scare me a bit. I am just not used to calling new without handling the delete myself. Once I gain some more experience and comfort with this method of "garbage collection", I think it will be a very useful tool.

One thing I can't praise enough is the beauty of the source code. Very easy to follow and consistent throughout. Even if I did not use POCO as a class library, I would find it a good example of how to write clear, self documenting c++.

Thanks Again,

Return to “Support”

Who is online

Users browsing this forum: No registered users and 5 guests