2013-03-05 88 views
0

下面我有一個int main()和兩個頭文件,其中一個是創建線程的類,另一個是在windows_thread類中創建的名爲object的類。這個非常簡單的練習應該輸出99,而不是輸出1(出於某種未知的原因)。我也嘗試使用一個指向new的對象,當void call()從函數Thread_no_1()崩潰到類object時,可能是因爲它不存在。我希望有人可以解決這個問題,否則我只會在int main()中使用windows線程。Windows CreateThread在類函數調用中,指針引用崩潰?

這是主要的。

#include "windows_thread.h" 

int main() 
{ 
    windows_thread* THREAD = new windows_thread(); 
    THREAD->thread(); 

    delete THREAD; 

    return 0; 
} 

這是windows_thread.h

#include <windows.h> 
#include <stdio.h> 
#include "object.h" 

#define BUF_SIZE 255 

class windows_thread 
{ 
     object OBJECT; 

    public: 

     windows_thread():OBJECT(99) 
     { 
      //OBJECT = new object(99); 

     } 
     ~windows_thread() 
     { 
      //delete OBJECT; 
     } 
     void thread() 
     { 
      std::cout<<"void thread: "<<std::endl; 
      int Data_Of_Thread_1 = 1;   // Data of Thread 1 
      HANDLE Handle_Of_Thread_1 = 0;  // variable to hold handle of Thread 1 

      HANDLE Array_Of_Thread_Handles[1]; // Aray to store thread handles 

    // Create thread 1. 
      Handle_Of_Thread_1 = CreateThread(NULL, 0, Wrap_Thread_no_1, &Data_Of_Thread_1, 0, NULL); 
      if (Handle_Of_Thread_1 == NULL) ExitProcess(Data_Of_Thread_1); 

    // Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects() 
      Array_Of_Thread_Handles[0] = Handle_Of_Thread_1; 


    // Wait until all threads have terminated. 
      WaitForMultipleObjects(1, Array_Of_Thread_Handles, TRUE, INFINITE); 

      printf("Since All threads executed lets close their handles \n"); 

    // Close all thread handles upon completion. 
      CloseHandle(Handle_Of_Thread_1); 
     } 
     void DisplayMessage (HANDLE hScreen, char *ThreadName, int Data, int Count) 
     { 
      TCHAR msgBuf[BUF_SIZE]; 
      size_t cchStringSize; 
      DWORD dwChars; 

    // Print message using thread-safe functions. 
    //StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Executing iteration %02d of %s having data = %02d \n"), Count, ThreadName, Data); 
    //StringCchLength(msgBuf, BUF_SIZE, &cchStringSize); 
      WriteConsole(hScreen, msgBuf, cchStringSize, &dwChars, NULL); 
      Sleep(1000); 
     } 
     DWORD WINAPI Thread_no_1() 
     { 
      std::cout<<"Thread_no_1: "<<std::endl; 
      OBJECT.call(); 
      //OBJECT->call(); 

      return 0; 
     } 
     static DWORD WINAPI Wrap_Thread_no_1(LPVOID lpParam) 
     { 
      std::cout<<"Wrap_Thread_no_1: "<<std::endl; 

      windows_thread *self = reinterpret_cast<windows_thread*>(lpParam); 
      self->Thread_no_1(); 

      return 0; 
     } 
}; 

下是object.h

#ifndef OBJECT_H 
#define OBJECT_H 

#include <iostream> 

class object 
{ 
     private: 

      int value; 

     public: 

     object(int value) 
     { 
      std::cout<<"object::constructor: "<<std::endl; 
      this->value = value; 
     } 
     ~object(){} 
     void call() 
     { 
      std::cout<<"object::call(): begin"<<std::endl; 
      std::cout<<value<<std::endl; 
      std::cout<<"object::call(): end"<<std::endl; 
     } 
}; 
#endif 
+0

你正在做的'main'指針的原因嗎? – chris 2013-03-05 14:14:26

+0

這可以做到這一點,看你如何傳遞一個指向整數的指針:'windows_thread * self = reinterpret_cast (lpParam); self-> Thread_no_1();' – chris 2013-03-05 14:16:13

+0

@我想它可能是'windows_thread THREAD;'。我傾向於臨時創建對象,因此是臨時使用的線程。有沒有辦法以其他方式釋放記憶? – pandoragami 2013-03-05 14:27:52

回答

4

此函數調用:

Handle_Of_Thread_1 = CreateThread(
    NULL, 
    0, 
    Wrap_Thread_no_1, 
    &Data_Of_Thread_1, // <== THIS IS A POINTER TO AN int 
    0, 
    NULL 
    ); 

通行證&Data_Of_Thread_1(指針一個int )至CreateThread()。這是最終傳遞給Wrap_Thread_no_1()的論點。

在該函數中,您將該指針轉換爲windows_thread*並通過它調用成員函數。這會在您的代碼中注入未定義的行爲

你可能爲了做這個:

Handle_Of_Thread_1 = CreateThread(NULL, 0, Wrap_Thread_no_1, this, 0, NULL); 
//               ^^^^ 
+0

同意,int Data_Of_Thread_1被傳遞給函數Wrap_Thread_no_1中的windows_thread * self。 – neohope 2013-03-05 14:26:59