std::map::insert(...) 中的分段错误

发布于 2024-09-17 08:12:29 字数 4379 浏览 11 评论 0原文

我使用过搜索,但没有找到令我满意的答案...所以..这是代码块:

 //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(...) 首先,但我有相同的结果。 backtrace:

#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的类型为order_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结构中!那是什么?请帮助我。

我已经找到了问题的原因,那不是这段代码。问题出在我之前的代码中。感谢大家参与这次讨论。

i've used search but i didn't find answer satisfying me... so.. this is chunk 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 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:

#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...

As Matthieu M. says the most probable reason is uninitialized value __y=@0x758948f87d894905, but __y has type of order_id_t but not Vote

i've tried to rewrite code :

std::map<int, int> m_votes;

and this didn't solve my problem, therefore, the problem isn't in my types...

here is the code invoking 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 there is problem not in my code! i've replaced std::map to other stl structures but the result was the 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'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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稀香 2024-09-24 08:12:29

据我所知,我敢说真正重要的代码丢失了。

如前所述: this=0x407a59, __x=@0x7fffffff0b50, __y=@0x758948f87d894905 非常奇怪,地址相差太远,所以我们可以假设其中一个(至少)只是未初始化。为了我自己的理智,我假设您的 std::map 实现没有错误。

我的直觉是寻找未初始化的地图,以及未初始化的 VoteContainer 对象。您是否有一些在调用 registerVote 之前忘记分配的 VoteContainer*

From what I can see, I would venture that the really important code is missing.

As noted: this=0x407a59, __x=@0x7fffffff0b50, __y=@0x758948f87d894905 is quite strange, the addresses are way too apart, so we can suppose that one of them (at least) is simply uninitialized. And for my own sanity I'll suppose that your implementation of std::map is not buggy.

My gut feeling would be to look for an uninitialized map, and therefore, and uninitialized VoteContainer object. Would you have some VoteContainer* that you forgot to allocate before invoking registerVote on it ?

孤芳又自赏 2024-09-24 08:12:29

如果你在Linux下工作,我可以推荐valgrind工具,它应该可以帮助你找到问题所在

If you working under Linux, I can recommend valgrind tool, it should help you to find where is the problem

↙温凉少女 2024-09-24 08:12:29

两个猜测:

  1. 你能展示调用registerVote()方法的代码吗? (我怀疑这可能会提供一些线索。)

  2. 你能把

    m_votes.clear();

VoteContainer 构造函数中?

Two guesses:

  1. Can you show the code calling the registerVote() method? (I suspect that may give some clue.)

  2. Can you put

    m_votes.clear();

in the VoteContainer constructor?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文