Why do I get segfaults randomly?

This is rather strange for me, but I get an unexpected and random segmentation error when I run my program. Several times it works, sometimes it falls. The Dev-C ++ debugger points me to the line of the file: stl_construct.h

/**
   * @if maint
   * Constructs an object in existing memory by invoking an allocated
   * object constructor with an initializer.
   * @endif
   */
  template<typename _T1, typename _T2>
    inline void
    _Construct(_T1* __p, const _T2& __value)
    {
      // _GLIBCXX_RESOLVE_LIB_DEFECTS
      // 402. wrong new expression in [some_]allocator::construct
     -> ::new(static_cast<void*>(__p)) _T1(__value);
    }

I often use STL. What should I do to discover the origin of segfault? Are there any tools that can help? What are the reasons that could lead to accidental failures like this.

Edit:

My program has about 5,000 lines of code. I don’t know what part of the code I need to show in order to get some help, since I have no idea about the origin of the problem, all I got from the debugger is that it is related to STL.

Edit:

Now I switched to Code::Blocks, here is the call stack:

#0 00464635 std::_Construct<std::pair<double const, int>, std::pair<double const, int> >(__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<double, std::pair<double const, int>, std::_Select1st<std::pair<double const, int> >, std::less<double>, std::allocator<std::pair<double const, int> > >::_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<double, std::pair<double const, int>, std::_Select1st<std::pair<double const, int> >, std::less<double>, std::allocator<std::pair<double const, int> > >::_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<double, std::pair<double const, int>, std::_Select1st<std::pair<double const, int> >, std::less<double>, std::allocator<std::pair<double const, int> > >::_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<double, std::pair<double const, int>, std::_Select1st<std::pair<double const, int> >, std::less<double>, std::allocator<std::pair<double const, int> > >::_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<double, int, std::less<double>, std::allocator<std::pair<double const, int> > >::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    ??() (??:??)

:

1/ . 2/: get_vehicles_distances(); . 3/, , IsMergeInstruction();

Edit:

, , segfault, :

vehicles_distances_.erase(vehicles_distances_.begin(), vehicles_distances_.end());

vehicle_distances_ - . : VehicleManager:: MoveAllVehicles();

void VehicleManager::MoveAllVehicles() {

     vehicles_distances_.erase(vehicles_distances_.begin(), vehicles_distances_.end());

     vector<Vehicle>::iterator iter_end = VehicleManager::vehicles_.end();
     for(vector<Vehicle>::iterator iter = VehicleManager::vehicles_.begin();
     iter != iter_end; ++iter) {

          (*iter).MoveVehicle();

          vehicles_distances_[(*iter).get_vec_vehicle_position().y] = (*iter).get_id_vehicle();

     }

}

?

Edit:

map:: clear(); map:: erase(); !

Edit:

, ... vehicle_distances_, .. (?)

Edit:

! :: erase(); . , , <key, value> , . ( , , - , , , id_vehicle !). , <key, value> , ​​ ...

!

+3
13

-, , , , Dev-++. , , . , , , . .

, : segfaults , - . .;)

- , . , segfault. , , ( , , , , , , () . , , , .

. segfault , (, , , ), , ( ).

, . , , : 1) , , 2) , , .

MSVC SCL , STL, , .

, GCC - ( ).

Segfaults . , , , , .:)

+10

Valgrind . , , , , .

+6

" _p". . _p . , .

, () , .

+6

, . - , . , , .

+2

_CrtSetDbgFlag() , . . CRT Debug Heap. , . Microsoft C, MinGW . GNU C, .

+2

, stl_construct.h. , STL Dev-++. , stl_construct.h, . , . ( , ) :

  • ( , )
  • , .
  • , NULL ( , , NULL, )
+2

, . ++: .

, foo, bar; , foo, bar. , , . (Is CheckingInstructionsThread , ? .)

, .cpp ( ), , . , stderr; fprintf, cerr, .

. , - , ​​ main():

 fprintf(stderr, "main() entered\n");

; main(), . fprintf, , .

+2

? gdb gdb a.out

core a.out.core

​​p >

+1

, - , / , / .
, - , .

+1

:)

. ? , . :

template<typename _T1, typename _T2>
    inline void
    _Construct(_T1** __p, const _T2& __value)
    {

       ::new(static_cast<void*>(*__p)) _T1(__value);
    }

:

template<typename _T1, typename _T2>
    inline void
    _Construct(_T1*& __p, const _T2& __value)
    {

       ::new(static_cast<void*>(__p)) _T1(__value);
    }
+1

, , , , , , , .

() , ().

.; -)

: http://en.wikipedia.org/wiki/Segmentation_fault
( , . ". " " " ).

+1

Cards are not very thread friendly. If you perform operations with maps in stream code, you really need to block all calls to this map and implement any iterators that you can use that can be canceled.

+1
source

The debugger should allow you to approach the call stack. Thus, you should be able to see the place in your own code causing the seg error.

-1
source

Source: https://habr.com/ru/post/1708838/


All Articles