无法找到错误无效读取大小为4的valgrind

时间:2014-08-19 14:26:04

标签: c++ boost valgrind

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

Valgrind输出:


    ==10769== Memcheck, a memory error detector
    ==10769== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
    ==10769== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
    ==10769== Command: ./tachyon -s /HOME_ann/BII/biidurgak/test_new_tachyon/Tachyon_v5_improvedQueryTime/settings_smallNR
    ==10769== 
    ==10769== Thread 5:
    ==10769== Invalid read of size 4

    ==10769==    at 0x342669C9DE: std::string::assign(std::string const&) (in /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==    by 0x41BE16: findInDatabase() (Tachyon.cpp:103)

    ==10769==    by 0x4351D4: boost::detail::thread_data::run() (thread.hpp:117)

    ==10769==    by 0x4E53D01: thread_proxy (in /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: clone (in /lib64/libc-2.5.so)

    ==10769==  Address 0xfffffffffffffff8 is not stack'd, malloc'd or (recently) free'd

    ==10769== 

    ==10769== 

    ==10769== Process terminating with default action of signal 11 (SIGSEGV)

    ==10769==  Access not within mapped region at address 0xFFFFFFFFFFFFFFF8

    ==10769==    at 0x342669C9DE: std::string::assign(std::string const&) (in /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==    by 0x41BE16: findInDatabase() (Tachyon.cpp:103)

    ==10769==    by 0x4351D4: boost::detail::thread_data::run() (thread.hpp:117)

    ==10769==    by 0x4E53D01: thread_proxy (in /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: clone (in /lib64/libc-2.5.so)

    ==10769==  If you believe this happened as a result of a stack

    ==10769==  overflow in your program's main thread (unlikely but

    ==10769==  possible), you can try to increase the size of the

    ==10769==  main thread stack using the --main-stacksize= flag.

    ==10769==  The main thread stack size used in this run was 10485760.

    ==10769== 

    ==10769== HEAP SUMMARY:

    ==10769==     in use at exit: 219,895,341 bytes in 4,598,508 blocks

    ==10769==   total heap usage: 36,680,650 allocs, 32,082,142 frees, 1,474,244,383 bytes allocated

    ==10769== 

    ==10769== LEAK SUMMARY:

    ==10769==    definitely lost: 1,904 bytes in 2 blocks

    ==10769==    indirectly lost: 0 bytes in 0 blocks

    ==10769==      possibly lost: 184,232,229 bytes in 4,598,462 blocks

    ==10769==    still reachable: 35,661,208 bytes in 44 blocks

    ==10769==         suppressed: 0 bytes in 0 blocks

    ==10769== Rerun with --leak-check=full to see details of leaked memory

    ==10769== 

    ==10769== For counts of detected and suppressed errors, rerun with: -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;

1 个答案:

答案 0 :(得分:0)

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

伪代码:

// 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();
}