Segmentation fault in std::map::insert(…)

hello, i've used search but i didn't find answer satisfying me... so.. this is chunk of code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 //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:
1
2
3
4
5
//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 in regardless of what i do:
m_votes.insert(std::make_pair(inOrderId, Vote(inOrderId, inPermittedDrivers, inSeconds)));
i've tried to use std::map::find(...) first, but i have same result. backtrace:
1
2
3
4
#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 suppose the reason of segfault is argument __y=@0x758948f87d894905. i have no idea why this is! at that moment m_votes map is empty. please, suggest me...
Try below see how.

 
m_votes.insert(std::make_pair<order_id_t, Vote>(inOrderId,  Vote(inOrderId, inPermittedDrivers, inSeconds)));


When I use C++ STL libraries, I always make it a point to supply the datatype in the templates. make_pair is templatized function so for me I put in the datatypes. Don't know if this is the reason for your error though.
Last edited on
I don't know why you have a crash, but consider these points.

1. You don't need to define/implement a copy constructor, the default is sufficient.
2. Vote doesn't need a virtual destructor (as it stands), and again, the default is sufficient.
3. VoteContainer has a map of Vodes, so it must copy Vote entries in.
4. registerVote passes a vector by value, and a Vote constructor takes a vector by value.
5. Have you thought about the number of vector copies involved in calling registerVote?
i've found out there is problem not in my code! i've replaced std::map to other structures but the result was same! i've deleted stl from that code and segfault was in Vote constructor, i've deleted this class and segfault was in other stl structures of my code! what is that? help me please.
I have no idea what's gone before. Can you reproduce the problem using just the code you've posted?
i've found out the reason of my problem, that isn't this code. problem was in my previous code. thank you all for participating this discussion.
Topic archived. No new replies allowed.