Do I need to explicitly release the memory while using RollingFileAppender?

292 views Asked by At

I am implementing the sample code of log4cplus in which I am using RollingFileAppender to store the log messages. My question is do I need to explicitly free the memory allocated by the following line by calling the destructor:

SharedAppenderPtr myAppender1(new RollingFileAppender(LOG4CPLUS_TEXT("myTTCCLayoutLogFile.log"),
                                                               3 * 1024 * 1024,    // Max backup size
                                                               3));                // Max index

The whole code is:

#include "samplefileloggerheader.h"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Entry point for the program
//
int main()
{
    //
    // Local variables definition
    //
    int                     initLoop    = 0;    // Initialization variable for
                                                // the loop
    int                     LOOP_COUNT  = 100000;
    std::auto_ptr<Layout>   myLayout1;

    //
    // Function logic begins here
    //
    for (;;)
    {
        log4cplus::initialize();

        //
        // LogLog class is used to output log statements from within the
        // log4cplus package.
        // setInternalDebugging() Allows to enable/disable log4cplus
        // internal logging
        //
        helpers::LogLog::getLogLog()->setInternalDebugging( true );

        //
        // Instantiating a file appender that will append events in the
        // TTCCLayout() layout
        //
        SharedAppenderPtr myAppender1(new RollingFileAppender(LOG4CPLUS_TEXT("myTTCCLayoutLogFile.log"),
                                                               3 * 1024 * 1024,    // Max backup size
                                                               3));                // Max index
        myAppender1->setName(LOG4CPLUS_TEXT("sampleAppender1"));

        //
        // Instantiating a layout object
        //
        myLayout1 = std::auto_ptr<Layout>(new log4cplus::TTCCLayout());

        //
        // Attaching the layout object with the appender object
        //
        myAppender1->setLayout(myLayout1);

        //
        // Getting the root logger
        //
        Logger::getRoot().addAppender(myAppender1);

        //
        // Creating instances of logger
        //
        Logger  root        = Logger::getRoot();
        Logger  test        = Logger::getInstance(LOG4CPLUS_TEXT("test"));
        Logger  subTest     = Logger::getInstance(LOG4CPLUS_TEXT("test.subTest"));

        //
        // Writing the logs to the file
        //
        for (initLoop; initLoop < LOOP_COUNT; ++initLoop)
        {
            NDCContextCreator _context(LOG4CPLUS_TEXT("for()"));
            LOG4CPLUS_DEBUG(subTest, "Loop count index is : " << initLoop);
        } // for

        //
        // Final break statement
        //
        break;
    } // for

   return 0;
} // main()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

One more question, do I need to take care of multi-threading or log4clpus do it for me?

1

There are 1 answers

0
Adam Romanek On

I don't know log4clplus library but the name of the variable type (SharedAppenderPtr) suggests it's a kind of shared_ptr which should take care of releasing resources for you.