Results 1 to 14 of 14

Thread: How do I use QTcpSocket properly ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2007
    Posts
    28
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    1

    Default Re: How do I use QTcpSocket properly ?

    Quote Originally Posted by fullmetalcoder View Post
    Nop... If that's really the code you're using (not talking about completeness here just exactitude...) Then the connection must close as soon as the execution leaves the scope where your socket is instantiated (loop, function, whatever) because you don't use dynamic allocation but heap allocation... Thus the socket object is destroyed and stops communicating with your server... I think you should read some more about OOP and C++...
    When I call the function like this : (session is a pointer to class object)

    Qt Code:
    1. session->tcpSocket.write((const char*)buf,len);
    2. session->tcpSocket.waitForBytesWritten();
    To copy to clipboard, switch view to plain text mode 

    I get this error :

    Unhandled exception at 0x670afb8a (QtCored4.dll) in MyProgram.exe: 0xC0000005: Access violation reading location 0xcdcdcdd9.

    and it's break at this location :

    Qt Code:
    1. private:
    2. -> Q_DECLARE_PRIVATE(QIODevice)
    3. Q_DISABLE_COPY(QIODevice)
    To copy to clipboard, switch view to plain text mode 

    I access the tcpSocket object that was created in a running thread.
    If I want to maintain the connection what should I do ?
    create a static object ?


    By the way isn't heap allocation & dynamic allocation is the same thing, just with different terms ?

    I read this on a book, chapter 20.3 of "An Introduction to Design Patterns in C++ with Qt" :

    The heap or free storage (dynamic storage): Objects created via new are examples.
    The lifetime of a heap object is determined entirely by the use of new and delete.
    In general, the allocation and freeing of heap objects should be in carefully encapsulated classes.
    And I read this on a book, chapter 3 of "Memory Management, Algorithms and Implementations in C-C++" :
    Heap memory allocation, also known as dynamic memory allocation
    (DMA), consists of requesting memory while an application is running
    from a repository known as the heap.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: How do I use QTcpSocket properly ?

    1. Generally speaking accessing class members directly is wrong. Instead you should provide wrapper functions/slots/whatever to handle that
    2. Sounds like your QTcpSocket object is an object and not a pointer. This is also considered as bad design because it is NOT a type which can be copied (noticed the Q_DISABLE_COPY() macro ?) just like basic types (int, QString, QPixmap QList<>, ... for instance) which implies that in most cases, and especially when using such objects outside of their creation scope, they should be created through new and accessed via pointers...
    3. There seems to be quite a misunderstanding about semantics... I used to to think that "heap allocation" referred to objects created as objects (i.e. without "new" operator) which were automatically deleted when the program leaved the creation scope whereas "dynamic allocation" referred to process of creating objects via "new" and deleting them manually with "delete". I might have been misleaded as far as names are concerned (and I shall investigate this) but my remarks still stand in your case : use pointers when dealing with network objects!

    Hope this helps.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. The following user says thank you to fullmetalcoder for this useful post:

    mnemonic_fx (29th March 2007)

  4. #3
    Join Date
    Mar 2007
    Posts
    28
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Thanks
    1

    Default Re: How do I use QTcpSocket properly ?

    Ok, I think I understand about dynamic allocation and using pointers, I try to use that, but still.. it doesn't solve my problem.

    I declare CMsgSession *session on CMsgClient private member.
    I write this on the constructor :

    Qt Code:
    1. CMsgClient::CMsgClient()
    2. {
    3. session = new CMsgSession();
    4. }
    To copy to clipboard, switch view to plain text mode 

    And I declare QTcpSocket *tcpSocket on CMsgSession public member, I write this on the constructor :

    Qt Code:
    1. CMsgSession::CMsgSession(QObject *parent) : QThread(parent), workerQuitFlag(false)
    2. {
    3. tcpSocket = new QTcpSocket();
    4. }
    To copy to clipboard, switch view to plain text mode 

    and I access the tcpSocket like this :

    session->tcpSocket->write(buf, len);

    it causes access violation, it's getting more confusing,
    but thanks anyway..

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: How do I use QTcpSocket properly ?

    Quote Originally Posted by mnemonic_fx View Post
    session->tcpSocket.write((const char*)buf,len);
    How do you initialize buf and len?

    Quote Originally Posted by mnemonic_fx View Post
    and it's break at this location : (...)
    Could we see the backtrace?

Similar Threads

  1. Tell QTcpSocket which interface to use
    By rianquinn in forum Qt Programming
    Replies: 7
    Last Post: 23rd December 2010, 17:28
  2. problem with QTcpSocket
    By SuperSonik in forum Qt Programming
    Replies: 8
    Last Post: 31st January 2007, 16:00
  3. Problems with QThread and QTcpSocket
    By cookiem in forum Qt Programming
    Replies: 6
    Last Post: 2nd November 2006, 08:25
  4. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

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
  •  
Qt is a trademark of The Qt Company.