Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

segfault on vector<struct>

I created a struct to hold some data and then declared a vector to hold that struct.

But when I do a push_back I get damn segfault and I have no idea why!

My struct is defines as:

typedef struct Group
{
    int codigo;
    string name;
    int deleted;
    int printers;
    int subpage;

    /*included this when it started segfaulting*/
    Group(){ name.reserve(MAX_PRODUCT_LONG_NAME); }
    ~Group(){ name.clear(); }
    Group(const Group &b)
    {
    codigo = b.codigo;
    name = b.name;
    deleted = b.deleted;
    printers = b.printers;
    subpage = b.subpage;
    }
   /*end of new stuff*/
 };

Originally, the struct didn't have the copy, constructor or destructor. I added them latter when I read this post below.

Seg fault after is item pushed onto STL container

but the end result is the same.

There is one this that is bothering me as hell! When I first push some data into the vector, everything goes fine. Later on in the code when I try to push some more data into the vector, my app just segfaults!

The vector is declared

vector<Group> Groups

and is a global variable to the file where I am using it. No externs anywhere else, etc...

I can trace the error to:

_M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage- this->_M_impl._M_start);

in vector.tcc when I finish adding/copying the last element to the vector....

As far as I can tell. I shouldn't be needing anything to do with a copy constructor as a shallow copy should be enough for this. I'm not even allocating any space (but I did a reserve for the string to try out).

I have no idea what the problem is!

I'm running this code on OpenSuse 10.2 with gcc 4.1.2

I'm not really to eager to upgrade gcc because of backward compatibility issues...

This code worked "perfectly" on my windows machine. I compiled it with gcc 3.4.5 mingw without any problems...

help!

--- ... ---

:::EDIT:::

I push data

Group tmp_grp;

(...)

tmp_grp.name = "Nova ";
tmp_grp.codigo=GetGroupnextcode();
tmp_grp.deleted=0;
tmp_grp.printers=0;
tmp_grp.subpage=0;
Groups.push_back(tmp_grp);
like image 745
André Moreira Avatar asked Apr 29 '10 17:04

André Moreira


People also ask

Is segfault a runtime error?

The segmentation error is one of the runtime error, that is caused because of the memory access violation, like accessing invalid array index, pointing some restricted address etc. In this article, we will see how to detect this type of error using the GDB tool.

Is segfault an exception?

They are both called exceptions, but they originate at different levels of the software/hardware of the system. Technically, you can catch segfaults with a signal handler for SIGSEGV . However, as Ivaylo explains, it's is not, typically, allowed to just "try again" if you get a segfault.

What can cause segfault?

Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

Can you catch segfault?

You can't catch segfaults. Segfaults lead to undefined behavior - period (err, actually segfaults are the result of operations also leading to undefined behavior.


1 Answers

Like Neil said, you don't need a default constructor, copy constructor, or destructor:

  • std::string cleans up after itself, so your destructor is never necessary.
  • The compiler-provided shallow copy constructor will work just fine.
  • std::string::reserve is unnecessary, since std::string will dynamically allocate memory as needed, but it may provide a performance benefit.

The code that you posted looks correct (and it looks very simple and straightforward, so it's hard to see where an error could creep in). Therefore, I suspect that you're corrupting memory elsewhere in your code and that the vector<Group> is simply the victim.

Try installing Valgrind (OpenSuse should provide a package for it) and run your application through it (from the command line, just run valgrind my-app) to see if Valgrind can catch any memory corruption.

like image 84
Josh Kelley Avatar answered Sep 29 '22 12:09

Josh Kelley