JoeC said:
Here let me post more code. I got this from a book.
I have a hard time believing that. Either you copied it wrong or you should
post the title of the book so that everybody can stay away from it.
class engine{
protected:
static engine* pengine;
HINSTANCE hin;
HWND hwnd;
TCHAR szClass[32];
TCHAR sc[32];
WORD icon, iconsm;
int wth, hght;
int delay;
bool sleep;
public:
engine(HINSTANCE, LPTSTR, LPTSTR, WORD, WORD, int, int);
virtual ~engine();
bool init(int);
LRESULT event(HWND, UINT, WPARAM, LPARAM);
static engine* gengine(){return pengine;}
HINSTANCE instance(){return hin;}
HWND window(){return hwnd;}
void window(HWND h){hwnd = h;}
LPSTR title(){return sc;}
WORD gicon(){return icon;}
WORD giconsm(){return iconsm;}
int width(){return wth;}
int height(){return hght;}
int gdelay(){return delay;}
void frate(int rate){delay = 1000 /rate;}
bool gsleep(){return sleep;}
void gsleep(bool sl){sleep = sl;}
};
That doesn't look too good. The abundance of getter and setter methods is a
sure give-away of poor design.
bool initialize(HINSTANCE hin){
e = new engine(hin, TEXT("Window!"), TEXT("Happy
Holloween."),IDI_Icon, IDI_Icons,800 ,480);
if(e == NULL){return false;}
The code above shows some serious misunderstanding about new. It assumes
that new could return 0. This is not the case. If new is unable to allocate
memory or to construct the object as required, it will throw instead of
returning at all. If you want a 0 return instead, you need to use the
nothrow version of new.
e->frate(10);
ghin = hin;
What is ghin? Looks like an undeclared identifier.
So this function news an object, sets the local pointer variable e to point
there and then returns without telling anybody about e and without deleting
e? That looks like a leak.
engine *engine:
engine = NULL;
If this really is the only place where engine:
engine is set, you have a
serious problem down the road.
int WINAPI WinMain(HINSTANCE hin, HINSTANCE phin, PSTR CmdLine,
int CmdShow){
MSG msg;
static int Trigger = 0;
int TickCount;
if(initialize(hin)){
if(!engine::gengine()->init(CmdShow)){return false;}
Here you are dereferencing engine:
engine through the call to gengine(). If
pengine still equals 0, you have undefined behavior.
At first sight, I was thinking that you may have come across the singleton
pattern. However, I cannot confirm that from the more code you posted.
Please note that the most important parts would be the places where pengine
is set, where the pointee of pengine is created, and where it is destroyed
(if at all). If you post those parts, it may become aparent what is going
on. So far, we have to assume that pengine==NULL, which seems a little
unlikely.
Best
Kai-Uwe Bux