***检测到的GLIBC *** MS2:free():无效指针:0xB7526FF4 ***
*** glibc detected *** ms2: free(): invalid pointer: 0xb7526ff4 ***
我不断遇到此错误,我不确定如何纠正它,因为我在代码编辑器中没有错误。我查找了类似的问题,但是我仍然很难理解如何在此处应用解决方案。我已经尝试更改代码几个小时,但无济于事。任何帮助,将不胜感激。我在下面提供了.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 的内容有更准确的了解。祝你好运。
相关文章:
- 如何实现容纳整数和无效指针的双向链表?
- 在基数排序中,我得到 munmap_chunk():无效指针和中止(核心转储).为什么?
- 应用地址清理器后,免费无效指针消失
- C++ 在具有数组成员的类上无效指针/双精度释放
- 不断得到分段错误和free()无效指针
- 使用无效指针初始化指针声明符的行为是否未定义?
- 结构作为参数的函数产生错误:free():无效指针:0x00007efd47b
- 无效指针、隔离标识符等
- c++ 无效指针从空闲列表中取消排队
- C++ munmap_chunk(): 无效指针:
- 为什么我的简单C++程序使用队列和 fstream 错误到无效指针
- 无效指针和非初始化的指针之间的差异
- 如何用无效指针初始化unique_ptr的向量
- 地址清理器在静态强制转换无效指针时报告错误
- MS4:Free(Free()无效指针0xB74E74E0 ***
- ***检测到的GLIBC *** MS2:free():无效指针:0xB7526FF4 ***
- 无效指针功能的输出
- 有人能告诉我这是如何在main()中实现的吗?(关于无效指针的问题)
- C++11使用智能指针,但得到无效指针
- 无效指针的相关性