Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Thread & Class

Thread & Class

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
3 Posts 3 Posters 0 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
    IceBerG71
    wrote on last edited by
    #1

    Hi I have a problem with the thread and class. I created a thread from a function and in the thread function, i called and init a class but not using the new and delete method. There is a problem when the thread ended as the class destructor was not activated when i called ExitThread...an example below main function() { CreateThread(.....,ThreadProc,...); } ThreadProc() { CDB_Database Data(); //Class Init DWORD dwRecords; HANDLE hFile; etc OpenFile(); ... .... ... ExitThread(0); } my problem is everytime i called creatThread, the class constructor will be activated but when i called ExitThread, the class destructor was not activated. So is the resources used by the thread free when i called exitThread as i seems to have sharing violation inside the function Thank you for ur advise

    M B 2 Replies Last reply
    0
    • I IceBerG71

      Hi I have a problem with the thread and class. I created a thread from a function and in the thread function, i called and init a class but not using the new and delete method. There is a problem when the thread ended as the class destructor was not activated when i called ExitThread...an example below main function() { CreateThread(.....,ThreadProc,...); } ThreadProc() { CDB_Database Data(); //Class Init DWORD dwRecords; HANDLE hFile; etc OpenFile(); ... .... ... ExitThread(0); } my problem is everytime i called creatThread, the class constructor will be activated but when i called ExitThread, the class destructor was not activated. So is the resources used by the thread free when i called exitThread as i seems to have sharing violation inside the function Thank you for ur advise

      M Offline
      M Offline
      markkuk
      wrote on last edited by
      #2

      The ExitThread() function never returns, so the ThreadProc() doesn't reach its end and destructors don't get called. One way to solve this is to add another pair of braces:

      ThreadProc()
      {
      {
      CDB_Database Data(); //Class Init
      DWORD dwRecords;
      HANDLE hFile; etc

      OpenFile();
      //...
      

      }
      ExitThread(0);
      }

      Now Data is destroyed when code exits the inner braces, just before ExitThread()

      1 Reply Last reply
      0
      • I IceBerG71

        Hi I have a problem with the thread and class. I created a thread from a function and in the thread function, i called and init a class but not using the new and delete method. There is a problem when the thread ended as the class destructor was not activated when i called ExitThread...an example below main function() { CreateThread(.....,ThreadProc,...); } ThreadProc() { CDB_Database Data(); //Class Init DWORD dwRecords; HANDLE hFile; etc OpenFile(); ... .... ... ExitThread(0); } my problem is everytime i called creatThread, the class constructor will be activated but when i called ExitThread, the class destructor was not activated. So is the resources used by the thread free when i called exitThread as i seems to have sharing violation inside the function Thank you for ur advise

        B Offline
        B Offline
        Blake Miller
        wrote on last edited by
        #3

        If the ThreadProc is declared as follows: DWORD WINAPI ThreadProc( LPVOID lpParameter // thread data ); Why don't you just return 0; from your thread function instead of calling ExitThread at the every end of it anyway? Then your variables can go out of scope and destroy themselves automatically. Anything monitoring the 'exit code' of the thread will still see the 0. FYI (From MSDN): A thread that uses functions from the C run-time libraries should use the _beginthread and _endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups