2013-03-16 89 views
0

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; 
} 

stack trace

Original image

林不知道这是否是一个堆栈跟踪...

+0

当我在VS 2010中执行此操作时,没有访问冲突。奇。 – 2013-03-16 09:52:12

+1

代码中没有看到任何错误。当您仅链接到所需的最小子库时,是否还存在访问违规? – 2013-03-16 14:16:20

+0

附上一个调试器,并在程序收到BUS或SEGV信号时向我们显示堆栈跟踪信息。 – 2013-03-16 14:40:25

回答

1

我建立并使用gcc 4.2.1没有问题

跑到你在我的Mac代码
samm$ g++ --version 
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00) 
Copyright (C) 2007 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
samm$ ./a.out 
Timer 1: 0 
Timer 2: 1 
Timer 1: 2 
Timer 2: 3 
Timer 1: 4 
Timer 2: 5 
Timer 1: 6 
Timer 2: 7 
Timer 1: 8 
Timer 2: 9 
Final count is 10 
samm$ 

我用这个相同的工具链来建立d我的增强库

samm$ grep BOOST_LIB_VERSION /opt/local/include/boost/version.hpp 
// BOOST_LIB_VERSION must be defined to be the same as BOOST_VERSION 
#define BOOST_LIB_VERSION "1_53" 
samm$ 

我建议使用相同的工具链来构建boost和您的应用程序。

+0

我刚刚在boost站点使用了“tutorial”来构建boost,我下载并安装了一个新的Xcode和命令行工具,然后我下载了boost 1_53_0并运行了'sudo ./bootstrap.sh --prefix = usr/local /',并用'./b2 install'来安装它。我没有改变任何东西,我在这里输了。我可以使用其他的boost函数,但只有“线程”给我一个错误,我真的要使用升压,使计时器... – Robert 2013-03-20 22:47:15

+0

'$ G ++ --version 的i686-苹果darwin11-LLVM-G ++ - 4.2(GCC)4.2.1(基于苹果公司建立5658)(LLVM构建2336.11。 00) 版权所有(C)2007自由软件基金会,公司 这是自由软件;请参阅复制条件的来源。有没有 保修;甚至不是针对特定PURPOSE.'适销性或 '$ grep的BOOST_LIB_VERSION /usr/local/include_64bit/boost/version.hpp // BOOST_LIB_VERSION必须被定义为同BOOST_VERSION 的#define BOOST_LIB_VERSION“1_53” ' – Robert 2013-03-20 22:53:47