M
michael.goossens
Hi,
I have a LogManager class, which is a singleton.
There is a static member inside the class of the type LogManager.
Constructor and Destructor are private. The creation and usage of the
LogManager works fine. But when the program terminates I expected the
destructor to be called. But that does not happen. I thought static
variables were destroyed when main returned or when exit was called.
So why is the destructor not called and is the memory not freed if it
is not called?
#################################
My code:
#################################
#ifndef LOGMANAGER_H_
#define LOGMANAGER_H_
#include <list>
#include "LogMessage.h"
class LogManager {
public:
static LogManager * getInstance(void);
void addLogMessage(std::string * message);
void report(void); //prints the whole log
void dump (void); //prints the whole log and clears it
void flush(void);
private:
static LogManager * INSTANCE;
std::list<LogMessage *> logMessages;
LogManager(); // adds "LogManager created." to the log
~LogManager(); // adds "LogManager destroyed." to the log and calls
dump()
};
int main(int arg){
LogManager * logMgr = LogManager::getInstance();
logMgr->addLogMessage(new std::string("last report comming up"));
logMgr->report();
return 0;
}
result:
LogManager created.
last report comming up
expected result:
LogManager created.
last report comming up
LogManager created.
last report comming up
LogManager destroyed.
I have a LogManager class, which is a singleton.
There is a static member inside the class of the type LogManager.
Constructor and Destructor are private. The creation and usage of the
LogManager works fine. But when the program terminates I expected the
destructor to be called. But that does not happen. I thought static
variables were destroyed when main returned or when exit was called.
So why is the destructor not called and is the memory not freed if it
is not called?
#################################
My code:
#################################
#ifndef LOGMANAGER_H_
#define LOGMANAGER_H_
#include <list>
#include "LogMessage.h"
class LogManager {
public:
static LogManager * getInstance(void);
void addLogMessage(std::string * message);
void report(void); //prints the whole log
void dump (void); //prints the whole log and clears it
void flush(void);
private:
static LogManager * INSTANCE;
std::list<LogMessage *> logMessages;
LogManager(); // adds "LogManager created." to the log
~LogManager(); // adds "LogManager destroyed." to the log and calls
dump()
};
int main(int arg){
LogManager * logMgr = LogManager::getInstance();
logMgr->addLogMessage(new std::string("last report comming up"));
logMgr->report();
return 0;
}
result:
LogManager created.
last report comming up
expected result:
LogManager created.
last report comming up
LogManager created.
last report comming up
LogManager destroyed.