试图读取数组时出现SIGSEGV

SIGSEGV whilst attempting to read array

本文关键字:SIGSEGV 数组 读取      更新时间:2023-10-16

我对此做了一些研究,在发现没有多少有用的东西之后,然后用尽了我所有的选择来尝试解决这个问题,我现在决定向Stack Overflow社区寻求建议。我就是弄不明白为什么我在尝试使用form = new_form(field);时得到SIGSEGV。

所讨论的函数是这个:

void GkForms::formNavLabel(std::shared_ptr<WINDOW> display, FIELD *field[], std::vector<char *> fieldNames, const size_t &fieldCount)
{
try {
    // Clear the WINDOW
    assert(display != NULL);
    wclear(display.get());
    // Initialization options
    cbreak();
    noecho();
    keypad(display.get(), TRUE);
    int formWinRows = 0;
    int formWinCols = 0;
    int subWinRows = 0;
    int subWinCols = 0;
    int ch = 0;
    static FORM *form;
    for (size_t i = 0; i < fieldCount; ++i) {
        if (fieldNames.at(i) == nullptr) {
            throw std::runtime_error(gettext("There has been an internal error with creating the form."));
        }
    }
    // Initialize the fields
    for (size_t i = 0; i < fieldCount; ++i) {
        field[i] = makeLabelActive(i, 0, fieldNames.at(i));
        assert(field[i] != NULL);
    }
    // Set field options
    for (size_t i = 0; i < fieldCount; ++i) {
        set_field_back(field[i], COLOR_PAIR(15));
        set_field_fore(field[i], COLOR_PAIR(16));
        field_opts_on(field[i], O_VISIBLE);
        field_opts_on(field[i], O_ACTIVE);
        field_opts_off(field[i], O_EDIT);
    }
    // Create the form and post it
    form = new_form(field);

实现这个函数使用的代码在这里:

            std::vector<xmlConfig::Servers> xmlData = data_serverMenu(xmlCfgFile);
            unsigned short lineCount = 0;
            unsigned short linesPerPage = (subScrollYSize - (borderSize * 2));
            std::vector<char *> output;
            short pages = (linesPerPage / xmlData.size());
            short curPage = 1;
            int ch;
            for (size_t i = 0; i < xmlData.size(); ++i) {
                if (i < linesPerPage) {
                    i = (i * curPage);
                    std::stringstream ss;
                    ss << " [ " << xmlData.at(i).serverProtocol.c_str() << " ] " << xmlData.at(i).serverName.c_str() << " ";
                    output.push_back(const_cast<char *>(ss.str().c_str()));
                    ++lineCount; // Do not get rid of this!
                }
            }
            FIELD *fields[(lineCount + 1)];
            for (unsigned short i = 0; i < (lineCount + 1); ++i) {
                fields[i] = new FIELD();
                if (i == (lineCount + 1)) {
                    fields[i] = 0;
                }
            }
            std::unique_ptr<GkForms> gkForms (new GkForms());
            gkForms->formNavLabel(display, fields, output, lineCount);

代码是相当混乱的时刻,因为它是在开发/实验的中间,但你可能会注意到,我正在实现一个NCurses应用程序,其中c++接口与C代码。如有任何帮助,我将不胜感激,谢谢。

注:我还决定包含这个辅助函数,以防需要它来诊断问题。

/**
 * @brief GkForms::makeLabelActive creates form 'labels' that can be selected and interacted with
 * @author Phobos Aryn'dythyrn D'thorga
 * @param frow      Position on the y-axis (NOTE: it is reversed, a possible bug?)
 * @param fcol      Position on the x-axis (NOTE: it is reversed, a possible bug?)
 * @param label     What you wish for the label to display as text
 * @return          Returns a complete FIELD object, ready to be used by the NCurses
 * forms library
 */
FIELD *GkForms::makeLabelActive(int frow, int fcol, char *label)
{
    FIELD *f = new_field(1, (int) strlen(label), frow, fcol, 0, 0);
    if (f) {
        set_field_buffer(f, 0, label);
        set_field_opts(f, (int) ((unsigned) field_opts(f) & O_ACTIVE));
    }
    return f;
}`

In

output.push_back(const_cast<char *>(ss.str().c_str()));

的指针
ss.str().c_str()

在语句结束时无效。

您需要动态分配副本,或者开始使用std::string

还有其他问题;

fields[i] = new FIELD();
if (i == (lineCount + 1)) {
     fields[i] = 0;
}
例如,如果条件为真,

是内存泄漏。
弃用const不应不认真考虑后果。