Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use delete[] within a QThread
Forum Updated to NodeBB v4.3 + New Features

Use delete[] within a QThread

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    isaacEnrique
    wrote on last edited by
    #1

    Greetings.

    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.

    Isaac Pérez
    Programming is understanding.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sandy.martel
      wrote on last edited by
      #2

      You can call delete [] anywhere you want, but it's for arrays allocated with new [], not C style array.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        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.

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved