Encountered a segfault during program start, it occured in loglevel.cxx:
static
LogLevel
defaultStringToLogLevelMethod(const tstring& s)
{
std::size_t const tbl_size
= sizeof (log_levels_table) / sizeof (log_levels_table[0]);
for (log_levels_table_rec const * it = log_levels_table;
it != log_levels_table + tbl_size; ++it)
{
if (*it->str == s) /////// Segfaulting Here ///////
return it->ll;
}
return NOT_SET_LOG_LEVEL;
}
The problem was that elements in the static log_levels_table hadn't been constructed yet.
The problem is well know in C++, this website explains it pretty well.
http://www.parashift.com/c++-faq/static-init-order.html
Well, there is nothing much I can do about it. You simply cannot use log4cplus before entering
main()at least.That's unacceptable. You do not always have control over when the first log message is being generated. It can very well be produced by a statically initialized object.
What do you want me to do about it? I ain't no magician.
True ;)
Looking at the code, I doubt it's a static initialization order fiasco.
The variable log_levels_table is defined in the same compilation unit as defaultStringToLogLevelMethod(). I must be missing something.
Are you using log4cplus as static library? I think it still could be static initialization order fiasco if you were using log4cplus as a static library.
You might get away with calling
log4cplus::initialize()yourself before invoking log4cplus from your static initializers.Last edit: Václav Haisman 2014-05-06
We are using a global static instance of log4cplus::Logger in most of our cpp files. During this construction it attempts to use defaultStringToLogLevelMethod some where which isn't initialized yet.
I have attached possible solution.
What compiler are you using?
g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) on RedHat Enterprise 6.5
I have your patch with some additional changes to 1.2.x branch. Please test it.
Thanks, we will try and test it.
And I have pushed similar patches to 1.1.x and master as well.
I had to revert the change because it made static log4cplus library non-functional. I am afraid we are back to "do not use log4cplus before
main()" situation.The "do not use log4cplus before
main()" advice remains. I am closing this.