You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-user@logging.apache.org by Zhou Tao <zh...@gmail.com> on 2009/10/14 05:11:45 UTC

How to clean up the log4cxx objects to avoid the memory leak?

Experts,

I am using the log4cxx-0.10.0. And at the begining of the application, I
create the log4cxx instances including Logger, FileAppender, PatternLayout
etc. Sometime within the application, I need to destroy the instances.
For example,
To create the below objects at the begining,

_layout = new log4cxx::PatternLayout(format);
_fileAppender = new log4cxx::RollingFileAppender();
....

Then somewhere to remove the above objects like the below,

delete _fileAppender;
delete _layout;
....

However when deleting the objects, always core dump on windows. On unix,
when the application exit, also get a core dump.
Then how to clean up the log4cxx instances or a sequence is required when
deleting the objects? or some example code?

Any suggestion are highly appreciated.
Thanks,
Tom

Re: How to clean up the log4cxx objects to avoid the memory leak?

Posted by deepak singh <de...@gmail.com>.
Hi Tom,    You dont need to reclaim the memory, smart pointer will do it
automatically once it goes out of scope.

   LoggerPtr is a typedef for helpers::ObjectPtrT<Logger>. The class
  ObjectPtr is a Smart Pointer pattern implementation, which uses the
  helper class ObjectImpl. This class uses apr_atomic_inc32 and
  apr_atomic_dec32 to count the references to itself. If the reference
  count is 0, it deletes itself.

   You can validate it by running any memory profiler on your code.
Thanks
Deepak

On Wed, Oct 14, 2009 at 1:50 PM, Zhou Tao <zh...@gmail.com> wrote:

> Thanks, Deepak
>
> So if want to close one created looger and release its releated resources,
> what should I do? using smart pointer, I don't need to delete the objects.
> But How can I release the resources? Do I need to call the below API to
> release the resources?
>
> //close the appender
> _appenderPtr->close();
>
> //close all appenders associated with the logger
> _loggerPtr->closeNestedAppenders();
> _loggerPtr->removeAllAppenders();
>
> Or some other APIs I need to call to release all resources to avoid memory
> leak?
>
> Thank you.
> 2009/10/14 deepak singh <de...@gmail.com>
>
> You should use smart pointer instead.
>> e.g
>>
>>     log4cxx::LogManager::resetConfiguration();
>>
>>    log4cxx::LayoutPtr layoutPtr(new log4cxx::PatternLayout("%c-%p (%d{dd MMM yyyy HH:mm:ss}) [%-5t] %m%n"));
>>    log4cxx::AppenderPtr appenderPtr( new log4cxx::ConsoleAppender(layoutPtr, "System.err"));
>>    log4cxx::BasicConfigurator::configure(appenderPtr);
>>
>>
>> Thanks
>> Deepak
>>
>>
>> On Wed, Oct 14, 2009 at 8:41 AM, Zhou Tao <zh...@gmail.com> wrote:
>>
>>> Experts,
>>>
>>> I am using the log4cxx-0.10.0. And at the begining of the application, I
>>> create the log4cxx instances including Logger, FileAppender, PatternLayout
>>> etc. Sometime within the application, I need to destroy the instances.
>>> For example,
>>> To create the below objects at the begining,
>>>
>>> _layout = new log4cxx::PatternLayout(format);
>>> _fileAppender = new log4cxx::RollingFileAppender();
>>> ....
>>>
>>> Then somewhere to remove the above objects like the below,
>>>
>>> delete _fileAppender;
>>> delete _layout;
>>> ....
>>>
>>> However when deleting the objects, always core dump on windows. On unix,
>>> when the application exit, also get a core dump.
>>> Then how to clean up the log4cxx instances or a sequence is required when
>>> deleting the objects? or some example code?
>>>
>>> Any suggestion are highly appreciated.
>>> Thanks,
>>> Tom
>>>
>>
>>
>

Re: How to clean up the log4cxx objects to avoid the memory leak?

Posted by Zhou Tao <zh...@gmail.com>.
Thanks, Deepak

So if want to close one created looger and release its releated resources,
what should I do? using smart pointer, I don't need to delete the objects.
But How can I release the resources? Do I need to call the below API to
release the resources?

//close the appender
_appenderPtr->close();

//close all appenders associated with the logger
_loggerPtr->closeNestedAppenders();
_loggerPtr->removeAllAppenders();

Or some other APIs I need to call to release all resources to avoid memory
leak?

Thank you.
2009/10/14 deepak singh <de...@gmail.com>

> You should use smart pointer instead.
> e.g
>
>     log4cxx::LogManager::resetConfiguration();
>    log4cxx::LayoutPtr layoutPtr(new log4cxx::PatternLayout("%c-%p (%d{dd MMM yyyy HH:mm:ss}) [%-5t] %m%n"));
>    log4cxx::AppenderPtr appenderPtr( new log4cxx::ConsoleAppender(layoutPtr, "System.err"));
>    log4cxx::BasicConfigurator::configure(appenderPtr);
>
> Thanks
> Deepak
>
>
> On Wed, Oct 14, 2009 at 8:41 AM, Zhou Tao <zh...@gmail.com> wrote:
>
>> Experts,
>>
>> I am using the log4cxx-0.10.0. And at the begining of the application, I
>> create the log4cxx instances including Logger, FileAppender, PatternLayout
>> etc. Sometime within the application, I need to destroy the instances.
>> For example,
>> To create the below objects at the begining,
>>
>> _layout = new log4cxx::PatternLayout(format);
>> _fileAppender = new log4cxx::RollingFileAppender();
>> ....
>>
>> Then somewhere to remove the above objects like the below,
>>
>> delete _fileAppender;
>> delete _layout;
>> ....
>>
>> However when deleting the objects, always core dump on windows. On unix,
>> when the application exit, also get a core dump.
>> Then how to clean up the log4cxx instances or a sequence is required when
>> deleting the objects? or some example code?
>>
>> Any suggestion are highly appreciated.
>> Thanks,
>> Tom
>>
>
>

Re: How to clean up the log4cxx objects to avoid the memory leak?

Posted by deepak singh <de...@gmail.com>.
You should use smart pointer instead.
e.g

    log4cxx::LogManager::resetConfiguration();
   log4cxx::LayoutPtr layoutPtr(new log4cxx::PatternLayout("%c-%p
(%d{dd MMM yyyy HH:mm:ss}) [%-5t] %m%n"));
   log4cxx::AppenderPtr appenderPtr( new
log4cxx::ConsoleAppender(layoutPtr, "System.err"));
   log4cxx::BasicConfigurator::configure(appenderPtr);

Thanks
Deepak

On Wed, Oct 14, 2009 at 8:41 AM, Zhou Tao <zh...@gmail.com> wrote:

> Experts,
>
> I am using the log4cxx-0.10.0. And at the begining of the application, I
> create the log4cxx instances including Logger, FileAppender, PatternLayout
> etc. Sometime within the application, I need to destroy the instances.
> For example,
> To create the below objects at the begining,
>
> _layout = new log4cxx::PatternLayout(format);
> _fileAppender = new log4cxx::RollingFileAppender();
> ....
>
> Then somewhere to remove the above objects like the below,
>
> delete _fileAppender;
> delete _layout;
> ....
>
> However when deleting the objects, always core dump on windows. On unix,
> when the application exit, also get a core dump.
> Then how to clean up the log4cxx instances or a sequence is required when
> deleting the objects? or some example code?
>
> Any suggestion are highly appreciated.
> Thanks,
> Tom
>