Sign in to follow this  
Lenox

Access Violations with virtual functions

Recommended Posts

Lenox    166
Hey, I have a problem. Well, I recently created my own thread class ( It may not look original, but trust me, I spent about 20 minutes researching the thread functions ( _beginthreadex, _endthreadex, ResumeThread, and SuspendThread..those types of things ) ) and now, I'm getting access violations when calling functions that are virtual but undefined in the base class. Thread.hpp
#ifndef THREAD_HPP
#define THREAD_HPP

#include "stdafx.hpp"

class Mutex
{
protected:
private:
	const char			*		_name;
	HANDLE						_handle;
public:
	Mutex( const char* Name )
		: _name( Name )
	{
		_handle = CreateMutex( NULL, true, _name );
		assert( _handle != NULL );
	}

	~Mutex()
	{
		ReleaseMutex( _handle );
	};

};

class Thread
{
protected:
		// Operations

	inline void Resume() { ResumeThread( GetCurrentThread() ); };
	inline void Suspend() { SuspendThread( GetCurrentThread() ); };
	inline void End() 
	{
		_endthreadex( _threadId );
		delete _mutex;
	};

	static unsigned int _stdcall EntryPoint( void* pThis );

	virtual void Setup()=0;
	virtual void Execute( void *Arg )=0;

	void Run();

private:
	unsigned int		_threadId;
	void		*		_Arg;
	Mutex		*		_mutex;
public:
	int Create( void* Arg );
	void Delete(){ End(); };

	Thread();
	virtual ~Thread()
	{
		if( !_mutex )
		{
			return;
		}
		else
		{
			End();
		};
	};
};


#endif



Thread.cpp
#include "Thread.hpp"

Thread::Thread() {}; 

int Thread::Create( void* arg )
{
	_Arg = arg;

	int cCode;

	_mutex = new Mutex(NULL);
	cCode = static_cast<int>(_beginthreadex(NULL, NULL, &Thread::EntryPoint, _Arg, NULL, &_threadId ));

	return cCode;
};

unsigned int _stdcall Thread::EntryPoint( void *pThis )
{
	Thread * pThread = static_cast< Thread* > ( pThis );
	pThread->Setup();
	pThread->Execute( pThread->_Arg );
	return 1;
};



I put that to work with this: Message Pump.hpp
#ifndef MESSAGEPUMP_HPP
#define MESSAGEPUMP_HPP

#include "Thread.hpp"
#include "stdafx.hpp"
#include "Kernel.hpp"


class MessagePump : public Thread
{
protected:
private:
	MSG msg;
	int _exit;
public:
	void Setup()
	{
		_exit = 0;
	};

	void Execute( void *arg )
	{
		do 
		{
			while( PeekMessage( &msg, static_cast<HWND>(arg), 0, 0, PM_REMOVE ) )
			{	
				if( msg.message == WM_QUIT )
				{
					Inst->SetExitCode(msg.wParam);
					_exit = 1;
					break;
				};
			};
		} while( !_exit );
	};
};


#endif



And then created an instance of that class and called Create(), calling Delete() at program exit. Any help and suggestions on improvements for my Thread class, please do NOT hesitate to post. SIDE-NOTE: I was not intending for mutex to work. Thanks for any help in advance, -Lenox

Share this post


Link to post
Share on other sites
LessBread    1415
I think a better title for your thread would be something like "Please help with my thread class" or "access violations with virtual functions". So you might want to edit your original post.

Share this post


Link to post
Share on other sites
KulSeran    3267
what is the arg you pass into create?
are you sure that it is the "this" pointer from your instance of MessagePump?
or the better question might be, what is the code you are using in your main program to test your thread function.

Share this post


Link to post
Share on other sites
Lenox    166
Thanks, I just went back and realized that the class needs serious refactoring, so I've changed my method of doing it - I now use templates to decide what type of class it is then make a copy of "this" to use, then if I really feel like passing any type of information through my Create() function, I'll pass it as void*.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this