Use delete within a QThread
isaacEnrique last edited by
I wanted to know if it is correct to use delete , within a subclass of QThread, to free the memory of an array (style-C array), memory that was allocated in another thread (the main thread in this case).
It should be clear that the operation delete  is not within the destructor of my QThread subclass (in which case, according what I understand, there is no problem). The operation is done in a function that is called from my implementation of QThread::run() (ie, while the thread is running).
Thanks in advance for any help and/or suggestions.
sandy.martel last edited by
You can call delete  anywhere you want, but it's for arrays allocated with new , not C style array.
MuldeR last edited by
Nowadays it should be perfectly safe to use delete in a "background" thread (i.e. a thread that is not the "main" thread) at any time you want. The C++ Runtime itself should be thread safe. BUT: You need to be aware that any pointer to the array becomes dangling as soon as the array has been deleted! So, if any pointer to that array exist that might be accessed outside of the thread which calls the delete, e.g. because the pointer is in some global variable, you must make sure that none of the other threads will ever try to access the array after it has been deleted! Or in other words: Your thread must not delete the array, until it's certain that no other thread might still access the array. Otherwise you get undefined behavior, including crashes.