Results 1 to 5 of 5

Thread: QTcpSocket - memory allocation problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default Re: QTcpSocket - memory allocation problem

    Thanks, but ... sorry, I don't get it. I can't see anything wrong with this. Could you please point out what's causing a memory leak here?

    This is executed every time a client calls connectToHost(). Thus if only 1 client connects, then I will get only 1 instance of serversocket. The socket descriptor is passed to the tcp socket who then emits the signal readyRead() whenever data is coming in. The signal is connected to my method readBlockData() which reads the data from the stream, and thus should free the buffer.

    I set a breakpoint at these lines to check how often a ServerSocket is instantiated, and it was only once as expected.

    Or did I miss anything else here.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: QTcpSocket - memory allocation problem

    Qt Code:
    1. ServerSocket *socket = new ServerSocket(this);
    To copy to clipboard, switch view to plain text mode 
    This creates a local variable called socket which points to a memory location.

    Two important facts:
    1. local means that the variable will be deleted at the end of the scope, in this case the } bracket of the function.
    2. Since it is a pointer, the memory allocated will not be freed at the end of the scope.

    This has a consequence:
    The allocated memory still exists, but you can't access it anymore (the socket variable).

    What you need to do:
    Manage all your pointers.
    If there isn't a parent/child relationship where the parent frees all the child allocations, or in the case the lifetime of the parent is too long (as in the parent exists for the duration the program is being run), then you need to explicitly free the resources yourself.

    Thus:
    In your case, just before the scope of the function ends, you should delete the memory allocated. This, of course, will make your program not work anymore like you expect it.

    Thus:
    You should really define your pointers at class level so they are accessible everywhere in the class for the lifetime of an object based on that class.

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

    dreac (31st July 2011)

Similar Threads

  1. memory allocation problem
    By marc2050 in forum Newbie
    Replies: 7
    Last Post: 23rd May 2011, 09:05
  2. Widget Memory Allocation
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 5
    Last Post: 9th May 2010, 19:51
  3. Virtual memory allocation problem
    By morfei in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2009, 11:30
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 09:21
  5. vector memory allocation
    By TheKedge in forum General Programming
    Replies: 1
    Last Post: 23rd March 2006, 17:27

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.