Q_OBJECT memory allocation in a thread
-
Actually, I am using the mandelbrot example as a starting point for my app. I would like to allocate and use a few QImages in the run function, and the question is whether they will be deleted when the thread exits?
-
That depends on how you allocate them.
-
@SGaist said in Q_OBJECT memory allocation in a thread:
That depends on how you allocate them.
And is nothing which is specific to Qt btw.
-
how should I allocate QImage so that the memory is freed when the thread is deleted.
-
@chessking5544 said in Q_OBJECT memory allocation in a thread:
how should I allocate QImage so that the memory is freed when the thread is deleted.
Please don't start with Qt when you don't have basic c++ knowledge.
-
@chessking5544
Sorry, what I meant earlier byAnd what would you do in a destructor? :)
is that this is a not a
new
/allocated (pointer) variable. You don't have to destroy it, and what would you write to do so anyway even if you needed to? When the object (youraThread
instance) goes out of scope/is destroyed, theQImage
/QMutex
members will be destroyed without you doing anything. -
well, it seems that regardless of whether I allocate QImage off the stack or the heap, the memory is not freed when the thread exits. that is, an explicit delete must be called. I mention this only because you mentioned that it depends on how you allocate them.
-
@chessking5544 said in Q_OBJECT memory allocation in a thread:
well, it seems that regardless of whether I allocate QImage off the stack or the heap, the memory is not freed when the thread exits.
This has nothing to do with the thread. Since you add it as member of your class the memory is freed when your class gets destroyed. No thread or Qt magic here. Plain c++
-
This post is deleted!
-
As @Christian-Ehrlicher already said: the image gets deleted when the thread object is deleted. Do you delete the thread object?
Furthermore, I am not sure how you are trying to check if the memory is actually freed. The operating system might not be able to show you this. Your software will request memory from the OS when necessary, but can choose to keep it for later allocations when memory is freed. The only way you can test this is by repeatedly allocating and deallocating an image. If the memory usage does not grow then memory is correctly freed even if the OS does not show you this.