I am working on an assignment for networking where we are supposed to create a networking library in C and then use it in our C++ program. My C++ isn't as strong as my C so I got started on that first so I could tackle any problems that came up, and I present you my first one. :D
I have a base class and an inherited class (there will eventually be another inherited one) which will provide functions that determine the servers behavior.
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 arises 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;}
When I go ahead and run my program it segfaults on the line delete msgFrwder; I go ahead and use GDB with the dumped core and ask where it happens and it gives me the following:
#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
With my limited C++ knowledge I feel like I am following the proper steps to clean up and free my memory. When I run my program it does in fact output "Cleaning up MessageForwarder" so I know it executed that line.
I have searched for solution and struggled with the problem for a while but can't find a solution. Any help would be much appreciated or an explanation of what is actually going on and why the segfault is happening would help.
Thanks for all your help. Its appreciated. :D