SIGSEGV在尝试读取数组时

时间:2015-10-25 16:43:27

标签: c++ ncurses dynamic-arrays

我已经对此进行了一些研究,在发现并没有太多帮助之后,为了解决这个问题而耗费了我所有的选择,我现在决定向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代码接口。非常感谢您的任何帮助,谢谢。

P.S。我还决定包括这个帮助函数,以防万一它需要诊断问题。

/**
 * @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;
}`

1 个答案:

答案 0 :(得分:2)

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