UPDATE:如果我使用的libC++编译,然后我发现了错误,但是当我改变了编译器与libstdC++(GNU C++标准库),程序将不显示任何错误运行。升压线坏访问
我从升压网站尝试一些示例代码,当运行这段代码不知何故,我得到一个坏访问错误。代码运行良好,直到它调用它看起来的析构函数。
-lmysqlclient
-lm
-lz
-lboost_date_time
-lboost_system
-lboost_filesystem
链接。
有没有人知道我做错了什么?
#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io)
: strand_(io),
timer1_(io, boost::posix_time::seconds(1)),
timer2_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print1()
{
if (count_ < 10)
{
std::cout << "Timer 1: " << count_ << "\n";
++count_;
timer1_.expires_at(timer1_.expires_at() + boost::posix_time::seconds(1));
timer1_.async_wait(strand_.wrap(boost::bind(&printer::print1, this)));
}
}
void print2()
{
if (count_ < 10)
{
std::cout << "Timer 2: " << count_ << "\n";
++count_;
timer2_.expires_at(timer2_.expires_at() + boost::posix_time::seconds(1));
timer2_.async_wait(strand_.wrap(boost::bind(&printer::print2, this)));
}
}
private:
boost::asio::strand strand_;
boost::asio::deadline_timer timer1_;
boost::asio::deadline_timer timer2_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
boost::thread t(boost::bind(&boost::asio::io_service::run, &io));
io.run();
t.join();
return 0;
}
林不知道这是否是一个堆栈跟踪...
当我在VS 2010中执行此操作时,没有访问冲突。奇。 – 2013-03-16 09:52:12
代码中没有看到任何错误。当您仅链接到所需的最小子库时,是否还存在访问违规? – 2013-03-16 14:16:20
附上一个调试器,并在程序收到BUS或SEGV信号时向我们显示堆栈跟踪信息。 – 2013-03-16 14:40:25