C++ “a.out”中的错误:free():下一个大小无效(正常)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26661877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:39:49  来源:igfitidea点击:

Error in 'a.out': free(): invalid next size (normal)

c++gccfile-io

提问by keke

I'm writing a big application and i recently included there my new module for working with files. It has simple functions of writing integers and strings, it was tested to be perfect on Red Hat. It does exactly what i want.

我正在编写一个大型应用程序,最近我在其中包含了用于处理文件的新模块。它具有写整数和字符串的简单功能,在 Red Hat 上测试是完美的。它正是我想要的。

Here's the implementation of the functions there:

这是那里功能的实现:

int CFile_open(CFile* owner, char* filename, char* mode) {
    owner->file = fopen(filename, mode);
    if (owner->file == NULL) {
        return 0;
    } else {
        return 1;
    }
}

int CFile_close(CFile* owner) {
    if (owner->file == NULL) {
        return 0;
    } else {
        if (fclose(owner->file) ) {
            return 1;
        } else {
            return 0;
        }
    }
}

int CFile_writeInt(CFile* owner, int num) {
    if (owner->file == NULL) {
        return 0;
    } else {
        if (fwrite(&num, sizeof(int), 1, owner->file)) {
            return 1;
        } else {
            return 0;
        }
    }
}

int CFile_writeString(CFile* owner, char* str) {
    if (owner->file == NULL) {
        return 0;
    } else {
        char b = strlen(str);
        if (fwrite(&b, sizeof(char), 1, owner->file)) {
            for (int i=0; i<=b; i++) {
                fwrite(&str[i], sizeof(char), 1, owner->file);
            }
        } else {
            return 0;
        }
    }
}

These functions are being used in the wrapper-function and then being executed in my application.
Here's that function:

这些函数在包装函数中使用,然后在我的应用程序中执行。
这是那个函数:

void fileWrite(void* data) {
    CList* list = (CList*)data;
    CNode* node = list->first;
    CData* info = NULL;
    CFile* f = (CFile*)malloc(sizeof(CFile));
    f->init = CFile_init;
    f->init(f);

    if (list->length == 0) {
        printf("%s\n", "The list is empty!");
        wait();
    } else {
        int i = 1;

        if (f->open(f, (char*)"./f", (char*)"w")) {
            while (node) {
                info = node->getData(node);

                if (f->writeInt(f, info->getNum(info))) {
                    if (f->writeString(f, info->getAdr(info))) {
                        if (f->writeString(f, info->getPh(info))) {
                            printf("%s%d\n", "Wrote item #", i);
                            i++;
                        } else {
                            printf("%s%d\n", "Error writing Phone of item #", i);
                        }
                    } else {
                        printf("%s%d\n", "Error writing Adress of item #", i);
                    }
                } else {
                    printf("%s%d\n", "Error writing Number of item #", i);
                }

                node = node->next;
            }
            if (f->close(f)) {
                printf("%s\n", "Wrote successfully");
                wait();
            }
        }
        free(f);
    }
}

The problematic spot is the f->close(f)at the very end of the function.
If i don't close the file, everything works flawlessly. But if i do, i'm getting the following error:

有问题的地方是f->close(f)在函数的最后。
如果我不关闭文件,则一切正常。但如果我这样做,我会收到以下错误:

*** Error in 'a.out': free(): invalid next size (normal): 0x0859c320 ***

However, despite the application crash, it does close the file, and the info i wrote to it is correct and not corrupted.

然而,尽管应用程序崩溃,它确实关闭了文件,而且我写给它的信息是正确的并且没有损坏。

But i can't figure out why am i getting the error. I close the file only once (as you may see from the function), and i also tried commenting the free(f)line.
Also if i call fclose(f->file)there i get the same error, so that's not the close()function issue.

但我不明白为什么我会收到错误。我只关闭文件一次(正如您从函数中看到的那样),并且我还尝试注释该free(f)行。
另外,如果我fclose(f->file)在那里打电话,我会得到同样的错误,所以这不是close()功能问题。

I assume that the rest of the application works (and worked before i added the function) perfectly except for the line where i close the file.

我假设除了我关闭文件的那一行之外,应用程序的其余部分都可以正常工作(并且在我添加该功能之前已经工作)。

The reason i mentioned RedHat is because it was working perfectly there, but when i recompiled it under ArchLinux (also tried Debian) - it won't close the file. Same compiler versions were used everywhere.

我提到 RedHat 的原因是因为它在那里工作得很好,但是当我在 ArchLinux(也尝试过 Debian)下重新编译它时 - 它不会关闭文件。到处都使用相同的编译器版本。

I compile it with g++if that matters, but i also have the same issue with clang++.

g++如果这很重要,我会编译它,但我也有同样的问题clang++

I also tried running it with valgrindand it says nothing about files, or i just couldn't find the right keys for that. Perhaps you know?

我也尝试使用它运行它,valgrind但它没有说明文件,或者我找不到正确的键。也许你知道?

So do you have any ideas on what's going on and how to fix it? I was staring at the code for literally 3 hours already and i can't figure it out for the life of me.

那么您对正在发生的事情以及如何解决它有什么想法吗?我已经盯着代码看了 3 个小时,但我一辈子都搞不清楚。



Test case:

测试用例:

info->getNum()returns integer "11" (assigned during input)
info->getAdr()returns dynamically allocated char array "aa" (allocated and assigned during input)
info->getPh()returns dynamically allocated char array "bb" (allocated and assigned during input)

info->getNum()返回整数“11”(在输入期间分配)
info->getAdr()返回动态分配的字符数组“aa”(在输入期间分配和分配)
info->getPh()返回动态分配的字符数组“bb”(在输入期间分配和分配)

I can test these values using other functions in my application, so i'm 100% confident they are correct.

我可以在我的应用程序中使用其他函数测试这些值,因此我 100% 确信它们是正确的。

Then i just evoke the fileWrite()function.

然后我只是唤起fileWrite()功能。

回答by C-B

At a first glance:

乍一看:

In CFile_writeStringthere's an off-by-one error. for (int i=0; i<=b; i++)should terminate at i<b. The &str[b]will result in undefined behaviour.

CFile_writeString那里有一个一对一的错误。for (int i=0; i<=b; i++)应终止于i<b. 该&str[b]会导致不确定的行为。

BTW, result type of strlenis size_t, not char.

顺便说一句,结果类型strlensize_t,不是char

I don't know if that's the cause of your problem, though.

不过,我不知道这是否是您的问题的原因。