Fixed crash when joining new channels
[quassel.git] / src / client / client.cpp
index 55bf9c5..95748cb 100644 (file)
@@ -85,10 +85,10 @@ Buffer *Client::buffer(uint bufferUid) {
 
 Buffer *Client::buffer(BufferInfo id) {
   Buffer *buff = buffer(id.uid());
-  
+
   if(!buff) {
     Client *client = Client::instance();
-    Buffer *buff = new Buffer(id, client);
+    buff = new Buffer(id, client);
 
     connect(buff, SIGNAL(userInput(BufferInfo, QString)),
            client, SLOT(userInput(BufferInfo, QString)));
@@ -102,7 +102,7 @@ Buffer *Client::buffer(BufferInfo id) {
     client->_buffers[id.uid()] = buff;
     emit client->bufferUpdated(buff);
   }
-  
+  Q_ASSERT(buff);
   return buff;
 }
 
@@ -144,10 +144,6 @@ Client::Client(QObject *parent)
 }
 
 Client::~Client() {
-// since we're now the parent of buffers this should be no longer needed
-  
-//   foreach(Buffer *buf, buffers.values()) delete buf; // this is done by disconnectFromCore()! FIXME?
-//   Q_ASSERT(!buffers.count());
 }
 
 void Client::init() {
@@ -237,16 +233,9 @@ void Client::connectToCore(const QVariantMap &conn) {
 }
 
 void Client::disconnectFromCore() {
-  if(clientMode == RemoteCore) {
-    socket->close();
-    //QAbstractSocket *sock = qobject_cast<QAbstractSocket*>(socket);
-    //Q_ASSERT(sock);
-    //sock->disconnectFromHost();
-  } else {
-    socket->close();
-    //disconnectFromLocalCore();
+  socket->close();
+  if(clientMode == LocalCore)
     coreSocketDisconnected();
-  }
 }
 
 void Client::coreSocketConnected() {
@@ -267,17 +256,17 @@ void Client::coreSocketDisconnected() {
 
   /* Clear internal data. Hopefully nothing relies on it at this point. */
   _bufferModel->clear();
-  // Buffers, if deleted, send a signal that causes their removal from buffers and bufferInfos.
-  // So we cannot simply go through the array in a loop (or use qDeleteAll) for deletion...
-  while(!_buffers.empty()) {
-    delete _buffers.take(_buffers.keys()[0]);
+
+  foreach(Buffer *buffer, _buffers.values()) {
+    delete buffer;
   }
   Q_ASSERT(_buffers.empty());
 
-  while(!_networkInfo.empty()) {
-    delete _networkInfo.take(_networkInfo.keys()[0]);
+  foreach(NetworkInfo *networkinfo, _networkInfo.values()) {
+    delete networkinfo;
   }
-    
+  Q_ASSERT(_networkInfo.empty());
+
   coreConnectionInfo.clear();
   sessionData.clear();
   layoutQueue.clear();
@@ -343,18 +332,18 @@ void Client::updateCoreConnectionProgress() {
   int numChannelsWaiting = 0;
 
   foreach(NetworkInfo *net, networkInfos()) {
-    if(not net->initialized())
+    if(! net->initialized())
       numNetsWaiting++;
 
     numIrcUsers += net->ircUsers().count();
     foreach(IrcUser *user, net->ircUsers()) {
-      if(not user->initialized())
+      if(! user->initialized())
        numIrcUsersWaiting++;
     }
 
     numChannels += net->ircChannels().count();
     foreach(IrcChannel *channel, net->ircChannels()) {
-      if(not channel->initialized())
+      if(! channel->initialized())
        numChannelsWaiting++;
     }