WinApi - Błąd z pamięcią podczas zamykania okna potomnego

0

Witam.
Stworzyłem okno potomne z polami tekstowymi i przyciskiem.
Chcę aby po naciśnięciu przycisku okno się wyłączało i dane z pól tekst. zapisywało do zmiennych.
Zapisywanie działa dopóki jest wypełnione co najmniej jedno pole. Gdy nie wypełnię nic tylko od razu zapisuję pojawia się błąd:

App.exe has triggered a breakpoint.

while( GetMessage( & msg1, NULL, 0, 0 ) )
{
    TranslateMessage( & msg1 );
    DispatchMessage( & msg1 ); // <- tutaj błąd: ((msg1).hwnd)->unused | <Unable to read memory>
}
EnableWindow( window_main, TRUE );
return msg1.wParam;
LRESULT CALLBACK WndProc( HWND window_main, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
    case WM_CLOSE:
        DestroyWindow( window_main );
        if( new_Window != NULL ) DestroyWindow( new_Window );
        
        break;
        
    case WM_DESTROY:
        PostQuitMessage( 0 );
        break;
        
    case WM_COMMAND:
        {
            switch( wParam ) {
                
            case Add_something:
                {
                    HWND hText7;
                    new_Window = CreateWindowEx( WS_EX_CLIENTEDGE, ClassName, "New Window", WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 550, 230, window_main, NULL, hInstance, NULL );
                    hText1 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 90, 20, new_Window,( HMENU ) ID_TEXT_ADD_RESOURCES_1, hInstance, NULL );
                    hText2 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 350, 20, 90, 20, new_Window,( HMENU ) ID_TEXT_ADD_RESOURCES_2, hInstance, NULL );
                    hText3 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 60, 90, 20, new_Window,( HMENU ) ID_TEXT_ADD_RESOURCES_3, hInstance, NULL );
                    hText4 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 350, 60, 90, 20, new_Window,( HMENU ) ID_TEXT_ADD_RESOURCES_4, hInstance, NULL );
                    hText5 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 100, 90, 20, new_Window,( HMENU ) ID_TEXT_ADD_RESOURCES_5, hInstance, NULL );
                    hText6 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 350, 100, 90, 20, new_Window,( HMENU ) ID_TEXT_ADD_RESOURCES_6, hInstance, NULL );
                    hText7 = CreateWindowEx( 0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY, 30, 20, 60, 20, new_Window, NULL, hInstance, NULL );
                    hButton = CreateWindowEx( 0, "BUTTON", "Zapisz", WS_CHILD | WS_VISIBLE | WS_BORDER, 230, 140, 80, 20, new_Window,( HMENU ) ID_BUTTON_ADD_RESOURCES, hInstance, NULL );
                    
                    SetWindowText( hText7, "Option 1" );
                    
                    EnableWindow( window_main, FALSE );
                    while( GetMessage( & msg1, NULL, 0, 0 ) )
                    {
                        TranslateMessage( & msg1 );
                        DispatchMessage( & msg1 );
                    }
                    EnableWindow( window_main, TRUE );
                    return msg1.wParam;
                    
                } break;
                
            case Print:
                {
                }
                
            case ID_BUTTON_ADD_RESOURCES:
                {
                    string temp;
                    DWORD dlugosc;
                    LPSTR Bufor;
                    bool Check_Bufor, Check_Status = TRUE;
                    int value;
                    
                    dlugosc = GetWindowTextLength( hText1 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText1, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->a = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
                    
                    dlugosc = GetWindowTextLength( hText2 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText2, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->b = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
                    
                    dlugosc = GetWindowTextLength( hText3 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText3, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->c = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
                    
                    dlugosc = GetWindowTextLength( hText4 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText4, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->d = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
                    
                    dlugosc = GetWindowTextLength( hText5 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText5, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->e = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
                    
                    dlugosc = GetWindowTextLength( hText6 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText6, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->f = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
                    
                    if( Check_Status == TRUE ) {
                        GlobalFree( Bufor );
                        EnableWindow( window_main, TRUE );
                        DestroyWindow( new_Window );
                    }
                    else if( Check_Status == FALSE ) {
                        MessageBox( new_Window, "Błąd przy wpisywaniu. \nMożna wpisywać tylko cyfry.", "ERROR", MB_OK | MB_ICONWARNING );
                    };
                } break;
            }
        } break;
        
        default:
        return DefWindowProc( window_main, msg, wParam, lParam );
    }
    
    return 0;
}
  1. Mógłby ktoś wytłumaczyć dlaczego jet błąd gdy wszystkie pola zostaną puste, a gdy chociaż jeden jest wypełniony to już nie?
  2. I jak można to naprawić?
0

Daj kod który można skompilować, a nie jego połowę.

                    dlugosc = GetWindowTextLength( hText1 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText1, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->a = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };
 
                    dlugosc = GetWindowTextLength( hText2 );
                    if( dlugosc > 0 ) {
                        Bufor =( LPSTR ) GlobalAlloc( GPTR, dlugosc + 1 );
                        GetWindowText( hText2, Bufor, dlugosc + 1 );
                        Check_Bufor = Check( Bufor );
                        if( Check_Bufor == true ) {
                            value = boost::lexical_cast < int >( Bufor );
                            temp_class->b = value;
                        }
                        else {
                            Check_Status = FALSE;
                        }
                    };

To się nazywa metoda Copy'ego-Paste'a.

0

To się nazywa metoda Copy'ego-Paste'a.

Tzn? Coś tu jest źle czy jak?

win_main.cpp :

#include <windows.h>
#include"resource.h"
#include<string>
#include"Class_Definition.h"
#include <boost/lexical_cast.hpp>
using namespace std;

#define ID_TEXT_ADD_RESOURCES_1 200
#define ID_TEXT_ADD_RESOURCES_2 201
#define ID_TEXT_ADD_RESOURCES_3 202
#define ID_TEXT_ADD_RESOURCES_4 203
#define ID_TEXT_ADD_RESOURCES_5 204
#define ID_TEXT_ADD_RESOURCES_6 205
#define ID_BUTTON_ADD_RESOURCES 206

LPSTR ClassName = "Window_Class";
LPSTR Child_Window_1 = "Child_Class_1";

MSG Messange;
MSG msg1;

HINSTANCE hInstance;
int nCmdShow;

HWND new_Window;
HWND window_main;
HWND hText, hText1, hText2, hText3, hText4, hText5, hText6;
HWND hButton;

LRESULT CALLBACK WndProc(HWND window_main, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProcChild(HWND new_window, UINT msg1, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DlgProc(HWND menu, UINT Msg, WPARAM wParam, LPARAM lParam);

Temp_Class * temp_class = new Temp_Class;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	WNDCLASSEX wc, wc2;

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = ClassName;
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	wc2.cbSize = sizeof(WNDCLASSEX);
	wc2.style = 0;
	wc2.lpfnWndProc = WndProcChild;
	wc2.cbClsExtra = 0;
	wc2.cbWndExtra = 0;
	wc2.hInstance = hInstance;
	wc2.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc2.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc2.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc2.lpszMenuName = NULL;
	wc2.lpszClassName = Child_Window_1;
	wc2.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "ERROR", "ERROR", MB_ICONEXCLAMATION | MB_OK);
		return 1;
	}

	if (!RegisterClassEx(&wc2))
	{
		MessageBox(NULL, "ERROR", "ERROR", MB_ICONEXCLAMATION | MB_OK);
		return 1;
	}


	HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(101));
	window_main = CreateWindowEx(WS_EX_CLIENTEDGE, ClassName, "Main Window", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, hMenu, hInstance, NULL);

	if (window_main == NULL)
	{
		MessageBox(NULL, "ERROR", "ERROR", MB_ICONEXCLAMATION);
		return 1;
	}

	ShowWindow(window_main, nCmdShow); // Pokaż okienko...
	UpdateWindow(window_main);

	while (GetMessage(&Messange, NULL, 0, 0))
	{
		TranslateMessage(&Messange);
		DispatchMessage(&Messange);
	}
	return Messange.wParam;
	return 0;
}

LRESULT CALLBACK WndProc(HWND window_main, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE:
		DestroyWindow(window_main);
		if (new_Window != NULL) DestroyWindow(new_Window);
		break;

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	case WM_COMMAND:
	{
		switch (wParam){

		case Add_something:
		{
			HWND hText7;
			new_Window = CreateWindowEx(WS_EX_CLIENTEDGE, ClassName, "New Window", WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 550, 230, window_main, NULL, hInstance, NULL);
			hText1 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 20, 90, 20, new_Window, (HMENU)ID_TEXT_ADD_RESOURCES_1, hInstance, NULL);
			hText2 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 350, 20, 90, 20, new_Window, (HMENU)ID_TEXT_ADD_RESOURCES_2, hInstance, NULL);
			hText3 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 60, 90, 20, new_Window, (HMENU)ID_TEXT_ADD_RESOURCES_3, hInstance, NULL);
			hText4 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 350, 60, 90, 20, new_Window, (HMENU)ID_TEXT_ADD_RESOURCES_4, hInstance, NULL);
			hText5 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 100, 100, 90, 20, new_Window, (HMENU)ID_TEXT_ADD_RESOURCES_5, hInstance, NULL);
			hText6 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, 350, 100, 90, 20, new_Window, (HMENU)ID_TEXT_ADD_RESOURCES_6, hInstance, NULL);
			hText7 = CreateWindowEx(0, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_READONLY, 30, 20, 60, 20, new_Window, NULL, hInstance, NULL);
			hButton = CreateWindowEx(0, "BUTTON", "Zapisz", WS_CHILD | WS_VISIBLE | WS_BORDER, 230, 140, 80, 20, new_Window, (HMENU)ID_BUTTON_ADD_RESOURCES, hInstance, NULL);
			
			SetWindowText(hText7, "Option 1");

			EnableWindow(window_main, FALSE);
			while (GetMessage(&msg1, NULL, 0, 0))
			{
				TranslateMessage(&msg1);
				DispatchMessage(&msg1);
			}
			EnableWindow(window_main, TRUE);
			return msg1.wParam;

		}break;

		case Print:
		{
			string str = boost::lexical_cast<std::string>(temp_class->a);
			MessageBox(window_main, str.c_str(), "a", MB_OK);
			str = boost::lexical_cast<std::string>(temp_class->b);
			MessageBox(window_main, str.c_str(), "b", MB_OK);
			str = boost::lexical_cast<std::string>(temp_class->c);
			MessageBox(window_main, str.c_str(), "c", MB_OK);
			str = boost::lexical_cast<std::string>(temp_class->d);
			MessageBox(window_main, str.c_str(), "d", MB_OK);
			str = boost::lexical_cast<std::string>(temp_class->e);
			MessageBox(window_main, str.c_str(), "e", MB_OK);
			str = boost::lexical_cast<std::string>(temp_class->f);
			MessageBox(window_main, str.c_str(), "f", MB_OK);
		}

		case ID_BUTTON_ADD_RESOURCES:
		{
			string temp;
			DWORD dlugosc;
			LPSTR Bufor;
			bool Check_Bufor, Check_Status = TRUE;
			int value;

			dlugosc = GetWindowTextLength(hText1);
			if (dlugosc > 0){
				Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
				GetWindowText(hText1, Bufor, dlugosc + 1);
				Check_Bufor = Check(Bufor);
				if (Check_Bufor == true){
					value = boost::lexical_cast<int>(Bufor);
					temp_class->a = value;
				}
				else{
					Check_Status = FALSE;
				}
			};

			dlugosc = GetWindowTextLength(hText2);
			if (dlugosc > 0){
				Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
				GetWindowText(hText2, Bufor, dlugosc + 1);
				Check_Bufor = Check(Bufor);
				if (Check_Bufor == true){
					value = boost::lexical_cast<int>(Bufor);
					temp_class->b = value;
				}
				else{
					Check_Status = FALSE;
				}
			};

			dlugosc = GetWindowTextLength(hText3);
			if (dlugosc > 0){
				Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
				GetWindowText(hText3, Bufor, dlugosc + 1);
				Check_Bufor = Check(Bufor);
				if (Check_Bufor == true){
					value = boost::lexical_cast<int>(Bufor);
					temp_class->c = value;
				}
				else{
					Check_Status = FALSE;
				}
			};

			dlugosc = GetWindowTextLength(hText4);
			if (dlugosc > 0){
				Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
				GetWindowText(hText4, Bufor, dlugosc + 1);
				Check_Bufor = Check(Bufor);
				if (Check_Bufor == true){
					value = boost::lexical_cast<int>(Bufor);
					temp_class->d = value;
				}
				else{
					Check_Status = FALSE;
				}
			};

			dlugosc = GetWindowTextLength(hText5);
			if (dlugosc > 0){
				Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
				GetWindowText(hText5, Bufor, dlugosc + 1);
				Check_Bufor = Check(Bufor);
				if (Check_Bufor == true){
					value = boost::lexical_cast<int>(Bufor);
					temp_class->e = value;
				}
				else{
					Check_Status = FALSE;
				}
			};

			dlugosc = GetWindowTextLength(hText6);
			if (dlugosc > 0){
				Bufor = (LPSTR)GlobalAlloc(GPTR, dlugosc + 1);
				GetWindowText(hText6, Bufor, dlugosc + 1);
				Check_Bufor = Check(Bufor);
				if (Check_Bufor == true){
					value = boost::lexical_cast<int>(Bufor);
					temp_class->f = value;
				}
				else{
					Check_Status = FALSE;
				}
			};

			if (Check_Status == TRUE){
				GlobalFree(Bufor);
				EnableWindow(window_main, TRUE);
				DestroyWindow(new_Window);
			}
			else if (Check_Status == FALSE){
				MessageBox(new_Window, "Błąd przy wpisywaniu. \nMożna wpisywać tylko cyfry.", "ERROR", MB_OK | MB_ICONWARNING);
			};
		}break;
		}
	}break;

	default:
		return DefWindowProc(window_main, msg, wParam, lParam);
	}

	return 0;
}

LRESULT CALLBACK WndProcChild(HWND new_Window, UINT msg1, WPARAM wParam, LPARAM lParam)
{
	switch (msg1)
	{
	case WM_DESTROY:
		EnableWindow(window_main, TRUE);
		PostQuitMessage(0);
		break;

	case WM_CLOSE:
		EnableWindow(window_main, TRUE);
		DestroyWindow(new_Window);
		break;

	default:
		return DefWindowProc(new_Window, msg1, wParam, lParam);
	}

	return 0;
}

Class_Definition.h :

#ifndef Class_Definition
#define Class_Definition

#include<iostream>
using namespace std;

class Temp_Class{
public:
	long long a;
	long long b;
	long long c;
	long long d;
	long long e;
	long long f;

	Temp_Class(long long a1 = 0, long long a2 = 0, long long a3 = 0, long long a4 = 0, long long a5 = 0, long long a6 = 0){
		a = a1;
		b = a2;
		c = a3;
		d = a4;
		e = a5;
		f = a6;
	}
};

bool Check(string napis){
	char * tablica = new char(napis.size() + 1);
	strcpy(tablica, napis.c_str());

	char str[] = "QWEĘRTYUIÓPAĄSŚDFGHJKLŁZŻXŹCĆVBNŃMqweęrtyuioópaąsśdfghjklzżxźcćvbnńm`[email protected]#$%^&*()_+-=[]{};:'|\?,.<>\/";
	char * indeks = strpbrk(tablica, str);

	if (indeks == NULL){
		return true;
	}
	else{
		return false;
	}

	delete[] tablica;
	delete indeks;
}

#endif
0

Tzn? Coś tu jest źle czy jak?

Powtarzasz wielokrotnie ten sam kod dla różnych zmiennych, zamiast zrobić jedną funkcję, która skróci kod i poprawi czytelność.

Tu masz zredukowaną (i zracjonalizowaną) funkcję Check:

bool Check(const string &napis)
{
    const char *keys = "QWEĘRTYUIÓPAĄSŚDFGHJKLŁZŻXŹCĆVBNŃMqweęrtyuioópaąsśdfghjklzżxźcćvbnńm`[email protected]#$%^&*()_+-=[]{};:'|\?,.<>\/";
    return !strpbrk(napis.c_str(), keys);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1