On Mar 4, 2008, at 1:57 PM, Kiyoshi Mizumaru wrote:
I'm trying to narrowing down my problem and find that the following
code
crashes with signal 11, but eliminating the `//' on the sixth line
make it run
without any problem. Could anyone please give me a clue?
--kiyoshi
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/propertyconfigurator.h>
#include <iostream>
// static log4cxx::LoggerPtr s_message =
log4cxx::Logger::getLogger("root");
class baz {
public:
~baz() {
std::cout << "baz::~baz()" << std::endl;
LOG4CXX_FATAL(log4cxx::Logger::getLogger("root"), "~baz()");
}
} s_baz;
int main(int, char**) {
log4cxx::BasicConfigurator::configure();
return EXIT_SUCCESS;
}
You are getting bit by destruction order of static objects. The first
log4cxx call will create a local static member that initializes APR.
On program end, static objects are then destroyed in the reverse order
of creation. As the program stands, the sequence is:
baz::baz()
enter main
log4cxx::BasicConfigurator::configure() -> triggers APR initialization
exit main
APR termination
baz::~baz() -> calls log4cxx but APR has been terminated
If you uncomment the line, then:
s_message construction -> triggers APR initialization
baz::baz()
enter main
log4cxx::BasicConfigurator::configure()
exit main
baz::~baz()
APR termination
Anytime you perform logging in the destructor of a static object, you
need to ensure that log4cxx is initialized in or before the initial
construction of the static object.