I used a search, but I did not find an answer satisfying me ... so .. this is a piece of code:
//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; };
and how i use it:
//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; };
I have segfault, no matter what I do:
m_votes.insert(std::make_pair(inOrderId, Vote(inOrderId, inPermittedDrivers, inSeconds)));
At first I tried to use std :: map :: find (...), but I have the same result. trace:
#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)
I believe the reason for segfault is the __y=@0x758948f87d894905 argument. I have no idea why this is! at this moment the m_votes card is empty. please suggest me ...
According to Mattiu M., the most likely reason is the uninitialized value __y=@0x758948f87d894905 , but __y is of type order_id_t but not Vote
I tried to rewrite the code:
std::map<int, int> m_votes;
and this did not solve my problem, so the problem is not in my types ...
here is the code calling the registerVote() method.
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()); };
Yesterday I found out that the problem is not in my code! I replaced std :: map with other stl structures, but the result was the same! I removed stl from this code, and segfault was in the Vote constructor, I deleted this class, and segfault was in other stl structures of my code! what is it? help me please.
I found out the cause of my problem, not this code. The problem was in my previous code. Thank you all for participating in this discussion.