-4

I am getting the execption error in the following piece of code. Any suggestions on what might be causing it ? Error : Invalid deque <T> subscript

typedef boost::shared_ptr<HistObj> shared_hist_def;
        typedef std::deque<shared_hist_def> vector_def;
        typedef boost::shared_ptr<vector_def> shared_vector_def;
        typedef boost::unordered_map<int,shared_vector_def> in_map_def;
        typedef boost::shared_ptr<in_map_def> shared_inner_map_def;

Domain::shared_hist_def& Domain::GetSpecificHistoricalTuple(const std::string& symb,const int& tframe,const int& val)
{
    Domain::shared_inner_map_def tshare = stat_History_base[symb];
    shared_vector_def tmp = tshare->at(tframe);
    try
    {
        Domain::shared_hist_def safe_tuple =  tmp->at(val);
        return safe_tuple;
    }
    catch (std::exception &ex)
    {
        std::string a = ex.what();
        __debugbreak();
    }
}

More information:

The above method is a static method. And the program is multithreaded.Any chance that this error occurs because multiple threads access it. I had that assumption but then think that function parameters above could never be the same at one time.

MistyD
  • 16,373
  • 40
  • 138
  • 240
  • just added to the post – MistyD May 08 '13 at 15:42
  • I don't see the definition of `stat_History_base` but it isn't local to the function, so probably will cause problems with multithreading. – Ben Voigt May 08 '13 at 15:44
  • shared_ptr of the map where values are shared_ptrs of deque of shared_ptrs. Are you serious? My brains are blown... – maverik May 08 '13 at 15:44
  • is `val` negative? that's what you seem to be using to index the deque at `Domain::shared_hist_def safe_tuple = tmp->at(val);` – Marc Claesen May 08 '13 at 15:45
  • @MarcClaesen what gives you the impression that val is negative ? – MistyD May 08 '13 at 15:48
  • @MistyD I suspected `val` to be the cause of your errors, e.g. it's either too large or negative. If that's not it, I don't know what the problem might be. – Marc Claesen May 08 '13 at 15:54

1 Answers1

0

Your val parameter seems to be too high (greater or equal to the number of elements in the deque), or maybe it's negative.

nullptr
  • 11,008
  • 1
  • 23
  • 18
  • I always check my val parameter. when the exception occurs. Also the container does have the objects at index of val. – MistyD May 08 '13 at 15:46
  • I am sorry, but how do you know the container does have the objects at index of `val` at the time of the exception? It looks like `at(val)` itself fails. Maybe checking/logging val and size just before calling `at` will show something. Also, if the program is multi-threaded, can there be any chance that one thread modifies the deque while another thread keeps the old index? (Running it single-threaded would show this.) – nullptr May 08 '13 at 15:55
  • yeah I suspect that is the problem. That is why I am adding locks to my code – MistyD May 08 '13 at 16:01