Results 1 to 6 of 6

Thread: Is it safe to create QTcpServer on stack.

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Is it safe to create QTcpServer on stack.

    hi
    Does Creating a QTcpSocket or QTcpServer on a socket will create a problem(especially in Threading applications)
    Thank you.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it safe to create QTcpServer on stack.

    Why would it create a problem? Except that it goes out of scope unless you block the execution with an event loop or so.
    J-P Nurmi

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: Is it safe to create QTcpServer on stack.

    In the Fortune Client Example QTcpSocket is declared as pointer ,Whereas in the Blocking Fortune Client Example the QTcpSocket is declared without pointer.Does it makes any difference if dont declare QTcpSocket as pointer in the First case.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it safe to create QTcpServer on stack.

    Quote Originally Posted by babu198649 View Post
    In the Fortune Client Example QTcpSocket is declared as pointer
    It's allocated on the heap, because it's created in the constructor and it must remain alive after the constructor. If it was on the stack, the socket object would get automatically destructed after going out of scope, according to normal C++ rules.

    Whereas in the Blocking Fortune Client Example the QTcpSocket is declared without pointer.
    Because that example uses waitForXXX() methods to block until certain operations have finished. In that case the object remains alive long enough anyway, so there is no need to allocate it on the heap.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: Is it safe to create QTcpServer on stack.

    It's allocated on the heap, because it's created in the constructor and it must remain alive after the constructor.
    But it is declared in the private section of the class,So it should be visible to entire class Same as quint16 blockSize variable(please correct me ,i am sure i am missing important concept).

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Is it safe to create QTcpServer on stack.

    Quote Originally Posted by babu198649 View Post
    But it is declared in the private section of the class,So it should be visible to entire class Same as quint16 blockSize variable(please correct me ,i am sure i am missing important concept).
    If you allocate it on the stack as a member variable and you accidentally happen to pass a parent object, you might get a crash due to double delete; 1) by its parent 2) when going out of scope. Of course you can do it, but you have to be very careful not to break things. Allocating QObjects on the heap with correct parent is less error-prone, I'd say. Furthermore, even if it's not that huge advantage, this also makes a forward declaration sufficient in the header file.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    babu198649 (5th December 2008)

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.