title: Segfault when I delete an object - GDB says in free()
body: <p>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</p>
  
  <p>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. </p>
  
  <p>Base Class header and destructor:</p>
  
  <pre><code>    // 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 &amp;this-&gt;monitor;
      delete &amp;this-&gt;myName;
      delete &amp;this-&gt;myAddress;
      fprintf(stdout, "Cleaning up MessageForwarder\n");
  }
  </code></pre>
  
  <p>Inherited Class and destructor:</p>
  
  <pre><code>// 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");
  }
  </code></pre>
  
  <p>My problem arises when I try to delete a class object. My program follows this:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      /* ------ Variable Declarations ------ */
  
  // Server Object
  MessageForwarder *msgFrwder;
  
  msgFrwder = new ClientMessageForwarder(serverName, serverAddress);
  msgFrwder-&gt;Print();
  delete msgFrwder; &lt;------------ SEGFAULT here!
  
  return 0;}
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>#0  0x0000000800afe409 in free () from /lib/libc.so.7
  #1  0x00000008006cbd17 in std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::~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
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Thanks for all your help. Its appreciated. :D</p>