Повреждение структуры данных в c ++ - PullRequest
0 голосов
/ 03 марта 2020

У меня возникла проблема с доступом к секунде boost::unordered_map.

. В ходе расследования я обнаружил причину, по которой вероятность того, что функция вставки могла испортить это.

I иметь карту <std::string, struct securityId_symPosition>, где в функции создается локальный struct s_securityId_symPosition и его копия вставляется в данную карту с помощью функции make_pair.

Поскольку я работаю над старым кодом Я могу видеть, что другая та же карта другой структуры заполняется и используется соответственно, но когда данная карта используется для использования второй, она дает Segmentation fault.

Пример mininum моего способа реализация:

#include <boost/unordered_map.hpp>

struct test {
    int var1;
    char var2;
    bool var3;
    test():var1(0),var2('a'),var3(false){}
};

int main()
{
    typedef boost::unordered_map<int, test> map;
    map * nTest;

    nTest = new map();

    {
        for(int i = 0;i <=5; i++)
        {
            test t1;
            t1.var1 = i;
            nTest->insert(std::make_pair(i,t1));
        }
    }
    for(int i =0;i <=5; i++)
    {
        nTest->find(i)->second.var1; //this crashes
    }
}

Рабочий процесс моей программы:

  1. Заполнить карту в функции 1
  2. Использовать карту в функции 2

ПРИМЕЧАНИЕ. Я не трогал карту со вставки в 1, чтобы найти в 2

Печать GDB для секунды карты:

IN FUNC1:
 stockName_ = {
    static npos = <optimized out>,
    _M_dataplus = {
      <std::allocator<char>> = {
        <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
---Type <return> to continue, or q <return> to quit---
      members of std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider:
      _M_p = 0x7fffa40fb588 "ADANIPORTS27FEBFUT"
IdBuyLots_ = 0,
  IdBuyPrice_ = 0,
  IdSellLots_ = 0,
  IdSellPrice_ = 0,
  IdBuyQty_ = 0,
  IdSellQty_ = 0,
  IdBuyValue_ = 0,
  IdSellValue_ = 0,
  IntraDayCarriedLots_ = 0,
  IntraDayCarriedPrice_ = 0,
  IntraDayCarriedSpread_ = 0,
  IntraDayCarriedSpreadLots_ = 0,
  pBuyLots_ = 0,
  pBuyPrice_ = 0,
  pSellLots_ = 0,
  pSellPrice_ = 0,
  pBuyQty_ = 0,
  pSellQty_ = 0,
  pBuyValue_ = 0,
  pSellValue_ = 0,
  pCarriedLots_ = 0,
  pCarriedPrice_ = 0,
  pCarriedSpread_ = 0,
  pCarriedSpreadLots_ = 0,
  avgPrice_ = 0,
  TotalLotstobeCarried_ = 0,
  DiscountedavgPrice_ = 0,
  avgCarriedspread_ = 0,
  avgCarriedspreadLots_ = 0,
  avgCarriedspreadDiscounted_ = 0,
  numofDays_ = 1,
  availableLots_ = 0,
  Positional_Lots_To_Exit_ = 0,
  closePrice_ = 0


In Func2:
  stockName_ = {
    static npos = <optimized out>,
    _M_dataplus = {
      <std::allocator<char>> = {
        <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>},
---Type <return> to continue, or q <return> to quit---
      members of std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider:
      _M_p = 0x7fffa40fb468 "NSECM,15083,ADANIPORTS,20200227,ADANIPORTS,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"
    }

  IdBuyLots_ = 140737267093296,
  IdBuyPrice_ = 2.9643938750474793e-323,
  IdSellLots_ = 0,
  IdSellPrice_ = 2.0246810166574283e-320,
  IdBuyQty_ = 140733193388034,
  IdSellQty_ = 0,
  IdBuyValue_ = 0,
  IdSellValue_ = 0,
  IntraDayCarriedLots_ = 0,
  IntraDayCarriedPrice_ = 0,
  IntraDayCarriedSpread_ = 0,
  IntraDayCarriedSpreadLots_ = 0,
  pBuyLots_ = 0,
  pBuyPrice_ = 0,
  pSellLots_ = 0,
  pSellPrice_ = 0,
  pBuyQty_ = 0,
  pSellQty_ = 0,
  pBuyValue_ = 0,
  pSellValue_ = 0,
  pCarriedLots_ = 0,
  pCarriedPrice_ = 0,
  pCarriedSpread_ = 0,
  pCarriedSpreadLots_ = 0,
  avgPrice_ = 6.953143608256303e-310,
  TotalLotstobeCarried_ = 140736513561544,
  DiscountedavgPrice_ = 6.9533448814870815e-310,
  avgCarriedspread_ = 0,
  avgCarriedspreadLots_ = 140736486047744,
  avgCarriedspreadDiscounted_ = 6.9533076466158444e-310,
  numofDays_ = -221152928,
  availableLots_ = 140737267202288,
  Positional_Lots_To_Exit_ = 140737267202304,
  closePrice_ = 140737267109048

Единственное отличие, которое я вижу между картой, состоит в том, что работа, а это не так, другой сравнительно огромен, поэтому; Может ли это быть проблемой?

Valgrind Records

9563 ==8228== 672 bytes in 2 blocks are possibly lost in loss record 108,795 of 112,718
9564 ==8228==    at 0x15894203: operator new(unsigned long) (vg_replace_malloc.c:334)
9565 ==8228==    by 0x75D7F434: __gnu_cxx::new_allocator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::Bidding::securityId_symPosition> > >:: 
     allocate(unsigned long, void const*) (new_allocator.h:104)
9566 ==8228==    by 0x75D7DC89: boost::unordered::detail::allocator_traits<std::allocator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::      
     Bidding::securityId_symPosition> > > >::allocate(std::allocator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::Bidding::                  
     securityId_symPosition> > >&, unsigned long) (allocate.hpp:546)
9567 ==8228==    by 0x75D7B4CC: boost::unordered::detail::node_constructor<std::allocator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::      
     Bidding::securityId_symPosition> > > >::construct() (buckets.hpp:407)
9568 ==8228==    by 0x75D7840E: void boost::unordered::detail::node_constructor<std::allocator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA:: 
     Bidding::securityId_symPosition> > > >::construct_with_value<std::string&, SBA::Bidding::securityId_symPosition&>(std::string&, SBA::Bidding::                  
     securityId_symPosition&) (buckets.hpp:347)
9569 ==8228==    by 0x75D74971: std::pair<boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::Bidding:: 
     securityId_symPosition> > >, bool> boost::unordered::detail::table_impl<boost::unordered::detail::map<std::allocator<std::pair<std::string const, SBA::Bidding::
     securityId_symPosition> >, std::string, SBA::Bidding::securityId_symPosition, boost::hash<std::string>, std::equal_to<std::string> > >::emplace_impl<std::      
     string&, SBA::Bidding::securityId_symPosition&>(std::string const&, std::string&, SBA::Bidding::securityId_symPosition&) (unique.hpp:409)
9570 ==8228==    by 0x75D71205: std::pair<boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::Bidding:: 
     securityId_symPosition> > >, bool> boost::unordered::detail::table_impl<boost::unordered::detail::map<std::allocator<std::pair<std::string const, SBA::Bidding::
     securityId_symPosition> >, std::string, SBA::Bidding::securityId_symPosition, boost::hash<std::string>, std::equal_to<std::string> > >::emplace<std::string&,   
     SBA::Bidding::securityId_symPosition&>(std::string&, SBA::Bidding::securityId_symPosition&) (unique.hpp:380)
9571 ==8228==    by 0x75D6EA74: std::pair<boost::unordered::iterator_detail::iterator<boost::unordered::detail::ptr_node<std::pair<std::string const, SBA::Bidding:: 
     securityId_symPosition> > >, bool> boost::unordered::unordered_map<std::string, SBA::Bidding::securityId_symPosition, boost::hash<std::string>, std::           
     equal_to<std::string>, std::allocator<std::pair<std::string const, SBA::Bidding::securityId_symPosition> > >::emplace<std::string&, SBA::Bidding::              
     securityId_symPosition&>(std::string&, SBA::Bidding::securityId_symPosition&) (unordered_map.hpp:238)
9572 ==8228==    by 0x75D53CF6: SBA::Bidding::P_init(char const*) (cashfut_trade.cpp:732)
9573 ==8228==    by 0x75D4C6B5: SBA::Bidding::Bidding(API2::StrategyParameters*) (cashfut_trade.cpp:125)
9574 ==8228==    by 0x75D629D2: SBA::Bidding::bidDriver(void*) (cashfut_trade.cpp:2179)
9575 ==8228==    by 0x75D3E263: getDriver (externalInterface.cpp:16)
9576 ==8228==
...