我用过搜索,但没有找到让我满意的答案……所以……这是一段代码:
//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;
};
以及我如何使用它:
//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;
};
无论我做什么,我都有段错误:
m_votes.insert(std::make_pair(inOrderId, Vote(inOrderId, inPermittedDrivers, inSeconds)));
我尝试先使用 std::map::find(...) ,但结果相同。回溯:
#0 0x41096a std::less<unsigned int>::operator() (this=0x407a59, __x=@0x7fffffff0b50, __y=@0x758948f87d894905) (/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)
我想段错误的原因是争论__y=@0x758948f87d894905
。我不知道这是为什么!那时 m_votes 地图是空的。请给我建议...
正如 Matthieu M. 所说,最可能的原因是未初始化的值__y=@0x758948f87d894905
,但__y
类型为 oforder_id_t
但不是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 在 Vote 构造函数中,我已经删除了这个类并且 segfault 在我的代码的其他 stl 结构中!那是什么?请帮帮我。
我找到了我的问题的原因,那不是这段代码。问题出在我之前的代码中。感谢大家参与本次讨论。