簡體   English   中英

取決於構造函數調用,“銷毀地圖/設置迭代器不兼容”

[英]“map/set iterators incompatible” on map destruction, dependent on constructor call

我正在構建測試,但在Visual Studio 2013、2013年11月編譯器,Debug構建,32位版本中遇到了調試斷言問題:
“表達式:映射/設置迭代器不兼容”
“標准C ++庫無效參數” && 0

當將函數范圍保留在調用本地對象的析構函數的函數范圍內時,將發生錯誤。

奇怪的是,我可以通過將代碼語法更改為我認為應該等效的方式來修復它。 更准確地說,這是損壞的代碼:

Kinetics kinetics;
kinetics.components.emplace_back(Kinetics::Component{ 0, Kinetics::Params{}, 1, 1 });
kinetics.components[0].params.push_back(Kinetics::Param{
    "",
    vector<size_t>{ 0, 1 },
    map < size_t, vector<size_t> >{{0, vector<size_t>{ 0 }}},
    vector<size_t>{ 1 } 
});
kinetics.components[0].params.push_back(Kinetics::Param{
    "",
    vector<size_t>{ 0, 1 },
    map < size_t, vector<size_t> >{{0, vector<size_t>{ 1 }}},
    vector<size_t>{ 0 } 
});

雖然這是工作說明,但我只將map構造函數調用移出了Param構造函數調用

Kinetics kinetics;
kinetics.components.emplace_back(Kinetics::Component{ 0, Kinetics::Params{}, 1, 1 });
map < size_t, vector<size_t> > requirements = map < size_t, vector<size_t> >{{0, vector<size_t>{ 0 }}};
kinetics.components[0].params.push_back(Kinetics::Param{
    "",
    vector<size_t>{ 0, 1 },
    move(requirements),
    vector<size_t>{ 1 } 
});
requirements = map < size_t, vector<size_t> >{{0, vector<size_t>{ 1 }}};
kinetics.components[0].params.push_back(Kinetics::Param{
    "",
    vector<size_t>{ 0, 1 },
    move(requirements),
    vector<size_t>{ 0 } 
});

在我看來,以上兩個應該是等效的-每次我使用初始化列表(從臨時或移動中獲取右值引用)並將其存儲在其中時,我不知道為什么會出錯,錯誤的類型也使我感到困惑。

為了完整起見,我還添加了Kinetics對象的定義:

#define NO_COPY(TypeName) \
TypeName() = default;  \
TypeName(TypeName && ) = default;  \
TypeName& operator=(TypeName && ) = default; \
TypeName(const TypeName & ) = delete; \
TypeName& operator=(const TypeName & ) = delete; 

struct Kinetics {
    NO_COPY(Kinetics)

    struct Param {
        string context; ///< String representation of the context.
        vector<size_t> targets; ///< Towards which level this context may regulate.
        map<size_t, vector<size_t>> requirements; ///< vector<size_t> of the source components this param is relevant to, the vector<size_t> are sorted.

        vector<size_t> target_in_subcolor; ///< List of values from different subparametrizations for this specie, share indices between params.
    };
    using Params = vector < Param > ;

    struct Component {
        size_t ID; ///< ID of the component, shared with the model
        Params params; ///< Vector of parameters, sorted lexicographically by the context.
        size_t col_count; ///< Number of subcolors for this specie.
        size_t step_size; ///< In the context of the whole parametrization space, how may changes occur between a subcolor of this specie changes?
    };

    vector<Component> components; ///< Species shared with the model, sorted lexicographically. 
};

最后是調用堆棧:

tremppi_test.exe!std :: _ Tree_const_iterator>>>>> ::: operator ==(const std :: _ Tree_const_iterator>>>>&_Right)第328行C ++ tremppi_test.exe!std :: _ Tree>,std :: less, std :: allocator>>>,0>> :: erase(std :: _ Tree_const_iterator>>>>> _First,std :: _ Tree_const_iterator>>>>>> _>)1512 C ++ tremppi_test.exe!std :: _ Tree>, std :: less,std :: allocator>>>,0>> :: _ Tidy()第2230行C ++ tremppi_test.exe!std :: _ Tree>,std :: less,std :: allocator>>>,0>> ::〜_Tree>,std :: less,std :: allocator>>>,0>>()第1193行C ++ tremppi_test.exe!std :: map>,std :: less,std :: allocator>>>> ::〜map>,std :: less,std :: allocator>>>>()C ++ tremppi_test.exe!Kinetics :: Param ::〜Param()C ++ tremppi_test.exe!Kinetics :: Param :: scalar deleting destructor'(unsigned int) C++ tremppi_test.exe!std::allocator<Kinetics::Param>::destroy<Kinetics::Param>(Kinetics::Param * _Ptr) Line 608 C++ tremppi_test.exe!std::allocator_traits<std::allocator<Kinetics::Param> >::destroy<Kinetics::Param>(std::allocator<Kinetics::Param> & _Al, Kinetics::Param * _Ptr) Line 731 C++ tremppi_test.exe!std::_Wrap_alloc<std::allocator<Kinetics::Param> >::destroy<Kinetics::Param>(Kinetics::Param * _Ptr) Line 879 C++ tremppi_test.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Kinetics::Param> > >(Kinetics::Param * _First, Kinetics::Param * _Last, std::_Wrap_alloc<std::allocator<Kinetics::Param> > & _Al, std::_Nonscalar_ptr_iterator_tag __formal) Line 82 C++ tremppi_test.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Kinetics::Param> > >(Kinetics::Param * _First, Kinetics::Param * _Last, std::_Wrap_alloc<std::allocator<Kinetics::Param> > & _Al) Line 96 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::_Destroy(Kinetics::Param * _First, Kinetics::Param * _Last) Line 1567 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::_Tidy() Line 1628 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::~vector<Kinetics::Param,std::allocator<Kinetics::Param> >() Line 946 C++ tremppi_test.exe!Kinetics::Component::~Component() C++ tremppi_test.exe!Kinetics::Component:: scalar deleting destructor'(unsigned int) C++ tremppi_test.exe!std::allocator<Kinetics::Param>::destroy<Kinetics::Param>(Kinetics::Param * _Ptr) Line 608 C++ tremppi_test.exe!std::allocator_traits<std::allocator<Kinetics::Param> >::destroy<Kinetics::Param>(std::allocator<Kinetics::Param> & _Al, Kinetics::Param * _Ptr) Line 731 C++ tremppi_test.exe!std::_Wrap_alloc<std::allocator<Kinetics::Param> >::destroy<Kinetics::Param>(Kinetics::Param * _Ptr) Line 879 C++ tremppi_test.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Kinetics::Param> > >(Kinetics::Param * _First, Kinetics::Param * _Last, std::_Wrap_alloc<std::allocator<Kinetics::Param> > & _Al, std::_Nonscalar_ptr_iterator_tag __formal) Line 82 C++ tremppi_test.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Kinetics::Param> > >(Kinetics::Param * _First, Kinetics::Param * _Last, std::_Wrap_alloc<std::allocator<Kinetics::Param> > & _Al) Line 96 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::_Destroy(Kinetics::Param * _First, Kinetics::Param * _Last) Line 1567 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::_Tidy() Line 1628 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::~vector<Kinetics::Param,std::allocator<Kinetics::Param> >() Line 946 C++ tremppi_test.exe!Kinetics::Component::~Component() C++ tremppi_test.exe!Kinetics::Component:: scalar deleting destructor'(unsigned int) C++ tremppi_test.exe!std::allocator<Kinetics::Param>::destroy<Kinetics::Param>(Kinetics::Param * _Ptr) Line 608 C++ tremppi_test.exe!std::allocator_traits<std::allocator<Kinetics::Param> >::destroy<Kinetics::Param>(std::allocator<Kinetics::Param> & _Al, Kinetics::Param * _Ptr) Line 731 C++ tremppi_test.exe!std::_Wrap_alloc<std::allocator<Kinetics::Param> >::destroy<Kinetics::Param>(Kinetics::Param * _Ptr) Line 879 C++ tremppi_test.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Kinetics::Param> > >(Kinetics::Param * _First, Kinetics::Param * _Last, std::_Wrap_alloc<std::allocator<Kinetics::Param> > & _Al, std::_Nonscalar_ptr_iterator_tag __formal) Line 82 C++ tremppi_test.exe!std::_Destroy_range<std::_Wrap_alloc<std::allocator<Kinetics::Param> > >(Kinetics::Param * _First, Kinetics::Param * _Last, std::_Wrap_alloc<std::allocator<Kinetics::Param> > & _Al) Line 96 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::_Destroy(Kinetics::Param * _First, Kinetics::Param * _Last) Line 1567 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::_Tidy() Line 1628 C++ tremppi_test.exe!std::vector<Kinetics::Param,std::allocator<Kinetics::Param> >::~vector<Kinetics::Param,std::allocator<Kinetics::Param> >() Line 946 C++ tremppi_test.exe!Kinetics::Component::~Component() C++ tremppi_test.exe!Kinetics::Component::標量刪除析構函數'(unsigned int)C ++ tremppi_test.exe!std :: allocator :: destroy(Kinetics :: Component * _Ptr)行608 C ++ tremppi_test.exe!std :: allocator_traits> :: destroy(std :: allocator&_Al,Kinetics :: Component * _Ptr)行731 C ++ tremppi_test.exe!std :: _ Wrap_alloc> :: destroy(Kinetics :: Component * _Ptr)行879 C ++ tremppi_test.exe!std :: _ Destroy_range>>(Kinetics :: Component * _First,Kinetics :: Component * _Last, std :: _ Wrap_alloc>&_Al,std :: __ Nonscalar_ptr_iterator_tag __formal)82行C ++ tremppi_test.exe!std :: _ Destroy_range>>(運動學:: Component * _First,Kinetics :: Component * _Last,std :: __ Wrap_alloc>&_Al 96行C ++ tremppi_test.exe!std :: vector> :: _ Destroy(運動學:: Component * _First,Kinetics :: Component * _Last)1567行C ++ tremppi_test.exe!std :: vector> :: _ Tidy()1628 C ++ tremppi_test.exe!std :: vector> ::〜vector>() 946行C ++ tremppi_test.exe!Kinetics ::〜Kinetics()C ++ tremppi_test.exe!tremppi_validate(int argc,char * * argv)第153行C ++ tremppi_test.exe!basic_validate_test()19行C ++ tremppi_test.exe!CoreTest_AllProgramodys_Test: ()第13行C ++ [外部代碼] tremppi_test.exe!RUN_ALL_TESTS()第2289行C ++ tremppi_test.exe!tremppi_test(int argc,char * * argv)第19行C ++ tremppi_test.exe!main(int argc,char * * argv)第8行C ++ [外部代碼] [以下框架可能不正確和/或丟失,沒有為kernel32.dll加載任何符號]

這(很可能)被列為已關閉的錯誤#807419 https://connect.microsoft.com/VisualStudio/feedback/details/807419/initializer-lists-leaking-memory ,並且可能僅與所涉及的編譯器版本有關。 使用更新的VS13(不會編譯上面的代碼)或VS14應該可以避免此類問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM