c++ - c++构造函数中的段错误

标签 c++ constructor segmentation-fault

所以我有 cl_Page 类:

class cl_Page{
    public:
    cl_Page(cl_LessonMoment *parent_param);
    cl_Page(cl_SoftRoot *parent_param);
    int parent_type;
    cl_LessonMoment *parent_lmoment;
    cl_SoftRoot *parent_softroot;

    char id[256];

    //<content>
    //Backgrounds.
    str_Color bgcolor;
    cl_Image bgimage;

    //Actual content
    vector<cl_Textbox> textboxes;
    vector<cl_Button> buttons;
    vector<cl_Image> images;
    //</content>

    cl_Textbox* AddTextbox();
    cl_Button* AddButton();
    cl_Image* AddImage(char *filename = nullptr);
};

和 cl_Page 构造函数:

cl_Page::cl_Page(cl_LessonMoment *parent_param) : bgimage(nullptr){ //here is the segfault
    parent_lmoment = parent_param;
    parent_type = 1;
    id[0] = '\0';
    SetColor(bgcolor, 0xffffffff);
}

cl_Page::cl_Page(cl_SoftRoot *parent_param): bgimage(nullptr){ // or here if i call this constructor
    /*parent_softroot = parent_param;
    parent_type = 2;
    id[0] = '\0';
    SetColor(bgcolor, 0xffffffff);*/
}

发生的事情是,无论我如何调用构造函数,或者无论我调用哪一个(第二个都被注释掉;所以基本上是空的),全局的、局部的或动态的,在函数中或作为一个成员对象,我得到一个段错误,它似乎在 cl_Page::cl_Page(cl_LessonMoment *parent_param) : bgimage(nullptr){ 行上。调用堆栈如下所示:

#0 77C460CB strcat() (C:\WINDOWS\system32\msvcrt.dll:??)
#1 0022F168 ?? () (??:??)
#2 00401905 cl_Page::cl_Page(this=0x22fbe8, parent_param=0x0) (F:\Scoala\C++\EduSoftViewer_Parser\sources\classes\soft_tree\page.cpp:10)
#3 00402B8A main() (F:\Scoala\C++\EduSoftViewer_Parser\sources\main.cpp:11)

在我写这篇文章之前的一些构建中,(有完全相同的问题)调用堆栈中的 #1 位置,现在 在哪里? () (??:??)ntdll!RtlDosApplyFileIsolationRedirection_Ustr() (C:\WINDOWS\system32\ntdll.dll:??)

所以我的问题是:有人知道是什么原因造成的吗?我真的需要让它工作。

如果有任何不清楚的地方,请尽管提问,我会提供更多信息。

编辑:澄清一下:我在 Windows XP SP2 下运行 Code::Blocks with gcc。

编辑 2:cl_Image 构造函数:

cl_Image::cl_Image(char *filename_param){
    if (filename == nullptr){
        filename[0] = '\0';
    }
    else{
        strcpy(filename, filename_param);
    }
    SetPosition(position, 0, 0);
    id[0] = '\0';
    visible = 1;
    z_index = 0;
}

这个类不包含任何对象成员,POD 结构除外,position

编辑 3:cl_Image 类:

class cl_Image{
public:
    cl_Image(char* filename_param = nullptr);


    str_Position position;
    char filename[256];
    char id[256];
    bool visible;
    int z_index;
};

str_Position 只是一个包含 2 个整数的结构。

最佳答案

很确定这是你的问题:

cl_Image::cl_Image(char *filename_param){
    if (filename == nullptr){  // <<==== filename??? try using the param.
        filename[0] = '\0';
    }

试试这个:

cl_Image::cl_Image(char *filename_param){
    if (filename_param == nullptr){
        filename[0] = '\0';
    }

关于c++ - c++构造函数中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816431/

相关文章:

c++ - 我们有一个 vector ,但不清楚调用哪个构造函数

javascript - 构造函数可以返回什么值来避免返回 this?

javascript - ES6 super() 在构造函数中实际上做了什么?

c - C中的段错误

c - 尝试使用链接列表运行程序并收到 "Segmentation Fault 11"

c++ - 在 C++ 中引用而不通过引用传递

c++ - 从右值到右值引用的 reinterpret_cast

c++ - 使用颜色校正矩阵在 Opencv 中进行颜色校正

c - 插入排序段错误

c++ - 将 C 转换为 C++