【问题标题】:can't find error Invalid read of size 4 valgrind找不到错误大小为 4 valgrind 的无效读取
【发布时间】:2014-08-19 14:26:03
【问题描述】:

我已经用 C++ 实现了多任务应用程序。生产者推送队列,消费者从队列中获取元素。有时我的应用程序崩溃了。有人可以帮我解决这个问题。顺丰

Valgrind 输出:

==10769== Memcheck,内存错误检测器 ==10769== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. ==10769== 使用 Valgrind-3.9.0 和 LibVEX;使用 -h 重新运行以获取版权信息 ==10769== 命令:./tachyon -s /HOME_ann/BII/biidurgak/test_new_tachyon/Tachyon_v5_improvedQueryTime/settings_smallNR ==10769== ==10769== 线程 5: ==10769== 大小为 4 的读取无效 ==10769== 在 0x342669C9DE:std::string::assign(std::string const&)(在 /usr/lib64/libstdc++.so.6.0.8 中) ==10769== by 0x42E01A: std::pair::operator=(std::pair const&) (stl_pair.h:152) ==10769== by 0x42B27A: boost::lockfree::detail::ringbuffer_base >::pop(std::pair&, std::pair*, unsigned 长)(spsc_queue.hpp:154) ==10769== by 0x428719: boost::lockfree::detail::compile_time_sized_ringbuffer, 2ul>::pop(std::pair&) (spsc_queue.hpp:305) ==10769== by 0x425DEA: boost::lockfree::spsc_queue, boost::lockfree::capacity, boost::parameter::void_>::pop(std::pair&) (spsc_queue.hpp:572) ==10769== 0x41BE16:findInDatabase() (Tachyon.cpp:103) ==10769== by 0x4351D4: boost::detail::thread_data::run() (thread.hpp:117) ==10769== by 0x4E53D01:thread_proxy(在/HOME_ann/BII/biidurgak/test_new_tachyon/boost_install/boost_1_55_0/lib/libboost_thread.so.1.55.0) ==10769== by 0x342120673C: start_thread (in /lib64/libpthread-2.5.so) ==10769== by 0x34206D3D1C:克隆(在 /lib64/libc-2.5.so 中) ==10769== 地址 0xfffffffffffffff8 没有被堆栈、malloc 或(最近)释放 ==10769== ==10769== ==10769== 进程以信号 11 (SIGSEGV) 的默认操作终止 ==10769== 访问不在地址 0xFFFFFFFFFFFFFFF8 的映射区域内 ==10769== 在 0x342669C9DE:std::string::assign(std::string const&)(在 /usr/lib64/libstdc++.so.6.0.8 中) ==10769== by 0x42E01A: std::pair::operator=(std::pair const&) (stl_pair.h:152) ==10769== by 0x42B27A: boost::lockfree::detail::ringbuffer_base >::pop(std::pair&, std::pair*, unsigned long) (spsc_queue.hpp:154) ==10769== by 0x428719: boost::lockfree::detail::compile_time_sized_ringbuffer, 2ul>::pop(std::pair&) (spsc_queue.hpp:305) ==10769== by 0x425DEA: boost::lockfree::spsc_queue, boost::lockfree::capacity, boost::parameter::void_>::pop(std::pair&) (spsc_queue.hpp:572) ==10769== 0x41BE16:findInDatabase() (Tachyon.cpp:103) ==10769== by 0x4351D4: boost::detail::thread_data::run() (thread.hpp:117) ==10769== by 0x4E53D01:thread_proxy(在/HOME_ann/BII/biidurgak/test_new_tachyon/boost_install/boost_1_55_0/lib/libboost_thread.so.1.55.0) ==10769== by 0x342120673C: start_thread (in /lib64/libpthread-2.5.so) ==10769== by 0x34206D3D1C:克隆(在 /lib64/libc-2.5.so 中) ==10769== 如果您认为这是由于堆栈造成的 ==10769== 程序主线程溢出(不太可能但 ==10769==可能),你可以尝试增加大小 ==10769== 使用 --main-stacksize= 标志的主线程堆栈。 ==10769== 本次运行使用的主线程堆栈大小为 10485760。 ==10769== ==10769== 堆摘要: ==10769== 在退出时使用:4,598,508 个块中的 219,895,341 个字节 ==10769== 总堆使用量:36,680,650 次分配,32,082,142 次释放,1,474,244,383 字节分配 ==10769== ==10769== 泄漏摘要: ==10769== 肯定丢失:2 个块中的 1,904 个字节 ==10769== 间接丢失:0 个块中的 0 个字节 ==10769== 可能丢失:4,598,462 个块中的 184,232,229 个字节 ==10769== 仍然可达:44 个块中的 35,661,208 字节 ==10769== 抑制:0 个块中的 0 个字节 ==10769== 使用 --leak-check=full 重新运行以查看泄漏内存的详细信息 ==10769== ==10769== 对于检测到和抑制的错误计数,重新运行:-v

制作人:

    void producer(string file) {
            ifstream query(file.c_str());
            string description = "";
            string sequence = "";

            string line;
            while (getline(query, line)) {
                //read description
                if (line == "") continue;
                if (line.at(0) == '>') {
                    if (sequence != "") {
                        pair<string, string> a = make_pair(description, sequence);
                        while (!queue.push(a))
                            ;
                        sequence = "";
                    }
                    description = line.substr(1);
                } else {
                    sequence += line;
                }
            }

            if (sequence != "" && description != "") {
        pair<string, string> a = make_pair(description, sequence);
        while (!queue.push(a))
            ;
    }
}

在消费者中我有这个:

 void Consumer(void) {
             pair<string, string>element;
             //part of code 

             while(queue.pop(element)){  //Line 103 in Tachyon.cpp 
                string queryDescription = element.first;
                string sequence = element.second;

                //Part of code 
             }

          }

队列是全局变量:

boost::lockfree::spsc_queue<pair<string, string>, boost::lockfree::capacity<2> > queue;

【问题讨论】:

    标签: c++ boost valgrind


    【解决方案1】:

    全局变量、多个线程(我假设)、一个读取和另一个线程写入 - 您需要一个同步对象,如互斥锁或临界区。

    伪代码:

    // Consumer
    loop-begin;
    Lock();
    get-item-into-local-variable
    Unlock();
    process-local-variable-item;
    loop-end
    
    // Producer
    void AddItem(item)
    {
    Lock();
    Add-item-into-queue
    Unlock();
    }
    

    【讨论】:

    • 我使用 boost 库中的队列,这个队列是线程安全的,我不需要在代码中使用互斥锁。
    • 好吧..然后它使我的答案无效,我忽略了它。我不知道boost库。可能有一些方法不是并发安全的。我猜queue::pop
    • 不,所有方法都是并发安全的。
    • 是的,线程安全。但我读到只有一个线程(在特定时间)被允许推送/弹出。在调用pop之前还要检查项目是否在队列中。
    • 正如你在这个例子中看到的boost.org/doc/libs/1_54_0/doc/html/lockfree/examples.html我们可以使用更多线程来进行pop和push,而无需互斥锁
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    相关资源
    最近更新 更多