***检测到的GLIBC *** MS2:free():无效指针:0xB7526FF4 ***

*** glibc detected *** ms2: free(): invalid pointer: 0xb7526ff4 ***

本文关键字:无效 指针 0xB7526FF4 free GLIBC MS2 检测      更新时间:2023-10-16

我不断遇到此错误,我不确定如何纠正它,因为我在代码编辑器中没有错误。我查找了类似的问题,但是我仍然很难理解如何在此处应用解决方案。我已经尝试更改代码几个小时,但无济于事。任何帮助,将不胜感激。我在下面提供了.h和.cpp文件。

errormessage.h

#ifndef SICT_ERRORMESSAGE_H
#define SICT_ERRORMESSAGE_H
#include <iostream>
namespace sict {
    class ErrorMessage {
        char* message_; //pointer that holds the address of the message stored in current object
    public:
        explicit ErrorMessage(const char* errorMessage = nullptr); //receive address of a C-style nullterminate string holding an error message
        ErrorMessage(const ErrorMessage& em) = delete; //deleted copy constructor that prevents copying of an ErrorMessage object
        ErrorMessage& operator=(const ErrorMessage& em) = delete; //deleted assignment operator that prevents assignment of ErrorMessage object to current object
        virtual ~ErrorMessage(); //deallocates any memory that has been dynamically allocated by the current object
        void clear(); //clears any message stored by current object and initialize object to safe, empty state
        bool isClear() const; //return true if object is in a safe, empty state
        void message(const char* str); //stores a copy of the C-style string pointed to by str
        const char* message() const; //return address of the message stored in current object
    };
    //helper operator
      std::ostream& operator<<(std::ostream& os, const ErrorMessage& err);
}
#endif

errormessage.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "ErrorMessage.h"
namespace sict {
    ErrorMessage::ErrorMessage(const char* errorMessage) {
    if(errorMessage == nullptr) {
        message_ = nullptr;
    }
    else {
            message(errorMessage);
    }
    }
    ErrorMessage::~ErrorMessage() {
    delete [] message_;
    }
    void ErrorMessage::clear() {
    delete [] message_;
    message_ = nullptr;
    }
    bool ErrorMessage::isClear() const {
        if(message_ == nullptr) {
            return true;
        }
        else {
            return false;
        }
    }
    void ErrorMessage::message(const char* str) {
        delete [] message_;
        message_ = new char[strlen(str) + 1];
        strcpy(message_, str);
    }
    const char* ErrorMessage::message() const {
        return message_;
    }
    std::ostream& operator<<(std::ostream& os, const ErrorMessage& err) {
    if(!err.isClear()) {
          os << err.message();
    }
    return os;
    }
}

您的代码通过编辑语法检查和编译不足为奇 - 它是有效的代码。它只是在某处有一个不正确的指针。

这可能意味着您的''意外地取消了某物,或者也许在您应该通过指针的地方传递一个值。您应该在此类内容上发出编译时间警告。

另一种可能性是您无法初始化一些指针,其值恰好是0xb75...

显然,您和我都不太可能从此错误起源。正如Sam Varshavchik在评论中指出的那样,您甚至都不知道您发布的代码是否是错误的来源。即使您猜测了这一方法(或敏锐地观察,Sam),尝试以这种方式写C 也很愚蠢。

您需要的是调试器。调试器是您在中运行程序的程序,它可以跟踪程序的状态,以便当您违反内存时,调试器可以产生A backtrace 显示在哪里在您的源代码中,发生了错误。您还必须在调试支持的情况下编译程序,以便调试器具有可以用来引用源代码的标记。

这是一个远远超出了您的问题范围的过程,但是一旦您知道自己要做什么,它就很容易学习。如果可能的话,请寻找与您的IDE集成在一起的,因为您正在大力利用开发环境。您已经设置了它并不是不可能 - 您可能只需要使用它即可。首先在编辑器的上下文中搜索C 调试 - 如果什么都没出现gdb)。

您将对编程C/C 的内容有更准确的了解。祝你好运。