Skip to main content
GameDev.net gamedev.net
🔒 Locked

Access Violation...

Started by elendil67 Aug 5, 2002 at 10:15 PM 11 replies 900+ views
Original Post
elendil67
elendil67
Hey all, I''m not totally sure whether this should go in the DirectX or OpenGL forum (I''m making a mini-engine for both API''s), so moderators please feel free to move it. In my FinishScene() function, if the current renderer is Direct3D, I simply call the EndScene() and Present() functions. If my renderer is OpenGL, I simply call SwapBuffers(), with my device context as a parameter. Simple enough. If I run the function in Direct3D, it works flawlessly. If I run it in OpenGL though, I encounter some problems. I get an Access Violation error, and the debugger traces me back to the EndScene() function. The only thing is that if I''m using OpenGL, the if statement should skip the code... but I get an Access Violation. If you''re confused, here''s the source:
  
// Function to finish rendering the scene

int CEngine::FinishScene()
{
	// If Direct3D is the current renderer

	if(this->m_bOpenGL == false)
	{
		// Finish rendering the scene in Direct3D


		// Finish rendering the scene

		this->m_pDirect3DDevice->EndScene();

		// Present the scene

		this->m_pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
	}

	// If OpenGL is the current renderer

	else
	{
		// Finish rendering the scene in OpenGL


		// Swap buffers

		SwapBuffers(this->m_hDC);
	}

	return 1;
}
  
I''m not sure whether I''m using the debugger right (in that function... I run Go but it stops at EndScene()), because this is one really weird error. .elendil67
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
Just3D
Just3D
I''m wondering if the this statements are necessary. You are already in the scope of the class, therefore you can omit all of the this-> words. In my experience, if there is a pointer that you can avoid dealing with, do so.

I''m not positive this is the prblem, but its a start.

Hope this helps!




"There are only three types of people in this world: those who can count, and those who can''t."

Just3D
Justin Nordin
J Squared Productions
www.jsquaredproductions.com
"There are only three types of people in this world: those who can count, and those who can't."Just3DJustin NordinJ Squared Productionswww.jsquaredproductions.com
elendil67
elendil67
Well... I just like using this . It lets me know which are global variables, which are local variables... etc. I don''t think that would be the problem.

.elendil67
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
Arild Fines
Arild Fines
The this-> is an acceptable idiom - it stresses that you are accessing member variables instead of locals or globals. It does seem a bit redundant though when you are already using the m_ thingie.
I''m more concerned about the if ( this->m_bOpenGL == false ) statement. These kinds of conditionals just cry out for polymorphism instead. I assume you have similar conditionals in the rest of your engine''s methods as well?
Anyway - the code DOES look correct - I would venture to guess that your problem really cannot be found in this block alone.


"Imagine the people who believe such things and who are not ashamed to ignore, totally, all the patient findings of thinking minds through all the centuries since the Bible was written. And it is these ignorant people, the most uneducated, the most unimaginative, the most unthinking among us, who would make themselves the guides and leaders of us all; who would force their feeble and childish beliefs on us; who would invade our schools and libraries and homes."
Isaac Asimov
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
elendil67
elendil67
Well... that''s what I was thinking of doing. I am about to rip apart the code .

.elendil67
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
elendil67
elendil67
Thanks for that info, everybody. I''ve made my engine more polymorphic, so it''s something like this

  
class CGraphics
{
public:

// Function to initialize the engine

virtual int Create(HWND hwnd, HDC hdc, int width, int height, int bpp);

// Function to set the clear color of the screen

virtual int SetClearColor(unsigned char r, unsigned char g, unsigned char b);

// Function to begin rendering the scene

virtual int BeginScene();

// Function to finish rendering the scene

virtual int EndScene();

protected:
ofstream m_fLog; // Log file

};

class COpenGL : public CGraphics
{
public:
// Constructor

COpenGL();

// Destructor

~COpenGL();

// Function to initialize the engine

int Create(HWND hwnd, HDC hdc, int width, int height, int bpp);

// Function to set the clear color of the screen

int SetClearColor(unsigned char r, unsigned char g, unsigned char b);

// Function to begin rendering the scene

int BeginScene();

// Function to finish rendering the scene

int EndScene();

private:
HGLRC m_hGLRC; // Handle to the OpenGL rendering context

HDC m_hDC; // Handle to the device context

};


I don''t think I need to show you CDirect3D too, because that''s pretty much the same as COpenGL with differences in the members. But, when I compile it, I get errors like:

CDirect3D.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::EndScene(void)" (?EndScene@CGraphics@@UAEHXZ)
COpenGL.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::EndScene(void)" (?EndScene@CGraphics@@UAEHXZ)
test.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::EndScene(void)" (?EndScene@CGraphics@@UAEHXZ)
CDirect3D.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::BeginScene(void)" (?BeginScene@CGraphics@@UAEHXZ)
COpenGL.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::BeginScene(void)" (?BeginScene@CGraphics@@UAEHXZ)
test.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::BeginScene(void)" (?BeginScene@CGraphics@@UAEHXZ)
CDirect3D.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::SetClearColor(unsigned char,unsigned char,unsigned char)" (?SetClearColor@CGraphics@@UAEHEEE@Z)
COpenGL.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::SetClearColor(unsigned char,unsigned char,unsigned char)" (?SetClearColor@CGraphics@@UAEHEEE@Z)
test.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::SetClearColor(unsigned char,unsigned char,unsigned char)" (?SetClearColor@CGraphics@@UAEHEEE@Z)
CDirect3D.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::Create(struct HWND__ *,struct HDC__ *,int,int,int)" (?Create@CGraphics@@UAEHPAUHWND__@@PAUHDC__@@HHH@Z)
COpenGL.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::Create(struct HWND__ *,struct HDC__ *,int,int,int)" (?Create@CGraphics@@UAEHPAUHWND__@@PAUHDC__@@HHH@Z)
test.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CGraphics::Create(struct HWND__ *,struct HDC__ *,int,int,int)" (?Create@CGraphics@@UAEHPAUHWND__@@PAUHDC__@@HHH@Z)
Debug/Engine 02.exe : fatal error LNK1120: 4 unresolved externals


.elendil67
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
Arild Fines
Arild Fines
Either provide an implementation(possibly empty) for the virtuals in your base class, or make them pure virtual.

Either:

virtual int Create(HWND hwnd, HDC hdc, int width, int height, int bpp)
{ /*some code here*/;}

or:

virtual int Create(HWND hwnd, HDC hdc, int width, int height, int bpp) = 0;

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
elendil67
elendil67
Thanks Arild Fines, it works now! I can''t really make them pure virtual functions, because you can''t make objects of an abstract class. But the empty braces worked! Thanks!

.elendil67
When you go homeTell them of us, and say:For your tomorrow,We gave our today.
Evil Bill
Evil Bill
quote:
Original post by elendil67
I can''t really make them pure virtual functions, because you can''t make objects of an abstract class.


  
class CGraphics
{
public:
virtual void Draw() = 0;
};

class CD3D : public CGraphics
{
public:
virtual void Draw() {/* Code Here */}
};

void Render(CGraphics* pGfx)
{
pGfx->Draw();
}

int WinMain(...)
{
CGraphics* pGfx = new CD3D;

Render(pGfx);
}


Thats a better way to do it IMO.

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Arild Fines
Arild Fines
quote:
Original post by elendil67
I can''t really make them pure virtual functions, because you can''t make objects of an abstract class.

Thats sort of the idea of abstract classes. You dont make instances of them, you make instances of the derived classes. Why would you ever need an instance of a CGraphics object?

--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
elendil67
elendil67
Hermmm... you''re right. I was thinking of CGraphics to be more of an interface. Well, since it''s a module, I guess I''ll just have a pointer to it in the engine class... so it will work! Thanks

.elendil67
When you go homeTell them of us, and say:For your tomorrow,We gave our today.

Topic Locked

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

Sign in to reply to this topic.