memory leak

xray (5)
Hi

I have an issue with this code, it has memory leak.
it is probably due to this

FILENAME *arrFilename = (FILENAME *)malloc(10000 * sizeof *arrFilename);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
//#define PORTALPATH "\\\\srv-mantis\\TechDocs\\Portal\\RDP\\password"
#define PORTALPATH "c:\\temp"

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HINSTANCE g_hinst;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
	HWND hwnd;
	MSG  msg ;    
	WNDCLASS wc = {0};
	wc.lpszClassName = TEXT("Application");
	wc.hInstance     = hInstance ;
	wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
	wc.lpfnWndProc   = WndProc ;
	wc.hCursor       = LoadCursor(0,IDC_ARROW);

	g_hinst = hInstance;
  
	RegisterClass(&wc);
	hwnd = CreateWindow(wc.lpszClassName, TEXT("Combo Box"),
				WS_OVERLAPPEDWINDOW | WS_VISIBLE,
				300, 300, 270, 170, 0, 0, hInstance, 0);  


	while( GetMessage(&msg, NULL, 0, 0)) {
	DispatchMessage(&msg);
	}
	return (int) msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{

	static HWND hwndCombo, hwndStatic;
	const TCHAR *items[] = { TEXT("FreeBSD"), TEXT("OpenBSD"), TEXT("Ubuntu"), TEXT("Solaris"), TEXT("1"), TEXT("2"), TEXT("3") };
	int i;
	LRESULT sel = 0;

	// get array of files

	typedef char FILENAME[MAX_PATH + 1];
	// get number of files and then use this size to create the array
	FILENAME *arrFilename = (FILENAME *)malloc(10000 * sizeof *arrFilename);
    int j,k; 
	char path[100] = PORTALPATH;

	// directory var
	DIR *dir;
    struct dirent *ent;

	// set directory of password files 
	dir = opendir(path);
	
	// get filenames
	if (dir != NULL){
		
		j = 0;
		while((ent = readdir (dir)) != NULL) {
			//printf("j = %d fname: %s\n",j,ent->d_name) ;
			//copy string to array

			strcpy(arrFilename[j],ent->d_name);
			j++;
		}
		closedir (dir);
	} else {
      //could not open directory 
      perror (path);
      //return EXIT_FAILURE;
    }
	// get array of files END


	switch(msg)  
	{
		case WM_CREATE:
			hwndCombo = CreateWindow(TEXT("combobox"), NULL, 
					WS_CHILD | WS_VISIBLE | CBS_DROPDOWN | WS_VSCROLL,
					10, 10, 120, 510, hwnd, NULL, g_hinst, NULL);   

			CreateWindow(TEXT("button"), TEXT("Drop down"), 
					WS_CHILD | WS_VISIBLE,
					150, 10, 90, 25, hwnd, (HMENU)1, g_hinst, NULL); 

			hwndStatic = CreateWindow(TEXT("static"), TEXT(""), 
					WS_CHILD | WS_VISIBLE,
					150, 80, 90, 25, hwnd, NULL, g_hinst, NULL); 
			
			// enter filenames into combobox
			
			for ( i = 0; i < 7; i++ ) {
				SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]);
			}
			
			break;

		case WM_COMMAND:
			if (HIWORD(wParam) == BN_CLICKED) {
				SendMessage(hwndCombo, CB_SHOWDROPDOWN, (WPARAM) TRUE, 0);
			}
             
			if ( HIWORD(wParam) == CBN_SELCHANGE) {               
				sel = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
				SetWindowText(hwndStatic, items[sel]);
				SetFocus(hwnd);
			}

			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break; 
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}
naraku9333 (923)
So, why not free the memory?
vlad from moscow (3112)
It is not clear why do you read filenames and do not use them each time when the function is called?
xray (5)
because I don't know how to convert to UNICODE in here:


1
2
3
			for ( i = 0; i < 7; i++ ) {
				SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) items[i]);
			}


also I was told that WndProc is being run many times, so I need move the code between these comments (// get array of files >>> // get array of files END) to a function and call it but don't know how, and where to put it

Last edited on
Topic archived. No new replies allowed.