- // We cannot delete the socket right now because we can
- // be in the middle of a test or something. So we append
- // it to the list of objects to be deleted.
- m_text->AppendText(_T("Deleting socket.\n"));
- wxPendingDelete.Append(sock);
+ // Destroy() should be used instead of delete wherever possible,
+ // due to the fact that wxSocket uses 'delayed events' (see the
+ // documentation for wxPostEvent) and we don't want an event to
+ // arrive to the event handler (the frame, here) after the socket
+ // has been deleted. Also, we might be doing some other thing with
+ // the socket at the same time; for example, we might be in the
+ // middle of a test or something. Destroy() takes care of all
+ // this for us.
+
+ m_text->AppendText(_("Deleting socket.\n\n"));
+ sock->Destroy();