0

I am having some trouble using the pantheios logging library with boost::threads. It seems that if I use any pantheios logging before creating the thread, I will get a segfault.

For example, the following would work:

thread_ = new boost::thread(&foo);
pantheios::log(pantheios::debug, "Hello world");

But if the order of the statements are switched, a stack trace reveals that I crash on start_thread in boost.

pantheios::log(pantheios::debug, "Hello world");
thread_ = new boost::thread(&foo);
// SEGFAULT!

Any ideas?

EDIT: More context

int main()
{
    pantheios::log(...);
    MyClass myClass(/* some arguments, which are all literals */);

    // Do some more work

    return 0;
}

// MyClass constructor
MyClass::MyClass(/* args */)
    : member1_(arg1)
    , member2_(arg2)
{
    thread_ = new boost::thread(&MyClass::workerLoop, this);
}

// Destructor
MyClass::~MyClass()
{
    thread_->join();
    delete thread_;
}

This will segfault at start_thread. Once again if I swap the two lines in main it will work without any problems.

Andrew Lee
  • 2,543
  • 3
  • 20
  • 31
  • 1
    what is `foo`, a function? Does it do any logging? Does it use any globals? – Jonathan Wakely Jun 19 '12 at 22:40
  • `foo` is a member function of a class, actually. It's bound to the class' `this` using `boost::bind`. `foo` uses pantheios as well, and also it utilizes network sockets. pantheios is supposed to be thread-friendly though... – Andrew Lee Jun 20 '12 at 13:25
  • You're passing a pointer, `&foo`, so it can't be a member function or a function object returned by `boost::bind`. Is `foo` declared on the stack? Does it go out of scope? Please show some more code. – Jonathan Wakely Jun 20 '12 at 13:29
  • Here is the actual code: `thread_ = new boost::thread(&MyClass::workerLoop, this);` which is inside `MyClass` constructor. See my edit to the question for context. – Andrew Lee Jun 20 '12 at 13:46
  • Does the `MyClass` destructor join the thread before `myClass` goes out of scope? – Jonathan Wakely Jun 20 '12 at 13:56
  • Yes. It seems that when I allocate `myClass` on the heap (then delete at the end of `main`), rather than on the stack, I do not get a segfault. – Andrew Lee Jun 20 '12 at 14:08
  • That being said, it'd be nice to have it working with the stack... – Andrew Lee Jun 20 '12 at 14:40

1 Answers1

0

Looks like I figured it out. I was linking to different versions of boost. My system had boost version 1.40, whereas I was also using a newer version of boost I downloaded, 1.49. Notable the boost thread I was linking was to the older version.

Once I made the links to boost consistent, everything worked as expected.

Andrew Lee
  • 2,543
  • 3
  • 20
  • 31