What is wrong with my code? (C++)

Started by
8 comments, last by Drew_Benton 19 years ago
Well, ive done this kind of double buffer thing before but i made a new program to just test out all my new functions, and my Borland compiler come with this error "Case bypasses initializtion of a local variable", and the line of code that it says is errored is this case WM_DESTROY: DeInit(&Buffer); break; and the functions DeInit is void DeInit(BUFFER *buffer) { SelectObject(buffer->Back, buffer->hOldBitmap); DeleteDC(buffer->Back); DeleteDC(buffer->Bitmap); PostQuitMessage(0); }; And i dont know if that is the errored part, but please look at it and if it is fine, please make a suggestion of what is probably wrong, or tell me what the compiler means.
Sure i will write my signature, just gimme a piece of paper
Advertisement
Quote:Original post by dustydoodoo
Borland compiler come with this error "Case bypasses initializtion of a local variable", and the line of code that it says is errored is this


That error means that somewhere in your switch statement, you make a local variable. It is just warning you of this. If you can post your entire switch statement using the [source] [/source] tags, I'm sure we can point out where it is happening at.
Is it possible in your case statement for the initialization of Buffer to not happen but follow a code path where it is used?


#dth-0
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wparam, LPARAM lparam){    switch(iMsg){                case WM_CREATE:            HBITMAP Bit = LoadBitmap("Bitmap.bmp");            DisplayBitmap(&Buffer, Bit, 100, 100);            SwapBuffers(&Buffer);             Init(&Buffer, hwnd);            break;                case WM_DESTROY:            DeInit(&Buffer);            PostQuitMessage(0);            break;    }        return DefWindowProc (hwnd, iMsg, wparam, lparam);}  
Sure i will write my signature, just gimme a piece of paper
Yeap:
HBITMAP Bit = LoadBitmap("Bitmap.bmp");
is what's causing that warning. Ideally, it should be a global variable to you can free the resources allocated to it at the end of your program. So you can just do this:
// Top of programHBITMAP Bit;// In that codeBit = LoadBitmap("Bitmap.bmp");


The error should go away when you make those changes.
or try:

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wparam, LPARAM lparam){    switch(iMsg){                case WM_CREATE:        {            HBITMAP Bit = LoadBitmap("Bitmap.bmp");            DisplayBitmap(&Buffer, Bit, 100, 100);            SwapBuffers(&Buffer);             Init(&Buffer, hwnd);        }        break;                case WM_DESTROY:            DeInit(&Buffer);            PostQuitMessage(0);            break;    }        return DefWindowProc (hwnd, iMsg, wparam, lparam);}  


or

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wparam, LPARAM lparam){    switch(iMsg){                case WM_CREATE:            DisplayBitmap(&Buffer, LoadBitmap("Bitmap.bmp"), 100, 100);            SwapBuffers(&Buffer);             Init(&Buffer, hwnd);            break;                case WM_DESTROY:            DeInit(&Buffer);            PostQuitMessage(0);            break;    }        return DefWindowProc (hwnd, iMsg, wparam, lparam);}  

try to avoid globals where ever possible. Although in this case it may be a good idea if you are loading Bitmap.bmp in several place. Using a global in this instance would mean that you could load it in once, in some kind of initialisation function.
Gary.Goodbye, and thanks for all the fish.
Quote:Original post by garyfletcher
or try:
*** Source Snippet Removed ***
or
*** Source Snippet Removed ***


While that is a good suggestion, consider this from MSDN:

Quote:The application must call the DeleteObject function to delete each bitmap handle returned by the LoadBitmap function.


The only other way to get around this would be if you made a class that contained a HBITMAP, to which is a lot more work than just having one global variable wrapped in a namespace at the top [smile]. Not only that, the function call:
DisplayBitmap(&Buffer, LoadBitmap("Bitmap.bmp"), 100, 100);

should end up being called in more places than just the WM_CREATE message, so either a class or a global would have to be used regardless.
Indeed...is what I said..:)
Gary.Goodbye, and thanks for all the fish.
Hey, thanks (the person who said to make it a global variable) IT WORKS, ITS ALIVE!!! lol, thanks, oh and what kind of resorses are alocated to the bitmap? and and you said, "you can free the resources allocated to it at the end of your program." well how "can" i, or does it just do it it self? that might sound stupid, but im really tired.
Sure i will write my signature, just gimme a piece of paper
You will want to do what Garyfletcher and myself have agreed is the better way [wink]. Something like this (kind of messy, but quick example):
struct cData{HBITMAP Bit;public:   cData();   ~cData()   {      DeleteObject(Bit);   }   void Load( std::string name )   {         Bit = LoadBitmap( name );   }};


At the top you will have something like:
cData g_MyData;

Then in your initialize code, you will have:
g_MyData.Load("Bitmap.bmp")

Then in your draw:
DisplayBitmap(&Buffer, g_MyData.Bit, 100, 100);

If you see in the dtor, there is a call to DeleteObject, so it will do an auto cleanup. You might also want to consider using this with your Buffer variable as well so you can wrap everything up!

struct cData{HBITMAP Bit;BUFFER *buffer;public:   cData() { };   ~cData() { };   void Destroy()   {      SelectObject(buffer->Back, buffer->hOldBitmap);      DeleteDC(buffer->Back);      DeleteDC(buffer->Bitmap);      DeleteObject(Bit);   }   void Load( std::string name )   {      Bit = LoadBitmap( name );   }   void Draw( int x, int y)   {      DisplayBitmap(&Buffer, Bit, X, Y);      SwapBuffers(&Buffer);    }};...case WM_CREATE:        {            g_MyData.Draw( 100, 100 );                  Init(&Buffer, hwnd); // will need to fix this        }case WM_DESTROY:   g_MyData.Destroy();

This topic is closed to new replies.

Advertisement