Results 1 to 3 of 3

Thread: What is the best way to manage memory across threads?

  1. #1
    Join Date
    Jan 2009
    Location
    Holland
    Posts
    2
    Thanks
    1

    Default What is the best way to manage memory across threads?

    Hello,

    Short version
    One thread allocates memory and passes the pointer to the other thread. The other thread might free the memory block, OR it might send it back, in which case the original thread that allocated it frees it. I find this current solution messy and I want a cleaner / more centralized way of managing the memory.

    Long version
    I have a GUI (main) thread and a worker thread. To let them communicate with each other, I created a Communicator class.
    When the worker thread needs to communicate with the GUI, it allocates an instance of the Communicator class and sends it (using a signal) to the GUI.
    The GUI might send a signal back to the worker thread (passing the instance of the Communicator class back).

    Now the problem is that I find this solution messy and want a better way to manage the memory. Currently I do this: either A) If the GUI sends back the signal to the worker thread, the worker thread frees it OR B) If the GUI doesn't send back the signal to the worker thread, it frees it by itself.

    Source code
    If you want to have a look at the source code, it can be found over here. In the directory 'Ui' you'll find the files Ui.cpp and UiCommunicator.cpp, and in the directory 'MtpHelper' you'll find MtpHelper.cpp, which is used as the worker thread.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What is the best way to manage memory across threads?

    Get rid of your Communicator class and use events. You can send/post arbitrary events back and forth safely between threads as long as they both run event queues. If you need to pass some specific data, implement your own custom event. Qt handles freeing memory allocated to posted events (you need to handle memory of sent events yourself so better post instead of sending especially that this is what you want anyway) so your problem will naturally disappear.

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

    bughunter2 (4th January 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Holland
    Posts
    2
    Thanks
    1

    Default Re: What is the best way to manage memory across threads?

    That's what I initially tried, but I didn't know how to send data with the event and just assumed (forgot to check the docs on it) that I needed to create a lot of events for every single message box the worker thread would want to display.

    Thanks for the idea. I'll give it a go.

Similar Threads

  1. Replies: 4
    Last Post: 29th December 2008, 19:50
  2. Replies: 0
    Last Post: 26th September 2008, 21:33
  3. Memory leak weirdness
    By Darhuuk in forum General Programming
    Replies: 10
    Last Post: 10th January 2008, 18:51
  4. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 19:42
  5. 'memory leak' threads on QT centre
    By locus in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2007, 17:44

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.