C++: vector<string> *args = new vector<string>(); causes SIGABRT

Justin Mrkva picture Justin Mrkva · Feb 16, 2011 · Viewed 8.8k times · Source

Pretty self explanatory. Here's the method that's causing the SIGABRT on the 'new vector' line:

vector<string> * Task::arguments() {
    vector<string> *args = new vector<string>(); // CAUSES SIGABRT
    int count = sizeof(_arguments);
    for (int x = 0; x < count; x++) {
        string argument(_arguments[x]);
        args->push_back(argument);
    }
    return args;
}

Note that elsewhere I call that exact line without any issues. Here is the list of includes in the Task class:

#include <vector>
#include <unistd.h>
#include <string>
using namespace std;
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

Any thoughts?

Answer

sbi picture sbi · Feb 16, 2011

There is no real error in this code, although the style suggests that you urgently need a good C++ book.

As @James said in his comment to the question, it is almost certainly wrong to use a dynamically allocated vector object, and it's certainly wrong to not to keep it in a smart pointer.
If you've also done this elsewhere in your code, you very likely have messed up the heap, which is the only case I can think of when new would crash.