Memory management with Log4cpp
Im attempting to use log4cpp as a series of static member functions:
debug, error and info are each a function. Just pass in the relevant
formatted text and it writes to the configured location.
This is all well and good until I try to clean up the allocated memory.
Looking at the 'simple example' on this page it really looks like several
pointers are leaked. To avoid this I've tried several approaches but each
leads to a SIGSEGV:
smart pointers:
int main(int argc, char** argv)
{
//Setting up Appender, layout and Category
auto_ptr appender(new log4cpp::FileAppender( "FileAppender", LOGFILE ));
auto_ptr layout(new log4cpp::PatternLayout());
layout->setConversionPattern("%d: (%c)%p - %m %n");
log4cpp::Category& category = log4cpp::Category::getInstance( strCategory );
appender->setLayout( layout.get() );
category.setAppender( *appender );
category.setPriority( log4cpp::Priority::DEBUG );
category.debug( "debug message" );
}
This gives a segmentation fault when run.
Change this line:
auto_ptr layout(new log4cpp::PatternLayout());
to
log4cpp::PatternLayout* layout = new log4cpp::PatternLayout();
and add 'delete layout;' after the output.
Segmentation fault again.
Omitting the 'delete' line lets it run normally but it seems like it would
leak. (Im testing this on an ARM device using Debian 6.x - unfortunately
valgrind isn't available).
I'm missing something big and obvious here. Clue me in.
No comments:
Post a Comment