#303 Segfault during static initialization

v1.1.2
closed
5
2016-03-22
2014-05-06
Ray Logel
No

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

Discussion

  • Václav Haisman

    Václav Haisman - 2014-05-06
    • assigned_to: Václav Zeman
     
  • Václav Haisman

    Václav Haisman - 2014-05-06

    Well, there is nothing much I can do about it. You simply cannot use log4cplus before entering main() at least.

     
    • Marcel Loose

      Marcel Loose - 2014-05-07

      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.

       
      • Václav Haisman

        Václav Haisman - 2014-05-07

        What do you want me to do about it? I ain't no magician.

         
        • Marcel Loose

          Marcel Loose - 2014-05-07

          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.

           
          • Václav Haisman

            Václav Haisman - 2014-05-07

            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.

             
  • Václav Haisman

    Václav Haisman - 2014-05-06

    You might get away with calling log4cplus::initialize() yourself before invoking log4cplus from your static initializers.

     
    Last edit: Václav Haisman 2014-05-06
  • Václav Haisman

    Václav Haisman - 2014-05-06
    • labels: --> v1.1.2, initialization
     
  • Ray Logel

    Ray Logel - 2014-05-08

    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.

     
    Attachments
  • Václav Haisman

    Václav Haisman - 2014-08-17

    What compiler are you using?

     
    • Ray Logel

      Ray Logel - 2014-08-21

      g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4) on RedHat Enterprise 6.5

       
  • Václav Haisman

    Václav Haisman - 2014-08-21

    I have your patch with some additional changes to 1.2.x branch. Please test it.

     
    • Ray Logel

      Ray Logel - 2014-08-25

      Thanks, we will try and test it.

       
  • Václav Haisman

    Václav Haisman - 2014-08-21

    And I have pushed similar patches to 1.1.x and master as well.

     
  • Václav Haisman

    Václav Haisman - 2016-03-22

    The "do not use log4cplus before main()" advice remains. I am closing this.

     
  • Václav Haisman

    Václav Haisman - 2016-03-22
    • status: open --> closed
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks