mercredi 22 septembre 2021

What is the correct way of freeing std::thread* heap allocated memory?

I'm declaring a pointer to a thread in my class.

class A{
   std::thread*    m_pThread;   
   bool StartThread();
   UINT DisableThread();
}

Here is how I call a function using a thread.

bool A::StartThread()
{
    bool mThreadSuccess = false;
    {
       try {
          m_pThread= new std::thread(&A::DisableThread, this);
          mThreadSuccess = true;
      }
      catch (...) {
         m_pDisable = false;
      }

      if(m_pThread)
      {
         m_pThread= nullptr;
      } 
   }
return mThreadSuccess;
}

Here is the function called by my thread spawned.

UINT A::DisableThread()
{
    //print something here.
    return 0;
}

If I call this StartThread() function 10 times. Will it have a memory leak?

for (i = 0; i<10; i++){
    bool sResult = StartThread();
      if (sResult) {
        m_pAcceptStarted = true;
      }
}

Aucun commentaire:

Enregistrer un commentaire