Results 1 to 6 of 6

Thread: QList of QTcpSocket derived class pointers (stack corruption)

  1. #1
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QList of QTcpSocket derived class pointers (stack corruption)

    Hi

    I'm trying to build a streaming server/client program that accommodates multiple clients. I have to keep track of all of them at any one time to determine which one sent a message as ALL data has to be multiplexed through port 4012.

    At the moment I'm allocating a new QTcpSocket object each time a new connection is made and keeping track of them through a QList object, but I keep getting heap corruption errors on destruction. I think it may be because of the wrong allocation of sockets or through the destruction of them.

    I've been struggling with this for a while now and it's very frustrating as the errors aren't consistent.

    Here's the code when QTcpServer signals newConnection(): (ClientSocket inherits from QTcpSocket)

    Qt Code:
    1. void Server::acceptConnection()
    2. {
    3. //Allocate space for new connection
    4. ClientSocket *client = new ClientSocket(this);
    5. client = (ClientSocket*)server->nextPendingConnection();
    6.  
    7. if (!client)
    8. QMessageBox::information(this, tr("Error"), tr("No client pointer allocated"));
    9. //Seach for client in client list
    10. int found = -1;
    11.  
    12. if (!clients.isEmpty()) {
    13. for (int i = 0; i < clients.size(); ++i) {
    14. if (clients.at(i)->peerAddress().toString() == client->peerAddress().toString())
    15. found = i;
    16. }
    17. //If not found in array: Don't add duplicates ie. from same host
    18. if (found == -1) {
    19. clients.append(client);
    20. ++connectionCount;
    21.  
    22. updateConnectionLabel();
    23. connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    24. connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
    25. clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
    26. clients.last()->setReadBufferSize(READ_BUFFER_MAX);
    27. } else {
    28. //If already connected, add to data client list: Add duplicates to data client array
    29. if (connectedClients < MAX_DATA_CLIENTS) {
    30. dataClients.append(client);
    31. ++connectedClients;
    32. updateConnectionLabel();
    33. connect(dataClients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    34. }
    35. }
    36. } else {
    37. //Add to array
    38. clients.append(client);
    39. ++connectionCount;
    40.  
    41. updateConnectionLabel();
    42. connect(clients.last(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    43. connect(clients.last(), SIGNAL(readyRead()), this, SLOT(startRead()));
    44. clients.last()->setReadBufferSize(READ_BUFFER_MAX);
    45. clients.last()->setOpenConnections(MAX_DATA_CLIENTS);
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    The destruction code at this time (I've tried various methods):

    Qt Code:
    1. void Server::clientDisconnected()
    2. {
    3. //Remove all the unconnected sockets from the client list
    4. for (int i = 0; i < clients.size(); ++i) {
    5. if (clients.at(i)->state() == QAbstractSocket::UnconnectedState)
    6. {
    7. clients.at(i)->close();
    8. clients.at(i)->deleteLater();
    9. clients.removeAt(i);
    10.  
    11. --connectionCount;
    12. updateConnectionLabel();
    13. }
    14. }
    15.  
    16. //Remove unconnected data clients from dataclient list
    17. for (int i = 0; i < dataClients.size(); ++i) {
    18. if (dataClients.at(i)->state() == QAbstractSocket::UnconnectedState)
    19. {
    20. dataClients.at(i)->close();
    21. dataClients.at(i)->deleteLater();
    22. dataClients.removeAt(i);
    23.  
    24. --connectedClients;
    25. updateConnectionLabel();
    26. }
    27. }
    28. QMessageBox::information(this, tr("DISCONNECT"), tr("DISCONNECT"));
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for any suggestions

  2. #2
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList of QTcpSocket derived class pointers (stack corruption)

    I forgot to add the definition of clients and dataClients. ClientSocket inherits from QTcpSocket and adds just a few parameters like openConnections and so on.

    Qt Code:
    1. QList<ClientSocket *> clients;
    2. QList<ClientSocket *> dataClients;
    To copy to clipboard, switch view to plain text mode 

    Is this the way to go or is there a better way?

    Should I rather use lists of actual objects than the pointers? Such as
    Qt Code:
    1. QList<ClientSocket> clients;
    To copy to clipboard, switch view to plain text mode 
    Or is this just wasting memory?
    Last edited by zAAm; 19th April 2010 at 22:10.

  3. #3
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList of QTcpSocket derived class pointers (stack corruption)

    Is it possible that since the functions aren't thread-safe they are trying to access the same class member? (in this case the QList clients)

    I know that disconnected() and readyRead() will be called in worker threads, so can that be cause of the heap corruption? When disconnected() is called while still in the readyRead() function for example and the socket is destroyed before the read function is completed?
    Should I use QMutexLocker then?

  4. #4
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList of QTcpSocket derived class pointers (stack corruption)

    Any ideas? Or something I could use to find where the heap corruption happens?

  5. #5
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList of QTcpSocket derived class pointers (stack corruption)

    Ok, I think what I'm trying to do with the Client *client = (ClientSocket *)server->nextPendingConnection() line is to downcast from base to derived class. (Since ClientSocket is derived from QTcpSocket and nextPendingConnection returns a pointer to a QTcpSocket).
    I've read that this is a bad thing, even when using pointers, as I'm abusing the whole inheritance system

    I thought about using static_cast, but I guess it would be better to change it altogether by using a separate class ClientSocket that doesn't inherit from QTcpSocket but contains a QTcpSocket pointer? Also, would using auto pointers help in this regard?

  6. #6
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList of QTcpSocket derived class pointers (stack corruption)

    Ok, it was definitely the downcasting that caused the issues. I've basically rewritten the whole ClientSocket class to include a QTcpSocket pointer that I can access publicly. This solved the heap corruption errors...

    Now to go through the rest of the 1500 lines and check that everything is as it should be with the new class...

Similar Threads

  1. QList with pointers
    By Rockem in forum Qt Programming
    Replies: 9
    Last Post: 12th August 2009, 18:53
  2. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 09:15
  3. QList Pointers
    By coderbob in forum Newbie
    Replies: 2
    Last Post: 20th November 2007, 18:50
  4. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 07:36
  5. QList of pointers
    By Farcaller in forum Newbie
    Replies: 4
    Last Post: 24th January 2006, 16:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.