Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I getting segfaults randomly?
    text
    copied!<p>This is quite strange for me, but I'm getting an unexpected and random segmentation fault when I launch my program. Some times it works, some times it crashes.. The debugger of Dev-C++ points me to a line of the file : stl_construct.h</p> <pre><code>/** * @if maint * Constructs an object in existing memory by invoking an allocated * object's constructor with an initializer. * @endif */ template&lt;typename _T1, typename _T2&gt; inline void _Construct(_T1* __p, const _T2&amp; __value) { // _GLIBCXX_RESOLVE_LIB_DEFECTS // 402. wrong new expression in [some_]allocator::construct -&gt; ::new(static_cast&lt;void*&gt;(__p)) _T1(__value); } </code></pre> <p>I'm using the STL extensively by the way.. What should i do to detect the origin of the segfault? Are there any tools that can help? What are the reasons that can lead to random crashes like this.</p> <h3>Edit:</h3> <p>My program counts around 5000 lines of code. I don't know what piece of code I have to show in order to get some help since I have no clue about the origin of the problem, all I got from the debugger is that it has to do with the STL.</p> <h3>Edit:</h3> <p>I moved to <code>Code::Blocks</code> now, here is the call stack:</p> <pre><code>#0 00464635 std::_Construct&lt;std::pair&lt;double const, int&gt;, std::pair&lt;double const, int&gt; &gt;(__p=0xb543e8, __value=@0x10) (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_construct.h:81) #1 00462306 std::_Rb_tree&lt;double, std::pair&lt;double const, int&gt;, std::_Select1st&lt;std::pair&lt;double const, int&gt; &gt;, std::less&lt;double&gt;, std::allocator&lt;std::pair&lt;double const, int&gt; &gt; &gt;::_M_create_node(this=0x406fe50, __x=@0x10) (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_tree.h:367) #2 00461DA7 std::_Rb_tree&lt;double, std::pair&lt;double const, int&gt;, std::_Select1st&lt;std::pair&lt;double const, int&gt; &gt;, std::less&lt;double&gt;, std::allocator&lt;std::pair&lt;double const, int&gt; &gt; &gt;::_M_clone_node(this=0x406fe50, __x=0x0) (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_tree.h:379) #3 004625C6 std::_Rb_tree&lt;double, std::pair&lt;double const, int&gt;, std::_Select1st&lt;std::pair&lt;double const, int&gt; &gt;, std::less&lt;double&gt;, std::allocator&lt;std::pair&lt;double const, int&gt; &gt; &gt;::_M_copy(this=0x406fe50, __x=0x0, __p=0x406fe54) (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_tree.h:1029) #4 00462A9D std::_Rb_tree&lt;double, std::pair&lt;double const, int&gt;, std::_Select1st&lt;std::pair&lt;double const, int&gt; &gt;, std::less&lt;double&gt;, std::allocator&lt;std::pair&lt;double const, int&gt; &gt; &gt;::_Rb_tree(this=0x406fe50, __x=@0xb59a7c) (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_tree.h:559) #5 0045A928 std::map&lt;double, int, std::less&lt;double&gt;, std::allocator&lt;std::pair&lt;double const, int&gt; &gt; &gt;::map(this=0x406fe50, __x=@0xb59a7c) (C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_map.h:166) #6 0040B7E2 VehicleManager::get_vehicles_distances(this=0xb59a50) (C:/Program Files/CodeBlocks/MinGW/projects/AHS/VehicleManager.cpp:232) #7 00407BDA Supervisor::IsMergeInstruction(id_vehicle=1) (C:/Program Files/CodeBlocks/MinGW/projects/AHS/Supervisor.cpp:77) #8 00408430 CheckingInstructionsThread(arg=0x476100) (C:/Program Files/CodeBlocks/MinGW/projects/AHS/Supervisor.cpp:264) #9 00413950 _glfwNewThread@4() (??:??) #10 75A24911 KERNEL32!AcquireSRWLockExclusive() (C:\Windows\system32\kernel32.dll:??) #11 00476100 std::__ioinit() (??:??) #12 0406FFD4 ??() (??:??) #13 76E5E4B6 ntdll!RtlInitializeNtUserPfn() (C:\Windows\system32\ntdll.dll:??) #14 00476100 std::__ioinit() (??:??) #15 70266582 ??() (??:??) #16 00000000 ??() (??:??) </code></pre> <p>A few more precisions :</p> <p>1/ It's a multi-threaded application. 2/ The method : get_vehicles_distances(); returns a map. 3/ It's possible that the map isn't initalised by the time when it's called by IsMergeInstruction(); </p> <h3>Edit:</h3> <p>Apparently the line that is causing the segfault is :</p> <pre><code>vehicles_distances_.erase(vehicles_distances_.begin(), vehicles_distances_.end()); </code></pre> <p>Where vehicles_distances_ is the Map. This line is a part of the method : VehicleManager::MoveAllVehicles();</p> <pre><code>void VehicleManager::MoveAllVehicles() { vehicles_distances_.erase(vehicles_distances_.begin(), vehicles_distances_.end()); vector&lt;Vehicle&gt;::iterator iter_end = VehicleManager::vehicles_.end(); for(vector&lt;Vehicle&gt;::iterator iter = VehicleManager::vehicles_.begin(); iter != iter_end; ++iter) { (*iter).MoveVehicle(); vehicles_distances_[(*iter).get_vec_vehicle_position().y] = (*iter).get_id_vehicle(); } } </code></pre> <p>What is wrong with that ?</p> <h3>Edit:</h3> <p>I tried to use map::clear(); as a replacement to map::erase(); but the same problem occurs!</p> <h3>Edit:</h3> <p>I think i get it... A thread is trying to make a use of vehicles_distances_ while it's cleared.. (?)</p> <h3>Edit:</h3> <p>Problem solved! So it was coming from the map::erase(); as expected. i bypassed the problem by creating another map variable where the pair <code>&lt;key, value&gt;</code> was inverted so i can update the map. (since the key that i need is the distance, and the distance isn't unique since it changes everytime but the id_vehicle is unique!). At the end i just took that map, inverted the <code>&lt;key, value&gt;</code> again and transferred it to the original map that can be redeclared in each cycle... </p> <p>Thanks everyone !</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload