why is this prog crashing on delete?

This is a discussion on why is this prog crashing on delete? within the Windows Programming forums, part of the Platform Specific Boards category; ok, just got this beast working...but now when i try to reclaim memory it crashes (of course if i leave ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    why is this prog crashing on delete?

    ok,
    just got this beast working...but now when i try to reclaim memory it crashes (of course if i leave the pointers in existence everything is fine )
    but i want to reclaim the memory, what am i doing wrong here? i must be overwriting somewhere. thanks.
    Code:
    #include <windows.h>
    
    typedef HWND WindowHandle;
    typedef MSG WindowMessage;
    typedef WNDCLASS WindowClass;
    typedef HINSTANCE WindowInstance;
    typedef HDC DeviceHandle;
    typedef HICON WindowIcon;
    typedef HCURSOR WindowCursor;
    typedef HBRUSH WindowBrush;
    typedef WNDPROC WindowsProcedureVariable;
    
    
    static TCHAR szAppName[]=TEXT("Hungarians Suck");
    WindowHandle window;
    WindowMessage message;
    WindowClass _class;
    
    unsigned int * Style=&_class.style;
    WindowsProcedureVariable * WindowsProcedureName=&_class.lpfnWndProc;
    int * WindowClassExtraInfo=&_class.cbClsExtra;
    int * WindowExtraInfo=&_class.cbWndExtra;
    WindowInstance * WindowInstanceName=&_class.hInstance;
    WindowIcon * Icon=&_class.hIcon;
    WindowCursor * Cursor=&_class.hCursor;
    WindowBrush * Background=&_class.hbrBackground;
    const char ** MenuName=&_class.lpszMenuName;
    const char ** ClassName=&_class.lpszClassName;
    
    
    
    
    
    
    
    
    
    LRESULT CALLBACK WindowsProcedure(WindowHandle,UINT,WPARAM,LPARAM);
    
    int WINAPI WinMain (WindowInstance instance,WindowInstance prevInstance,LPSTR CommandLine,int ShowCommand)
    {
    
    
    	*Style=CS_HREDRAW|CS_VREDRAW;
    	*WindowsProcedureName=WindowsProcedure;
    	*WindowClassExtraInfo=0;
    	*WindowExtraInfo=0;
    	*WindowInstanceName=instance;
    	*Icon=LoadIcon(NULL,IDI_APPLICATION);
    	*Cursor=LoadCursor(NULL,IDC_ARROW);
    	*Background=(HBRUSH)GetStockObject(WHITE_BRUSH);
    	*MenuName=NULL;
    	*ClassName=szAppName;
    
    	if (!RegisterClass(&_class))
    	{
    		MessageBox(NULL,TEXT("This Program Requires Windows NT"),szAppName,MB_ICONERROR);
    		return 0;
    	}
    
    	window=CreateWindow(szAppName,
    												TEXT("Hello World From The Anti-Hungarian Society"),
    												WS_OVERLAPPEDWINDOW,
    												CW_USEDEFAULT,
    												CW_USEDEFAULT,
    												CW_USEDEFAULT,
    												CW_USEDEFAULT,
    												NULL,
    												NULL,
    												instance,
    												NULL);
    	ShowWindow(window,ShowCommand);
    	UpdateWindow(window);
    
    	while (GetMessage(&message,NULL,0,0))
    	{
    		TranslateMessage(&message);
    		DispatchMessage(&message);
    	}
    	/*
    	delete Style;
    	Style=NULL;
    	delete WindowsProcedureName;
    	WindowsProcedureName=NULL;
    	delete WindowClassExtraInfo;
    	WindowClassExtraInfo=NULL;
    	delete WindowExtraInfo;
    	WindowExtraInfo=NULL;
    	delete WindowInstanceName;
    	WindowInstanceName=NULL;
    	delete Icon;
    	Icon=NULL;
    	delete Cursor;
    	Cursor=NULL;
    	delete Background;
    	Background=NULL;
    	delete MenuName;
    	MenuName=NULL;
    	delete ClassName;
    	ClassName=NULL;
    	*/
    	return message.wParam;
    }
    
    LRESULT CALLBACK WindowsProcedure (WindowHandle window, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	DeviceHandle DeviceHandle;
    	PAINTSTRUCT pstruct;
    	RECT rect;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		return 0;
    	case WM_PAINT:
    		DeviceHandle=BeginPaint(window,&pstruct);
    		GetClientRect(window,&rect);
    		DrawText(DeviceHandle,TEXT("Hungarians Suck!"),-1,&rect,
    							DT_SINGLELINE|DT_CENTER|DT_VCENTER);
    		EndPaint(window,&pstruct);
    		return 0;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(window,message,wParam,lParam);
    }
    PHP and XML
    Let's talk about SAX

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    DUH!
    i never used new to allocate any memory...
    damn it's been a long night.
    Fixed now.
    PHP and XML
    Let's talk about SAX

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    I have a few comments. You start off correctly by attaching pointers to the actual variables. But you then try to delete the pointers. You can't. They point to real objects, not dynamic memory. And if you allocate memory to the pointers, then they cannot point to the variables since they now point to dynamic memory. The reason you use new is because you need a chunk of memory. Not to "allocate a pointer".

    1)
    int real = 256;
    int * ptr_real = &real;

    2)
    int * ptr_dyn = new int;
    delete ptr_dyn;


    The point is, you won't delete ptr_real, since it doesn't point to dynamic memory.

    Ok. The next suggestion I have may or may not appeal to you, just an idea:



    Code:
    
    class WindowClass : public WNDCLASS {
    public:
    UINT Style( int new_style = -1 ){
     if(new_style != -1) style = (UINT)new_style;
     return style;
    }
    UINT AddStyle( UINT additional ){
     return Style( additional | Style() );
    }
    UINT RemoveStyle( UINT remove ){
     return Style( Style() & ~remove );
    }
    WNDPROC ProcedureVariable( WNDPROC new_proc = 0 ){
     if(new_proc) lpfnWndProc = new_proc;
     return lpfnWndProc;
    }
    int ClassExtraBytes(int amount = -1){
     if(amount != -1) cbClsExtra = amount;
     return cbClsExtra;
    }
    int ExtraBytes(int amount = -1){
     if(amount != -1) cbWndExtra = amount;
     return cbWndExtra;
    }
    HINSTANCE Instance(HINSTANCE some_other_instance = 0){
     if(some_other_instance) hInstance = some_other_instance;
     return (HINSTANCE)hInstance;
    }
    HICON Icon(HICON new_icon = 0){
     if(new_icon) hIcon = new_icon;
     return hIcon;
    }
    HCURSOR Cursor(HCURSOR new_cursor= 0){
     if(new_cursor) hCursor = new_cursor;
     return hCursor;
    }
    HBRUSH Background(HBRUSH new_background = 0){
     if(new_background) hbrBackground = new_background;
     return hbrBackground;
    }
    LPCSTR MenuName(LPCSTR new_name = 0){
     if(new_name ) const_cast<char*>(lpszMenuName) = new_name;
     return lpszMenuName;
    }
    LPCSTR ClassName(LPCSTR new_name = 0){
     if(new_name ) const_cast<char*>(lpszClassName) = new_name;
     return lpszClassName;
    }
    void Reset(){
     memset(this, 0, sizeof(*this));
    }
    WindowClass(){
     Reset();
     Style(CS_HREDRAW|CS_VREDRAW);
     Instance(GetModuleHandle(NULL));
     Background((HBRUSH)GetStockObject(WHITE_BRUSH));
     Icon(LoadIcon(NULL,IDI_APPLICATION));
     Cursor(LoadCursor(NULL,IDC_ARROW));
    }
    };


    The major advantages of this setup are:

    1) Can be passed into functions expecting a WNDCLASS struct.
    3) Exploit the constructor mechanism to create defaults.
    4) Add as many member functions as you can think of.
    5) Use same functions to get/set data.


    Prerequisite:

    - Only add member functions, don't add more variables to the structure.


    Let's see it in action now:







    Code:
    
    typedef HWND WindowHandle;
    typedef MSG WindowMessage;
    
    typedef HINSTANCE WindowInstance;
    typedef HDC DeviceHandle;
    typedef HICON WindowIcon;
    typedef HCURSOR WindowCursor;
    typedef HBRUSH WindowBrush;
    typedef WNDPROC WindowsProcedureVariable;
    
    
    static TCHAR szAppName[]=TEXT("Hungarians Suck");
    WindowHandle window;
    WindowMessage message;
    
    WindowClass _class; 
    
    LRESULT CALLBACK  WindowsProcedure(WindowHandle,UINT,WPARAM,LPARAM);
    
    int WINAPI WinMain (WindowInstance instance,WindowInstance prevInstance,LPSTR CommandLine,int ShowCommand)
    {
    	_class.AddStyle(CS_DBLCLKS);
    	_class.ProcedureVariable(WindowsProcedure);
        _class.Instance(instance);
        _class.ClassName( TEXT("Hungarians Suck") );
    
    
    	if (!RegisterClass(&_class))
    	{
    		MessageBox(NULL,TEXT("This Program Requires Windows NT"),szAppName,MB_ICONERROR);
    		return 0;
    	}
    
    	window=CreateWindow(_class.ClassName(),
    												TEXT("Hello World From The Anti-Hungarian Society"),
    												WS_OVERLAPPEDWINDOW,
    												CW_USEDEFAULT,
    												CW_USEDEFAULT,
    												CW_USEDEFAULT,
    												CW_USEDEFAULT,
    												NULL,
    												NULL,
    												_class.Instance(),
    												NULL);
    	ShowWindow(window,ShowCommand);
    	UpdateWindow(window);
    
    	while (GetMessage(&message,NULL,0,0))
    	{
    		TranslateMessage(&message);
    		DispatchMessage(&message);
    	}
     	return message.wParam;
    }
    
    LRESULT CALLBACK WindowsProcedure (WindowHandle window, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	DeviceHandle DeviceHandle;
    	PAINTSTRUCT pstruct;
    	RECT rect;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		return 0;
    	case WM_PAINT:
    		DeviceHandle=BeginPaint(window,&pstruct);
    		GetClientRect(window,&rect);
    		DrawText(DeviceHandle,TEXT("Hungarians Suck!"),-1,&rect,
    							DT_SINGLELINE|DT_CENTER|DT_VCENTER);
    		EndPaint(window,&pstruct);
    		return 0;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(window,message,wParam,lParam);
    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST inser method seems to be crashing my prog
    By ray_broome in forum C++ Programming
    Replies: 12
    Last Post: 04-22-2005, 12:02 PM
  2. Crashing on prog quit
    By Hunter2 in forum Game Programming
    Replies: 11
    Last Post: 09-07-2002, 12:13 PM
  3. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  4. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM
  5. sprintf crashing my prog?
    By Eber Kain in forum C++ Programming
    Replies: 11
    Last Post: 11-21-2001, 03:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21