c++ - 当我删除一个对象时出现段错误 - GDB 在 free() 中说

标签 c++ c memory memory-management

我正在做一个网络作业,我们应该用 C 创建一个网络库,然后在我们的 C++ 程序中使用它。我的 C++ 不如我的 C 强大,所以我先从 C++ 着手,这样我就可以解决出现的任何问题,我向您展示我的第一个问题。 :D

我有一个基类和一个继承类(最终会有另一个继承类),它们将提供确定服务器行为的函数。

基类头文件和析构函数:

    // Message Forwarder Base Class 
class MessageForwarder
{
public:
    /* ------ Public Function Declarations ------ */
    MessageForwarder(const string serverName, const string serverAddress);
    virtual ~MessageForwarder();
    virtual void Print() const = 0; 

protected:
    /* ------ Private Variable Declarations ------ */
    string monitor; // 192.168.1.102:8000 - The address to the monitoring server
    string myName; // The name of message forwarding server
    string myAddress; // The address of the message forwarding server
};    

MessageForwarder::~MessageForwarder()
{
    delete &this->monitor;
    delete &this->myName;
    delete &this->myAddress;
    fprintf(stdout, "Cleaning up MessageForwarder\n");
}

继承类和析构函数:

// Client Message Forwarder Derived Class
class ClientMessageForwarder : public MessageForwarder
{
public:
    /* ------ Public Function Declarations ------ */
    ClientMessageForwarder(const string serverName, const string serverAddress);
    ~ClientMessageForwarder();
    void Print() const; 

private:
    /* ------ Private Variable Declarations ------ */

};

ClientMessageForwarder::~ClientMessageForwarder()
{
    fprintf(stdout, "Cleaning up ClientMessageHandler\n");
}

当我试图删除一个类对象时,我的问题出现了。我的程序如下:

int main(int argc, char *argv[])
{
    /* ------ Variable Declarations ------ */

// Server Object
MessageForwarder *msgFrwder;

msgFrwder = new ClientMessageForwarder(serverName, serverAddress);
msgFrwder->Print();
delete msgFrwder; <------------ SEGFAULT here!

return 0;}

当我继续运行我的程序时,它在删除 msgFrwder 行出现段错误;我继续将 GDB 与转储核心一起使用,并询问它发生在哪里,它给了我以下信息:

#0  0x0000000800afe409 in free () from /lib/libc.so.7
#1  0x00000008006cbd17 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string () from /usr/lib/libstdc++.so.6
#2  0x0000000000401e88 in ~MessageForwarder (this=0x800d02080) at ./classes/msgfrwd.cpp:44
#3  0x00000000004023c5 in ~ClientMessageForwarder (this=0x800d02080) at ./classes/climsgfrwd.cpp:44
#4  0x000000000040158c in main (argc=7, argv=0x7fffffffe478) at ./msgfrwdserver.cpp:97

凭借我有限的 C++ 知识,我觉得我正在按照正确的步骤清理和释放我的内存。当我运行我的程序时,它实际上会输出“Cleaning up MessageForwarder”,所以我知道它执行了那一行。

我已经搜索了解决方案并为这个问题苦苦挣扎了一段时间,但找不到解决方案。任何帮助将不胜感激,或者对实际发生的事情以及段错误发生的原因的解释将有所帮助。

感谢您的帮助。它赞赏。 :D

最佳答案

字符串对象不是使用 new 运算符分配的。不要删除它们,它们会自动释放

关于c++ - 当我删除一个对象时出现段错误 - GDB 在 free() 中说,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2353124/

相关文章:

c++ - C++ 宏中的模板?

c++ - 使用类的非模板版本作为父类

c++ - 绑定(bind)到成员初始值设定项列表中的引用成员的临时对象的生命周期 (C++14)

c - 使用管道写入和读取 Int 数组

c - 使用回溯解决数独错误

c - 对于将字符串插入链表非常困惑

c++ - 我应该使用 QVariant 还是 MyCustomType* 将对象从 Qml 传递到 C++?

c - MAP_ANON 和 MAP_ANONYMOUS 都未在 C 中为 sys/mmap 声明

c++ - abi::__cxa_demangle 无法重用其自身返回的内存

c - Little-endian 字节顺序(在 C 中)