Crash in QTcpSocket inherited class : Help Needed



  • Hi All,

    I created a class that inhertits from QTcpSocket, because I need a QTcpSocket that binds on a local address (AFAIK such a feature is not supported by the standard QTcpSocket)
    The skeleton of my class is:

    @
    class QTcpBoundSocket : public QTcpSocket
    {
    Q_OBJECT

    /* some class stuff here */
    

    public:
    QTcpBoundSocket(QObject *parent = 0);
    virtual ~QTcpBoundSocket() {}

    virtual void connectToHost(const QString& aRemoteHostname, quint16 aRemotePort,
                               const QString& aLocalHostname, OpenMode openMode = ReadWrite);
    /* other stuff here */
    

    };
    @

    The class itself seems to work but I got a crash each time the class destructor is called.
    Is there anything clearly wrong in the way I'm inheriting from the QTcpSocket class?

    Here's the sample code that cause the crash:

    @
    QTcpBoundSocket* myBoundSocketPtr = new QTcpBoundSocket();
    Q_ASSERT(myBoundSocketPtr);

    QObject::connect ( myBoundSocketPtr, SIGNAL(disconnected()),
                       myBoundSocketPtr, SLOT(deleteLater()) );
    
    bool b = false;
    QByteArray data;
    
    qDebug() << "Connecting to " << aRemoteHost << ":" << aRemotePort << "from" << aLocalHost;
    myBoundSocketPtr->connectToHost(aRemoteHost, aRemotePort, aLocalHost);
    
    b = myBoundSocketPtr->waitForConnected();
    if (b)
    {
        qDebug() << "Bound socket connected :)";
        myBoundSocketPtr->write(QString("GET index.html\r\n").toAscii());
        myBoundSocketPtr->waitForReadyRead();
        while( (data = myBoundSocketPtr->readAll()).length() )
        {
            qDebug() << data;
        }
    }
    myBoundSocketPtr->close();
    

    @

    Thanks
    Sergio


  • Moderators

    Welcome to devnet

    What do you mean with binds on a local address?

    The question is probably how your destructor is invoked?
    The problem may be deleteLater() when you destruct a connected socket.



  • Hi Koahnig,

    I'll try to answer your questions:

    My connectToHost(...) method uses the POSIX functions to create a tcp socket and bind it to the local IP address I specify as a parameter. Then I use the setSocketDescriptor() method to pass the control of the socket to the (base class) QTcpSocket object.

    I connected the disconnect() signal to the deleteLater(), because I thought I need to delete the object only after the connection has been closed. Some other Qt forum posts suggests this. Is that wrong?
    My class do not allocate (explicitly) any memory, so the destructor basically does nothing.

    Thanks
    Sergio


  • Moderators

    Could it be that you are trying to set up a pipe?
    You want to connect an external IP address to an internal IP address, but you are not interested to do anything other with that information in your application?
    I do not see the point to use Qt and POSIX functions in parallel for establishing sockets. I would rather stay with either method. But this qualifies more like a personal preference.

    QTcpSocket may be connected to local address (127.0.0.1) as well.



  • Hi,

    I see your point in saying that I should only use POSIX or Qt rather than mixing them, but I'm designing this class to integrate it in a bigger project that is Qt based, that's basically the rationale.
    When I talk about binding to a local address I do not mean localhost (127.0.0.1) but an address assigned to a local interface. The TCP connection will be done then to a remote address. (but from the bound address)
    On my system I have multiple ifaces with different addresses (let's say 192.168.0.1 and 10.0.0.1 for instance) Is there a way to bind a socket to a local iface address with the standard QTcpSocket? I did not find how to.

    Thanks
    Sergio


  • Moderators

    I am doing Qt based applications, but I am not using Qt for all things inside my application. Basically I am Qt, stl and boost in one application. For instance strings, maps, lists and other things I am using from stl, because I am used to. And I have no intension to change to Qt.

    [quote author="sergico" date="1327770596"]The TCP connection will be done then to a remote address. (but from the bound address)
    On my system I have multiple ifaces with different addresses (let's say 192.168.0.1 and 10.0.0.1 for instance) Is there a way to bind a socket to a local iface address with the standard QTcpSocket? I did not find how to.[/quote]
    I cannot help you on this issue.



  • This is possible in Qt 5.0 using http://doc.trolltech.com/5.0-snapshot/qabstractsocket.html#bind but I don't know if the same functionality is offered in 4.x.



  • Hi Rich,

    Thanks for the tip. I was not aware Trolltech introduced this feature in Qt 5.x At the moment we are using Qt 4.7. I was not able to find a similar functionality in this version.

    Thanks
    Sergio



  • Well, since most of the surrounding socket code is the same in Qt 5.0, you could have a look at the implementation from there. It might help you out.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.