Segfault when I delete an object - GDB says in free ()

I am working on an assignment for networks where we need to create a network library in C and then use it in our C ++ program. My C ++ is not as strong as my C, so I started from the beginning so that I can solve any problems that have arisen, and I present to you my first .: D

I have a base class and an inherited class (eventually it will be another inherited one) that will provide functions that determine the behavior of the servers.

Base class header and destructor:

    // 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");
}

Inherited class and destructor:

// 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");
}

My problem occurs when I try to delete a class object. My program follows this:

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;}

, segfaults 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

++ , . , " MessageForwarder", , .

, . , , segfault.

. .: D

+3
3

new. , .

+4

:

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

- . , new , , delete?

+2

free() malloc() , - , - , , . , segfault.

Your mistake, as others have pointed out, is the deleteinclusion of members of stringyour class. The memory for these members (ignoring any memory that they could dynamically allocate internally) was not allocated by a separate call to malloc / new, and so when you free them, you will ruin the heap. As @Nikolai points out, they accordingly start the destructor automatically.

However, if you stated the following:

class Foo {
    string *s1;
};

then you'll need delete this->s1a foo destructor.

0
source

Source: https://habr.com/ru/post/1734806/


All Articles