C ++ vector.push_back()崩溃了程序

时间:2012-12-18 05:08:57

标签: c++ vector crash

我正在编写一个shell程序并为传送带编写了一个测试。每次我启动它时,它都会在第三个vector.push_back()上崩溃,不会产生任何异常,并且会编写很多无法解释的单词。请告诉我,我做错了什么。

#include <stdio.h>
#include <vector>
#include "Program.cpp"
#include "Conveyor.cpp"
#include <stdlib.h>

using namespace std;

int main(){
    vector <Program> programs;
    char *argv[2];
    argv[0] = "./increaser";
    argv[1] = NULL;
    Program program1(argv[0], argv);
    Program program2(argv[0], argv);
    Program program3(argv[0], argv);

    printf("conveyor_test - PUSH 1\n");
    programs.push_back(program1);
    printf("conveyor_test - PUSH 2\n");
    programs.push_back(program2);
    printf("conveyor_test - PUSH 3\n");
    try{
        programs.push_back(program3);
        printf("conveyor_test - PUSHED 3\n");
    }
    catch (...){
        printf("Wild exception was caught.\n");
        exit(1);
    }
    printf("conveyor_test - pushed programs into vector\n");
    fflush(stdout);

    printf("---------START-----------\n");
    fflush(stdout);
    conveyor(programs);
    printf("---------END-------------\n");
    return 0;
}

这是写入输出的内容:

conveyor_test - PUSH 1
conveyor_test - PUSH 2
conveyor_test - PUSH 3
*** glibc detected *** ./conveyor_test: free(): invalid pointer: 0x0000000001c75040 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f357c463b96]
./conveyor_test[0x401806]
./conveyor_test[0x40388a]
./conveyor_test[0x4034c0]
./conveyor_test[0x402e57]
./conveyor_test[0x4024c5]
./conveyor_test[0x402718]
./conveyor_test[0x401dce]
./conveyor_test[0x40125d]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f357c40676d]
./conveyor_test[0x400d69]
======= Memory map: ========
00400000-00406000 r-xp 00000000 00:15 265077                             /home/crabman/Dropbox/Projects/C++/shell/conveyor_test
00605000-00606000 r--p 00005000 00:15 265077                             /home/crabman/Dropbox/Projects/C++/shell/conveyor_test
00606000-00607000 rw-p 00006000 00:15 265077                             /home/crabman/Dropbox/Projects/C++/shell/conveyor_test
01c75000-01c96000 rw-p 00000000 00:00 0                                  [heap]
7f357c0e9000-7f357c1e4000 r-xp 00000000 08:01 790941                     /lib/x86_64-linux-gnu/libm-2.15.so
7f357c1e4000-7f357c3e3000 ---p 000fb000 08:01 790941                     /lib/x86_64-linux-gnu/libm-2.15.so
7f357c3e3000-7f357c3e4000 r--p 000fa000 08:01 790941                     /lib/x86_64-linux-gnu/libm-2.15.so
7f357c3e4000-7f357c3e5000 rw-p 000fb000 08:01 790941                     /lib/x86_64-linux-gnu/libm-2.15.so
7f357c3e5000-7f357c59a000 r-xp 00000000 08:01 790899                     /lib/x86_64-linux-gnu/libc-2.15.so
7f357c59a000-7f357c799000 ---p 001b5000 08:01 790899                     /lib/x86_64-linux-gnu/libc-2.15.so
7f357c799000-7f357c79d000 r--p 001b4000 08:01 790899                     /lib/x86_64-linux-gnu/libc-2.15.so
7f357c79d000-7f357c79f000 rw-p 001b8000 08:01 790899                     /lib/x86_64-linux-gnu/libc-2.15.so
7f357c79f000-7f357c7a4000 rw-p 00000000 00:00 0 
7f357c7a4000-7f357c7b9000 r-xp 00000000 08:01 790924                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c7b9000-7f357c9b8000 ---p 00015000 08:01 790924                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c9b8000-7f357c9b9000 r--p 00014000 08:01 790924                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c9b9000-7f357c9ba000 rw-p 00015000 08:01 790924                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f357c9ba000-7f357ca9f000 r-xp 00000000 08:01 536749                     /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357ca9f000-7f357cc9e000 ---p 000e5000 08:01 536749                     /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357cc9e000-7f357cca6000 r--p 000e4000 08:01 536749                     /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357cca6000-7f357cca8000 rw-p 000ec000 08:01 536749                     /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.17
7f357cca8000-7f357ccbd000 rw-p 00000000 00:00 0 
7f357ccbd000-7f357ccdf000 r-xp 00000000 08:01 790877                     /lib/x86_64-linux-gnu/ld-2.15.so
7f357cec2000-7f357cec7000 rw-p 00000000 00:00 0 
7f357cedb000-7f357cedf000 rw-p 00000000 00:00 0 
7f357cedf000-7f357cee0000 r--p 00022000 08:01 790877                     /lib/x86_64-linux-gnu/ld-2.15.so
7f357cee0000-7f357cee2000 rw-p 00023000 08:01 790877                     /lib/x86_64-linux-gnu/ld-2.15.so
7fff2df40000-7fff2df61000 rw-p 00000000 00:00 0                          [stack]
7fff2df6e000-7fff2df6f000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

这是Program类的重要代码:

class Program{
private:
    char *path;
    int argc;
    char **argv;
public:
        Program(char *path, char **argv) {
        printf("Program::Program start\n");
        // вычисляем argc
        argc = 0;
        while (argv[argc] != NULL){
            argc++;
        }
        if (argc < 1){
            throw "Program::Program - argc < 1";
        }
        printf("\targc is calculated\n");
        // копируем path
        if (path == NULL){
            throw "Program::Program - path is NULL";
        }
        this -> path = new char[strlen(path) + 1];
        strcpy(this -> path, path);
        printf("\tpath is calculated\n");
        // копируем argv
        if (argv == NULL){
            throw "Program::Program - argv is NULL";
        }
        this -> argv = new char*[argc];
        for (int i = 0; i < argc; i++){
            this -> argv[i] = new char[strlen(argv[i]) + 1];
            strcpy(this -> argv[i], argv[i]);
        }
        printf("Program::Program end\n");
    }
    ~Program(){
        // printf("Program::~Program start\n");
        delete[] path;
        // printf("\tpath deleted\n");
        size_t size = sizeof(argv) / sizeof(argv[0]);
        // printf("\tsizeof(argv) = %d\n", size);
        for (size_t i = 0; i < size; i++){
            delete[] argv[i];
            // printf("\t\targv[%d] deleted\n", i);
        }
        // printf("\tall argv[i] deleted\n");
        delete[] argv;
        // printf("Program::~Program end\n");
    }

4 个答案:

答案 0 :(得分:4)

当您使用push_back时,实际放入向量的是副本。此副本是浅层副本,这意味着编译器只复制指针而不是它们指向的位置。

实际上会生成几个副本,当其中一个副本超出范围时,将调用副本析构函数,从而释放指针中的内存。但由于所有副本都有指向完全相同内存的指针,因此该内存现在标记为空闲,因此您无法访问它们。

你的问题的评论部分中有链接称为“三个规则”,这意味着如果你有 析构函数,复制构造函数或赋值运算符,那么你应该实施这三个。这是为了确保在实例复制时进行深层复制,同时复制实际数据。

答案 1 :(得分:2)

问题是你拥有指针并且没有实现三规则(C ++ 11中的五条规则)。

其他人建议你应该实施三条规则 我不同意这种观点。这是解决这个问题的错误方法。一个类不应该包含多个拥有的指针(很难正确执行)。你应该做的是使用正确的指针类型。

在这种情况下,path应为std::string。这是因为std :: string正确处理字符串的内存管理。

在这种情况下argv(在程序内)。应该是std::vector<std::string>。这是因为std::vector<>正确处理动态大小的数组的内存管理。在这种情况下,数组中的每个元素都是一个字符串(需要单独处理)。

一旦你做了这些更正,程序的实现变得更加简单,因为你正确地将内存管理移动到专门为此任务设计的类(这称为关注点分离:一个类处理业务逻辑或资源管理(你的class程序执行业务逻辑,因此不应该进行资源管理(内存管理)。

现在,新版程序更加简单:

class Program{
private:
    std::string              path;
    int                      argc;
    std::vector<std::string> argv;
public:
    Program(char *path, char **argv)
        :path(path ? path : "")
    {
        printf("Program::Program start\n");
        // вычисляем argc
        argc = 0;
        // What happens if argv is NULL?
        while (argv[argc] != NULL){
            argc++;
        }
        if (argc < 1){
            throw "Program::Program - argc < 1";
        }
        printf("\targc is calculated\n");
        // копируем path
        if (path == NULL){
            throw "Program::Program - path is NULL";
        }
        printf("\tpath is calculated\n");
        // копируем argv
        if (argv == NULL){
            throw "Program::Program - argv is NULL";
        }
        for (int i = 0; i < argc; i++){
            this->argv.push_back() = argv[i];
        }
        printf("Program::Program end\n");
    }
    /* Don't need this
    ~Program(){
    } */
}

答案 2 :(得分:1)

添加复制构造函数,分配内存并复制内容。 每当在向量中添加对象时,都会在向量对象上调用复制构造函数。这是崩溃的原因。

答案 3 :(得分:1)

我添加了一个复制构造函数,它不再崩溃了。谢谢大家。

Program (const Program& that){

        // copying argc

        argc = that.getargc();

        // copying path

        path = new char[argc + 1];

        strcpy(path, that.getpath());

        // copying argv

        int len = 0;

        while (that.getargv()[len] != NULL){

            len++;

        }

        argv = new char*[len + 1];

        for (int i = 0; i < len; i++){

            argv[i] = new char[strlen(that.getargv()[i]) + 1];

            strcpy(argv[i], that.getargv()[i]);

        }

    }