简体   繁体   English

std::lock_guard<std::mutex> 施工中的段错误?

[英]std::lock_guard<std::mutex> segfaults on construction?

I'm attempting to access a shared std::queue using a std::mutex and a std::lock_guard.我正在尝试使用 std::mutex 和 std::lock_guard 访问共享的 std::queue。 The mutex (pending_md_mtx_) is a member variable of another object (whose address is valid).互斥体(pending_md_mtx_)是另一个对象(其地址有效)的成员变量。 My code seems to be segfault'ing on the construction of the lock_guard.我的代码似乎在 lock_guard 的构造上出现了段错误。

Any ideas?有任何想法吗? Should I be using a std::unique_lock (or some other object) instead?我应该改用 std::unique_lock (或其他对象)吗? Running GCC 4.6 (--std=c++0x) under Ubuntu Linux.在 Ubuntu Linux 下运行 GCC 4.6 (--std=c++0x)。 I can't post the entire class, but the only accesses to the mutex and queue listed below.我不能发布整个课程,但只能访问下面列出的互斥锁和队列。

template <typename ListenerT>
class Driver
{
public:
    template <typename... Args>
    Driver(Args&&... args) :
        listener_(std::forward<Args>(args)...) {}

    void enqueue_md(netw::Packet* packet)
    {
        std::lock_guard<std::mutex> lock(pending_md_mtx_);
        pending_md_.push(packet);
    }

    void process_md()
    {
        std::lock_guard<std::mutex> lock(pending_md_mtx_);
        while (pending_md_.size())
        {
            netw::Packet* pkt=pending_md_.front();
            pending_md_.pop();
            process_md(*pkt);
        }
    }
    //... Other code which I can't post...

private:
    ListenerT listener_;
    std::mutex pending_md_mtx_;
    std::queue<netw::Packet*> pending_md_;
};

GDB Stacktrace: GDB 堆栈跟踪:

(gdb) bt
#0  __pthread_mutex_lock (mutex=0x2f20aa75e6f4000) at pthread_mutex_lock.c:50
#1  0x000000000041a2dc in __gthread_mutex_lock (__mutex=0xff282ceacb40) at /usr/include/c++/4.6/x86_64-linux-gnu/./bits/gthr-default.h:742
#2  lock (this=0xff282ceacb40) at /usr/include/c++/4.6/mutex:90
#3  lock_guard (__m=..., this=0x7f2874fc4db0) at /usr/include/c++/4.6/mutex:445
#4  driver::Driver<Listener, false>::enqueue_md (this=0xff282ceac8a0, packet=...) at exec/../../driver/Driver.hpp:95

I was getting a segfault on constructing the std::lock_guard , turns out my code was using an uninitialized std::shared_ptr<my_object_with_mutex> .我在构造std::lock_guard时遇到了段错误,结果我的代码使用了未初始化的std::shared_ptr<my_object_with_mutex> Using a properly constructed my_object_with_mutex resolves the problem.使用正确构造的my_object_with_mutex可以解决问题。

I recently encountered this problem.我最近遇到了这个问题。 It was caused by line of code causing a buffer overrun after acquiring the lock.它是由获取锁后导致缓冲区溢出的代码行引起的。 It would seem odd for a line of code below the lock to be causing a problem a few lines earlier, but I suppose the buffer overrun would cause some corruption that causes a problem on a second call to the function.锁定下方的一行代码在前几行引起问题似乎很奇怪,但我认为缓冲区溢出会导致一些损坏,从而导致第二次调用该函数时出现问题。

The issue rootcause in my case:就我而言,问题的根本原因是:

  1. An object A references object B对象 A 引用对象 B
  2. On call to object B.func() I see a SegFault on lock_guard在调用对象B.func()时,我在 lock_guard 上看到了 SegFault
  3. Object B has never been set for object A (not initialized, a NULL pointer), leading to a SegFault on accessing a field (mutex, in my case).从未为对象 A 设置对象 B(未初始化,NULL 指针),导致访问字段时出现 SegFault(在我的情况下为互斥体)。

The error could be diagnosed from GDB by noticing this=0x0 :通过注意到this=0x0可以从 GDB 诊断该错误:

...
#4  0x000055e3a9e14a3c in B<C>::write (this=0x4e2280, msg=0x55e3aac03be0) at /proj/B.hpp:35
#5  0x000055e3a9e206e6 in A::write (this=0x0, msg=0x55e3aac03be0) at /proj/A.cpp:286
#6  0x000055e3a9e2069a in A::write (this=0x7f21eae64010, msg=0x55e3aac03be0) at /proj/A.cpp:277
...

In my case the root cause was the same (object with mutex uninitialized), but the reason was not.在我的情况下,根本原因是相同的(未初始化互斥锁的对象),但原因不是。

The object that had the mutex had a function reset .拥有互斥锁的对象有一个函数reset Guess what, shared_ptr has also a function named reset , and I called that instead!猜猜看, shared_ptr还有一个名为reset的函数,我改为调用它!

Avoid using reset as a name, or double-check if you're not using obj.reset() instead of obj->reset() !避免使用reset作为名称,或者仔细检查您是否使用obj.reset()而不是obj->reset()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM