我用搜索,但我沒有找到答案滿足我......所以..這是代碼塊:分段故障
//VoteContainer.h
typedef uint32_t order_id_t;
typedef int driver_id_t;
class Vote {
public:
enum DriverVoteResponse {YES, NO, TIMEOUT};
struct DriverResponse {
driver_id_t driver_id;
time_t time;
DriverVoteResponse response;
};
Vote() : m_order_id(0), m_time_until(0) {};
Vote(order_id_t inOrderId, std::vector<driver_id_t> inPermittedDrivers, int inSeconds);
Vote(const Vote & other) : m_order_id(other.m_order_id), m_time_until(other.m_order_id) {
m_drivers_responses = other.m_drivers_responses;
m_permitted_drivers = other.m_permitted_drivers;
};
virtual ~Vote() {};
virtual void addDriverVote(driver_id_t inDriverId, DriverVoteResponse inDriverResponse);
virtual void getAppropriateDriverId(driver_id_t * inDriverId); //with min response time
private:
order_id_t m_order_id;
time_t m_time_until;
std::vector<DriverResponse> m_drivers_responses;
std::vector<driver_id_t> m_permitted_drivers;
};
class VoteContainer {
public:
VoteContainer() {};
virtual ~VoteContainer() {};
void registerVote(order_id_t inOrderId, std::vector<driver_id_t> inPermittedDrivers, int inSeconds);
private:
std::map<order_id_t, Vote> m_votes;
};
以及如何使用它:
m_votes.insert(std::make_pair(inOrderId, Vote(inOrderId, inPermittedDrivers, inSeconds)));
我試圖使用std ::地圖::找到(...)第一:
//VoteContainer.cpp
void VoteContainer::registerVote(order_id_t inOrderId, std::vector<driver_id_t> inPermittedDrivers, int inSeconds) {
m_votes.insert(std::make_pair(inOrderId, Vote(inOrderId, inPermittedDrivers, inSeconds)));
return;
};
我已經不管我做什麼段錯誤,但我有同樣的結果。回溯:
#0 0x41096a std::less<unsigned int>::operator() (this=0x407a59, [email protected], [email protected]) (/usr/include/c++/4.4/bits/stl_function.h:230)
#1 0x4105fb std::_Rb_tree<unsigned int, std::pair<unsigned int const, Vote>, std::_Select1st<std::pair<unsigned int const, Vote> >, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, Vote> > >::_M_insert_unique(this=0x407a59, __v=...) (/usr/include/c++/4.4/bits/stl_tree.h:1170)
#2 0x40fb25 std::map<unsigned int, Vote, std::less<unsigned int>, std::allocator<std::pair<unsigned int const, Vote> > >::insert(this=0x407a59, __x=...) (/usr/include/c++/4.4/bits/stl_map.h:500)
#3 0x40f06f VoteContainer::registerVote(this=0x407a51, inOrderId=1, inPermittedDrivers=..., inSeconds=32) (/home/user/workspace/src/merit_your_name/VoteContainer.cpp:81)
我想段落原因是參數[email protected]
。我不知道這是爲什麼!那時候m_votes地圖是空的。請,建議我...
由於馬修M.說,最可能的原因是未初始化值[email protected]
,但__y
有型的但不Vote
我試圖重寫代碼:
std::map<int, int> m_votes;
,這並沒有解決我的問題,因此,這個問題是不是在我的類型...
這裏是調用registerVote()
方法的代碼。
void OrderProcessor::processOrder(Order inOrder) {
//test!!!
driver_id_t driver_ids[] = {1,2};
std::vector<driver_id_t> drivers(driver_ids, driver_ids + sizeof(driver_ids)/sizeof(driver_id_t));
m_vote_container->registerVote(inOrder.getId(), drivers, 32);
for(size_t i = 0; i < drivers.size(); i++) {
std::cout << "sending vote to " << drivers[i] << " driver. " << std::endl;
std::cout << "send returns " << Arch::send_to_socket_nonblock((*m_drivers_connections)[drivers[i]], "<vote>1</vote>") << std::endl;
}
sleep(32);
Vote vote = m_vote_container->getVote(inOrder.getId());
vote.getAppropriateDriverId(driver_id);
m_vote_container->deleteVote(inOrder.getId());
};
昨天,我發現有問題不在我的代碼!我已將std :: map替換爲其他stl結構,但結果相同!我從代碼中刪除了stl,並且segfault在投票構造函數中,我刪除了這個類,並且segfault在我的代碼的其他stl結構中!那是什麼?請幫幫我。
我已經找到了我的問題,這不是這個代碼的原因。問題出在我以前的代碼中。感謝大家參加這次討論。
+1,或者一個已經被銷燬的'VoteContainer'(你是否將指針保存爲自動的'VoteContainers'?你可能刪除了一個'VoteContainer'並且稍後嘗試使用這個指針on?) – 2010-09-02 08:26:41
'VoteContainer'不能被初始化,因爲它首先初始化。我沒有動態分配'VoteContainer',我在程序的初始階段靜態地分配它。它首先是程序的多線程部分,但我以單線方式重寫,所以所有對象都必須存在。 – milo 2010-09-02 08:47:08