2

I'm here to ask your opinion. I'm new in a big project so I will try to describe the simple example as I see it.

The top backtrace is

#0  0xb6adfc6d in pthread_mutex_lock () from /usr/lib/libpthread.so.0
#1  0x080d8565 in boost::mutex::lock() ()
#2  0x080d8613 in boost::unique_lock<boost::mutex>::lock() ()
#3  0x080d8642 in boost::unique_lock<boost::mutex>::unique_lock(boost::mutex&)
#4  0x...      in ???    //just ??? in stack
#5  0x...      in ???
#6  0x...      in ???

It seems the mutex does not exist but it is created in class contructor. Example:

class A
{
  boost::mutex::scoped_lock mutex_;
public:
  A(): mutex_() {}

  void Read (...)
  {
    //some checks
    boost::mutex::scoped_lock lock(mutex_); // <-- Segfault
    //read
  }

  void Write (...)
  {
    //some checks
    boost::mutex::scoped_lock lock(mutex_);
    //write
  }
};

It seems strange for me because I have no idea the reason of the segfault or possible ways to find the root cause. I would be glad to hear your any advises about this one.

Torrius
  • 737
  • 3
  • 12
  • 20
  • 1
    Compile your code with debug information enabled (`-g` option for GCC and clang for example), and run again. You should hopefully get more information in that backtrace. – Some programmer dude Apr 07 '13 at 18:31
  • 3
    As for the crash, are you sure you're calling the `Read` function on a valid object? I.e. not an object you've deleted or using a pointer/reference to a local variable returned from some function? You should probably provide more information, like how the object is created or where you get it from. – Some programmer dude Apr 07 '13 at 18:32
  • Thanks I'll pay an attention to this one say about results. – Torrius Apr 07 '13 at 18:57
  • 1
    Finally someone listened to me and posted backtrace , – stdcall Apr 07 '13 at 20:40
  • I've found the root cause: object exists but it has corrupted data in fields. Segfault happens when I try to get an access to any (not only mutex) its field. – Torrius Apr 08 '13 at 20:34
  • Corrupted data might mean that you delete the object but kept the pointer. Or that you are using a pointer/reference to an object declared locally in another function. – Some programmer dude Apr 09 '13 at 13:50

1 Answers1

1

It' looks like you're scope locking a scope lock - it's probably a typo

Random example usage: http://www.boost.org/doc/libs/1_53_0/libs/thread/example/mutex.cpp

The usual pattern is to scope the mutex, using the scoped_lock class

boost::recursive_mutex mutex;
void somefunc() {
    boost::unique_lock<boost::recursive_mutex> scoped_lock(mutex);
}
Jason De Arte
  • 467
  • 1
  • 3
  • 14