2013-03-16 88 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