destruction of static member

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.
 
V

Victor Bazarov

I have a LogManager class, which is a singleton.
There is a static member inside the class of the type LogManager.

Not true. The static member is of the type "pointer to LogManager", not
"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.

Who is supposed to call the destructor? Did you write any code that
would do that?
> But that does not happen. I thought static
variables were destroyed when main returned or when exit was called.

They are. The pointer to LogManager (LogManager::INSTANCE) *is*
destroyed. The only problem (for you) is that the destruction of the
pointer does NOT trigger a call to the destructor of the actual object
to which that pointer points.
So why is the destructor not called and is the memory not freed if it
is not called?

Nobody can tell. You didn't post the complete program.
#################################
My code:
#################################

#ifndef LOGMANAGER_H_
#define LOGMANAGER_H_

What is that for?
#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;
}

There seems to be an #endif missing.
result:
LogManager created.
last report comming up

I guess we have to take your word for it. Nothing in your posted code
suggests that the output should be what you describe here.
expected result:
LogManager created.
last report comming up
LogManager created.
last report comming up
LogManager destroyed.

Nothing in your posted code suggests that you programmed it to get this
specific output.

V
 
M

michael.goossens

Thanks you guys are right.

Victor it was a theoretical question about the working of c++. It
didn't require my full code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top