A Big Trouble in my program
There is a big trouble in my game program which developed in mutiply file links. In other words, there is more than one .cpp file in my project, like following:
[My Project]
A.h------>define functions to A.cpp
B.h------>define functions to B.cpp
Main.h--->include A.h and B.h;
A.cpp---->include main.h; LPDIRECTDRAW lpDraw
B.cpp---->include main.h;
Main.cpp->include main.h;
I defined lpDraw which is LPDIRECTDRAW structure and global varible in A.cpp. First I try to call it from the same file (which is A.cpp) , it''s ok (of course). But once time I try to call lpDraw from other file (which is B.cpp) , something happened.
It''s all fine when compiling or linking. but there is a big trouble when I run this program. It shows "Unhandle exception in MyProgram.exe : 0xC0000005: Access Violation"...
Somebody tall me what''s the problem with my program?
I have to call the lpDraw from other file , so somebody save my life please! thanks anyway.
Besides, sorry for my poor English.
there is my code following:
[in A.cpp]
LPDIRECTDRAWSURFACE DDCreateSurface(DWORD width, DWORD height, BOOL sysmem )
{
LPDIRECTDRAW lpdd = lpDraw->lpdd; //<==trouble
...
...
}
[in B.cpp]
void Init()
{
...
LPDIRECTDRAWSURFACE lpSurf = DDCreateSurface(100,100,0);
...
...
}
quote: Original post by Jonoson
There is a big trouble in my game program which developed in mutiply file links. In other words, there is more than one .cpp file in my project, like following:
[My Project]
A.h------>define functions to A.cpp
B.h------>define functions to B.cpp
Main.h--->include A.h and B.h;
A.cpp---->include main.h; LPDIRECTDRAW lpDraw
B.cpp---->include main.h;
Main.cpp->include main.h;
///////////////// In A.h// This basically says "there's an LPDIRECTDRAW called lpDraw// somewhere in this project, and it will be linked in."extern LPDIRECTDRAW lpDraw;///////////////// in A.cpp// In this file we declare our structLPDIRECTDRAW lpDraw;LPDIRECTDRAWSURFACE DDCreateSurface(DWORD width, DWORD height, BOOL sysmem ){LPDIRECTDRAW lpdd = lpDraw->lpdd;......}///////////////// In B.cpp// since we #include A.h, we don't need to write anything special here...void Init(){...LPDIRECTDRAWSURFACE lpSurf = DDCreateSurface(100,100,0);......}
Try this... I don't know how you did it before, since you got it to compile... Did you have two variables with the same name in both files?
[EDIT]: If this isn't your problem, you might want to do some error checking, like checking if lpDraw==NULL... Maybe checking exactly what line causes the program to crash, and why...
The stuff I explained is about how to access lpDraw in B.cpp (see below), but is that what you want to do?
[in B.cpp]
void Init()
{
...
LPDIRECTDRAWSURFACE lpSurf = DDCreateSurface(100,100,0);
lpDraw->whatever=lpSurf;
delete lpDraw;
}
/Ksero
Edited by - Ksero on November 24, 2000 4:13:22 AM
This is what to do:
1. In A.h (extern LPDIRECTDRAW lpdp)
2. In A.CPP (Include A.h;LPDIRECTDRAW lpdp=NULL )
3. In B.CPP (Include A.h;LPDIRECTDRAW lpdp=NULL )
That is all.
Bye 4 now
System Failure
1. In A.h (extern LPDIRECTDRAW lpdp)
2. In A.CPP (Include A.h;LPDIRECTDRAW lpdp=NULL )
3. In B.CPP (Include A.h;LPDIRECTDRAW lpdp=NULL )
That is all.
Bye 4 now
System Failure
November 24, 2000 09:19 AM
TO Ksero:
Thanks for your stuff. That''s certain that what I want is access lpDraw in B.cpp. This way I tried , but still cannot work.
wherever I define the lpDraw in ( wherever in A.cpp or B.cpp ), It always show "assess violation" when running this program.
Is there any other way to solve this terrible problem? thanks anyway.
Thanks for your stuff. That''s certain that what I want is access lpDraw in B.cpp. This way I tried , but still cannot work.
wherever I define the lpDraw in ( wherever in A.cpp or B.cpp ), It always show "assess violation" when running this program.
Is there any other way to solve this terrible problem? thanks anyway.
November 24, 2000 09:20 AM
TO Ksero:
Thanks for your stuff. That''s certain that what I want is access lpDraw in B.cpp. This way I tried , but still cannot work.
wherever I define the lpDraw in ( wherever in A.cpp or B.cpp ), It always show "assess violation" when running this program.
Is there any other way to solve this terrible problem? thanks anyway.
Thanks for your stuff. That''s certain that what I want is access lpDraw in B.cpp. This way I tried , but still cannot work.
wherever I define the lpDraw in ( wherever in A.cpp or B.cpp ), It always show "assess violation" when running this program.
Is there any other way to solve this terrible problem? thanks anyway.
In fact , I access the lpDraw in B.app in a class structure like the code following:
[B.h]
class UnitList
{
public:
LPDIRECTDRAWSURFACE lpSurf;
...
}
[B.app]
void UnitList::Init()
{
lpSurf = DDCreateSurface( 100, 100, NULL );
...
}
[B.h]
class UnitList
{
public:
LPDIRECTDRAWSURFACE lpSurf;
...
}
[B.app]
void UnitList::Init()
{
lpSurf = DDCreateSurface( 100, 100, NULL );
...
}
Hmmm...
Well, then you''re doing it the right way... The way I''d do it is add a logfile, checking everytime lpDraw is used whether it equals NULL or not.
Check that you initialise lpDraw properly... A very common reason for getting an access violation is that you''re using an uninitialised pointer...
disclaimer: this might sound silly/too basic to you, but a difficulty of giving advice online is not knowing how much the other one knows ...
/Ksero
Well, then you''re doing it the right way... The way I''d do it is add a logfile, checking everytime lpDraw is used whether it equals NULL or not.
///////////////// in main.hextern ofstream debug;///////////////// in (e.g) a.cppofstream debug("debug.out");///////////////// in b.cppLPDIRECTDRAWSURFACE DDCreateSurface(DWORD width, DWORD height, BOOL sysmem ){LPDIRECTDRAW lpdd = lpDraw->lpdd;if(lpDraw==NULL){ debug << "lpDraw was uninitialized in DDCreateSurface, "; debug << __FILE__; exit(1);}lpDraw->whatever=lpSurf;delete lpDraw;......}
Check that you initialise lpDraw properly... A very common reason for getting an access violation is that you''re using an uninitialised pointer...
disclaimer: this might sound silly/too basic to you, but a difficulty of giving advice online is not knowing how much the other one knows ...
/Ksero
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement