我有以下最小的示例代碼。我想要做的是: 在一個單獨的線程中,啓動一個boost::asio::deadline_timer
,以便每調用一個函數loop
,就會完成一些操作,定時器會自行重新設置,直到無窮大。Valgrind錯誤:系統調用參數epoll_pwait(sigmask)指向無法尋址的字節(s)
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
void wait(int milliseconds) {
boost::asio::io_service ioService;
boost::asio::deadline_timer timer(ioService, boost::posix_time::milliseconds(milliseconds));
timer.wait();
}
class Foo {
public:
boost::asio::io_service ioService;
boost::thread* thread = nullptr;
boost::posix_time::milliseconds duration;
boost::asio::deadline_timer timer;
bool shouldStop = false;
Foo() : ioService(), duration(10), timer(ioService, duration) {
}
~Foo() {
if (thread != nullptr) {
shouldStop = true;
wait(15);
ioService.stop();
thread->join();
delete thread;
thread = nullptr;
}
}
void start() {
timer.async_wait(boost::bind(&Foo::loop, this));
thread = new boost::thread([&](){ioService.run();});
}
void loop() {
if (shouldStop)
return;
timer.expires_at(timer.expires_at() + duration);
timer.async_wait(boost::bind(&Foo::loop, this));
}
};
int main() {
Foo foo;
foo.start();
wait(1000);
return 0;
}
您可以
g++ -std=c++11 -Wall -pedantic -pthread -g -I. -L/usr/local/lib -L/usr/lib -lboost_system -lpthread -lboost_thread threaded-io-service.cpp -o threaded-io-service
它工作正常編譯它,我敢肯定,前一陣子valgrind
沒有不抱怨。我最近更新,並valgrind-3.13.0
現在抱怨:
==22212== Memcheck, a memory error detector
==22212== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22212== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==22212== Command: ./threaded-io-service
==22212==
==22212== Thread 2:
==22212== Syscall param epoll_pwait(sigmask) points to unaddressable byte(s)
==22212== at 0x5E62326: epoll_pwait (in /usr/lib/libc-2.26.so)
==22212== by 0x111D50: boost::asio::detail::epoll_reactor::run(bool, boost::asio::detail::op_queue<boost::asio::detail::task_io_service_operation>&) (epoll_reactor.ipp:438)
==22212== by 0x112EE8: boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&) (task_io_service.ipp:356)
==22212== by 0x1129F0: boost::asio::detail::task_io_service::run(boost::system::error_code&) (task_io_service.ipp:149)
==22212== by 0x11322B: boost::asio::io_service::run() (io_service.ipp:59)
==22212== by 0x114C9C: Foo::start()::{lambda()#1}::operator()() const (threaded-io-service.cpp:35)
==22212== by 0x11E8FF: boost::detail::thread_data<Foo::start()::{lambda()#1}>::run() (thread.hpp:116)
==22212== by 0x526E44E: ??? (in /usr/lib/libboost_thread.so.1.65.1)
==22212== by 0x5045089: start_thread (in /usr/lib/libpthread-2.26.so)
==22212== by 0x5E621BE: clone (in /usr/lib/libc-2.26.so)
==22212== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==22212==
==22212==
==22212== HEAP SUMMARY:
==22212== in use at exit: 0 bytes in 0 blocks
==22212== total heap usage: 31 allocs, 31 frees, 81,701 bytes allocated
==22212==
==22212== All heap blocks were freed -- no leaks are possible
==22212==
==22212== For counts of detected and suppressed errors, rerun with: -v
==22212== ERROR SUMMARY: 112 errors from 1 contexts (suppressed: 0 from 0)
這是一個真正的錯誤?我發現this bug report人們認爲它可能是valgrind中的一個錯誤。
什麼版本gcc和boost? – sehe
啊鏈接的bug表示它可能取決於特定的glibc版本,在這種情況下,它可能更有效地命名分發/自定義回購 – sehe