1

I am receiving a segmentation fault whenever I attempt to push a templated object into a vector. I have ran gdb, and am still unable to understand why I get the segmentation fault error.

Program received signal SIGSEGV, Segmentation fault. 0x0000003f5869d4f3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib64/libstdc++.so.6

This is where I am inserting the object into the vector:

void ReadyDelivery::LoadTruck(){
  string name = "";
  int capacity = 0;
  ifstream inputStream;

  inputStream.open(m_truckFile.c_str());
  while(inputStream >> name >> capacity ){
    Truck<Item, MAX_CAPACITY> t(name,capacity);
    cout<<name<<" "<<capacity<<endl;
    m_truck.push_back(t);
  }

  cout<<"Trucks loaded: "<<m_truck.size()<<endl;
  inputStream.close();

If I comment out where I push_back the object into the vector, there is no segmentation fault.

I also have a function that returns the vector. I am not sure if this could be causing it though...

vector<Truck<Item,MAX_CAPACITY> > & ReadyDelivery:: GetTruck(){
  return m_truck;
}

Thanks for the help!

Here is the definition of m_truck, which is a private member variable:

private:
  vector< Truck<Item, MAX_CAPACITY> > m_truck; //Vector of templated trucks

Here is where I construct the truck object in my template:

template <class T, int N>
  Truck<T,N>::Truck(string inName, int capacity){

  m_name = inName;

  m_capacity = capacity;

}

If I run gdb and use the where command... I get this:

#0  0x000000346ce9d4f3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib64/libstdc++.so.6
#1  0x0000000000402bd9 in Item::operator= (this=0x60eb00) at Item.h:11
#2  0x0000000000402c66 in Tqueue<Item, 200>::~Tqueue (this=0x60e478,
    __in_chrg=<value optimized out>) at Tqueue.h:96
#3  0x0000000000402b43 in Truck<Item, 200>::~Truck (this=0x60e450,
    __in_chrg=<value optimized out>) at Truck.h:114
#4  0x00000000004027d5 in std::_Destroy<Truck<Item, 200> > (__pointer=0x60e450)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:90
#5  0x000000000040249c in std::_Destroy_aux<false>::__destroy<Truck<Item, 200>*>
    (__first=0x60e450, __last=0x60e488)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:100
#6  0x00000000004021d3 in std::_Destroy<Truck<Item, 200>*> (__first=0x60e450,
    __last=0x60e488)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:123
#7  0x0000000000401cdf in std::_Destroy<Truck<Item, 200>*, Truck<Item, 200> > (
    __first=0x60e450, __last=0x60e488)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_construct.h:149
#8  0x000000000040197c in std::vector<Truck<Item, 200>, std::allocator<Truck<Item, 200> > >::~vector (this=0x7fffffffe1b0, __in_chrg=<value optimized out>)
    at /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_vector.h:313
#9  0x0000000000401593 in main (argc=1, argv=0x7fffffffe2c8) at driver.cpp:29
GameAnger
  • 11
  • 2
  • 1
    where do you declare/instantiate m_truck? – twain249 May 07 '17 at 20:54
  • In the header file, I have it as a private member variable – GameAnger May 07 '17 at 21:00
  • 3
    "If I comment out where I push_back the object into the vector, there is no segmentation fault." That line is using the copy constructor. If you are violating the Rule of Three, it's likely that making copies causes the program to crash. [Obey the Rule of Three.](http://stackoverflow.com/q/4172722/103167) – Ben Voigt May 07 '17 at 21:05
  • If the crash occurs when you call `m_truck.push_back(t)`, perhaps you should look at {`m_truck`} or {`Truck::Truck(const Truck & copy_ctor)`, and `Truck::~Truck()`} to ensure you're doing something safely. My guess is you own something such as a raw pointer and you're doing Something Bad with it. – inetknght May 07 '17 at 21:07
  • Would this mean I would have to write a copy constructor? – GameAnger May 07 '17 at 21:10
  • Please post the definition of `m_truck`. – Stephan Lechner May 07 '17 at 21:10
  • Posted some more information – GameAnger May 07 '17 at 21:18
  • @GameAnger Try to run the debug-build in gdb. After the segmentation fault occurs, type `where` as command. It might give you / us more information. – Bobface May 07 '17 at 21:38
  • @Bobface I put the information above. Still a little confused. I know it shows exactly which line, but line 29 in my driver is insignificant to the problem. Even if I comment that out, I still receive the error – GameAnger May 07 '17 at 21:47
  • @GameAnger it seems to be happening in the destructor of your `Truck` class. You should check there. – Bobface May 07 '17 at 21:51
  • @Bobface my destructor is empty. Could that be an issue? – GameAnger May 07 '17 at 21:54
  • @GameAnger It could, it could not. It is hard to tell without knowning your whole code. Debugging this kind of errors is, in most cases, pretty time intensive. It is hard for other people to find out where and why this is happening, because you might need "deep" insight into the processes which are happening inside your program. Most of the time only the programer himself has enough knowledge of his own code to find a good starting point. Time to get familiar with debugging :) – Bobface May 07 '17 at 22:01

0 Answers0