Menu

#40 Access Violation with msvc6

open
nobody
core (26)
5
2002-09-27
2002-09-27
Anonymous
No

When I try to execute the following code with win xp,
visual c++ 6.0, log4cpp 0.3.2rc5 the I get an access
violation in ntdll.dll.

When I remove the line begins with static all works fine.
I need the static method for use of log4cpp in classes.
At the moment I retrieve an pointer to an preinitalized
Category with the Catgory::exists() method, to get
working log4cpp with classes.

Any hints? Maybe I do something wrong?
I'm new user of log4cpp.

#include <log4cpp/Category.hh>

using namespace log4cpp;

// this line produced an access violation
static Category& cat = Category::getInstance
("main.test");

int main() {

Category &log=Category::getInstance("main");
log.debug("test");
return 0;

}

Discussion

  • Nobody/Anonymous

    Logged In: NO

    Hello,

    i experienced the same problem, some other bug reports
    seem to go in same direction...

    Well the cause is just an old problem of dependant global
    objects: construction order.

    // snip begin+++ appender.cpp

    namespace log4cpp {
    Appender::AppenderMap* Appender::_allAppenders;
    threading::Mutex Appender::_appenderMapMutex; <----
    this mutex object poses the problem!!!

    // snip end---

    The order of initialization is undefined across compilation
    units. So if someone uses (like me) static log instance
    references at global files scope they must be initialized at the
    beginning.

    example:

    static log4cpp::Category& = log4cpp::Category::getInstance
    () .... constructs a new Appender, which gets added to map
    of appenders.
    If the mutex, which is used to protect the map is not yet
    constructed (in another compilation unit) -> *boom*.

    Solutions: not elegant ones yet ...

    1.) instead of "threading::Mutex
    Appender::_appenderMapMutex;"

    use "first time access - first init"

    threading::Mutex& _getAppenderMapMutex()
    {
    static threading::Mutex mutex;
    return mutex;
    }

    2.) #pragma init_seg (non portable)

    Regards,

    Anastasius Focht

     
  • Nobody/Anonymous

    Logged In: NO

    Hello,

    i experienced the same problem, some other bug reports
    seem to go in same direction...

    Well the cause is just an old problem of dependant global
    objects: construction order.

    // snip begin+++ appender.cpp

    namespace log4cpp {
    Appender::AppenderMap* Appender::_allAppenders;
    threading::Mutex Appender::_appenderMapMutex; <----
    this mutex object poses the problem!!!

    // snip end---

    The order of initialization is undefined across compilation
    units. So if someone uses (like me) static log instance
    references at global files scope they must be initialized at the
    beginning.

    example:

    static log4cpp::Category& = log4cpp::Category::getInstance
    () .... constructs a new Appender, which gets added to map
    of appenders.
    If the mutex, which is used to protect the map is not yet
    constructed (in another compilation unit) -> *boom*.

    Solutions: not elegant ones yet ...

    1.) instead of "threading::Mutex
    Appender::_appenderMapMutex;"

    use "first time access - first init"

    threading::Mutex& _getAppenderMapMutex()
    {
    static threading::Mutex mutex;
    return mutex;
    }

    2.) #pragma init_seg (non portable)

    Regards,

    Anastasius Focht

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.