Skip to main content
gamedev.net
🔒 Locked

multithreading violation ?

Started by sprinter_trueno 1 reply 300+ views
Original Post
sprinter_trueno
sprinter_trueno
Hi ppl, Fiddling with multithreading provided me with a problem. I defined the following class:
  

class CTest
{
public:

	CTest(); virtual CTest();


	static DWORD __stdcall ThreadRoutine(LPVOID lparam)
	{
              CTest* pThread = (CTest*)lparam;

              pThread->TestFunc();
        }


	void StartThread()
	{
		CreateThread(NULL,NULL,ThreadRoutine,this,NULL,NULL);
	};


	void TestFunc()
	{
             int x = 0;
        }

};



  
Instantiating CTest somewhere in the main thread CTest Test; Test.StartThread(); Up to here everything is fine but after the typecast in the threadfunction I constantly get an exception if I try to call the TestFunc member of the CTest object. Why is that ? Do I violate some special rule concerning multithreading ? Maybe some security rights ? From my understanding of threads there shouldn''t be a problem. Just to make sure: pThread is the correct address of the object I instantiated. Maybe somebody could tell me about this. Thanks for any comments. Lorenz
cbenoi1
cbenoi1
quote:

Instantiating CTest somewhere in the main thread

CTest Test;
Test.StartThread();



Who handles the destruction of the CTest class instance? My guess is that you are constructing a stack-based CTest object in a routine and then return; by the time the thread is started, the object''s contents is no longer valid.

-cb
sprinter_trueno
sprinter_trueno
Blast !

Now that you mention it ... I don''t remember creating it on the heap when I tested.

Thanks for the hint. That''s most likely be the problem.


C ya

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.