0

I am using Boost multi index map container to support cache in my application. But during insertion I seeing a application crash. My code looks somewhat like this

class MultiIndexMap
{
public:
    MultiIndexMap();
    ~MultiIndexMap();

    string  _uid;
    string  _name1;
    string  _name2;
};

/**
 * Tags for accessing corresponding indices
 */
class uid{};
class name1{};
class name2{};

/** 
 *   - a unique index sorted by MultiIndexMap::_uid,
 *   - a non-unique index sorted by MultiIndexMap::name1,
 *   - a non-unique index sorted by MultiIndexMap::name2.
 */

typedef multi_index_container<
    MultiIndexMap, 
    indexed_by< 
    ordered_unique< tag<uid>,  BOOST_MULTI_INDEX_MEMBER(MultiIndexMap,string,_uid)>,
    ordered_non_unique< tag<name1>,BOOST_MULTI_INDEX_MEMBER(MultiIndexMap,string,_name2)>,
    ordered_non_unique< tag<name2>, BOOST_MULTI_INDEX_MEMBER(MultiIndexMap,string,_name2)> > > MultiIndexMap_set;

void Insert(MultiIndexMap_set& s, MultiIndexMap& entry)
{
    MultiIndexMap_set::nth_index<0>::type& uid_index= s.get<0>();
    
    uid_index.insert(entry); <<-- crashing at this line
}

What could be the reason for this crash.

enter image description here

This the location and the crash what I am getting in my application.

Community
  • 1
  • 1
Apoorva sahay
  • 1,900
  • 3
  • 28
  • 45

1 Answers1

1

We will have to assume that you have Undefined Behaviour.

The following test works across MSVC (VS2013), GCC and Clang: Live On Coliru

#if !defined(NDEBUG)
#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
#endif

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

using boost::multi_index_container;
namespace bmi = boost::multi_index;

class MultiIndexMap
{
public:
    MultiIndexMap() {}
    ~MultiIndexMap() {}

    std::string  _uid;
    std::string  _name1;
    std::string  _name2;
};

/**
 * Tags for accessing corresponding indices
 */
class uid{};
class name1{};
class name2{};

/**
 *   - a unique index sorted by MultiIndexMap::_uid,
 *   - a non-unique index sorted by MultiIndexMap::name1,
 *   - a non-unique index sorted by MultiIndexMap::name2.
 */

typedef multi_index_container<
    MultiIndexMap,
    bmi::indexed_by<
        bmi::ordered_unique<bmi::tag<uid>, BOOST_MULTI_INDEX_MEMBER(MultiIndexMap, std::string, _uid)>,
        bmi::ordered_non_unique<bmi::tag<name1>, BOOST_MULTI_INDEX_MEMBER(MultiIndexMap, std::string, _name2)>,
        bmi::ordered_non_unique<bmi::tag<name2>, BOOST_MULTI_INDEX_MEMBER(MultiIndexMap, std::string, _name2)>
    > > MultiIndexMap_set;

void Insert(MultiIndexMap_set& s, MultiIndexMap& entry)
{
    MultiIndexMap_set::nth_index<0>::type& uid_index = s.get<0>();
    uid_index.insert(entry); //<< --crashing at this line
}

int main()
{
    MultiIndexMap_set x;

    MultiIndexMap m;
    m._uid = "uid_1";
    m._name1 = "name1_1";
    m._name2 = "name2_1";
    Insert(x, m);

    m._uid = "uid_2";
    m._name1 = "name1_2";
    m._name2 = "name2_2";
    Insert(x, m);

    m._uid = "uid_3";
    m._name1 = "name1_3";
    m._name2 = "name2_3";
    Insert(x, m);

    for (auto& i : x)
        std::cout << i._uid << "\n";
}

Now, what usually triggers Undefined Behaviour when using MultiIndex is when you violate the container invariants (e.g. by modifying parts of the index without using the appropriate modify calls)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Can you pin point me the error which I was making. I am not able to make out the difference. – Apoorva sahay Jun 12 '14 at 15:15
  • 1
    No, we can't, because you only show the code that is obviously correct. You will have to either show the real code or investigate for yourself. FWIW the difference with my code is obviously that I made the [sample complete, and self contained (**sscce**)](http://sscce.org). This is a very good tool to isolate the problem yourself: **[Solve your problem by _almost_ asking on StackOverflow](http://blog.jerryorr.com/2014/04/solve-your-problem-by-almost-asking.html)** – sehe Jun 12 '14 at 22:05
  • Sorry my mistake @sehe I found out the issue. I was creating an object on heap and trying to pass it to my multiindex cache. this resulted in crash. Thanks for you help. – Apoorva sahay Jun 13 '14 at 07:32