Skip to content

Commit 8a8a890

Browse files
committed
Merge pull request #13 from quangnh89/master
fix memory leak and close About dialog when clicking "Close" button
2 parents bdab7ae + a6a2a70 commit 8a8a890

File tree

2 files changed

+104
-63
lines changed

2 files changed

+104
-63
lines changed

NppJSONViewer/JSONDialog.cpp

Lines changed: 103 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,22 @@ inserts a node in the tree
5151
HTREEITEM JSONDialog::insertToTree(HWND hWndDlg,HTREEITEM parent,char *text)
5252
{
5353
TV_INSERTSTRUCT tvinsert;
54-
54+
HTREEITEM item = NULL;
5555
tvinsert.hParent=parent;
5656
tvinsert.hInsertAfter=TVI_LAST;
5757
tvinsert.item.mask=TVIF_TEXT;
5858

5959
int len = strlen(text) + 1;
6060
wchar_t *w_msg = new wchar_t[len];
61-
memset(w_msg, 0, len);
62-
MultiByteToWideChar(CP_UTF8, NULL, text, -1, w_msg, len);
61+
if (w_msg)
62+
{
63+
memset(w_msg, 0, len);
64+
MultiByteToWideChar(CP_UTF8, NULL, text, -1, w_msg, len);
6365

64-
tvinsert.item.pszText=w_msg;
65-
HTREEITEM item=(HTREEITEM)SendDlgItemMessage(hWndDlg,IDC_TREE1,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
66+
tvinsert.item.pszText = w_msg;
67+
item = (HTREEITEM)SendDlgItemMessage(hWndDlg, IDC_TREE1, TVM_INSERTITEM, 0, (LPARAM)&tvinsert);
68+
delete[] w_msg; // fix memory leak
69+
}
6670

6771
return item;
6872
}
@@ -92,21 +96,30 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
9296
{
9397
int len=strlen(json_root->parent->text)+3+strlen(json_root->text)+1;
9498
char *txt=new char[len];
95-
memset(txt, 0, len);
96-
char *unesc_text=json_unescape(json_root->text);
97-
char *unesc_parent_text=json_unescape(json_root->parent->text);
98-
sprintf(txt,"%s : %s",unesc_parent_text,unesc_text);
99-
free(unesc_text);
100-
free(unesc_parent_text);
101-
102-
len = strlen(txt) + 1;
103-
wchar_t *w_txt = new wchar_t[len];
104-
memset(w_txt, 0, len);
105-
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
106-
107-
t.pszText=w_txt;
108-
t.mask=TVIF_TEXT;
109-
::SendDlgItemMessage(hWndDlg,IDC_TREE1,TVM_SETITEM,0,(LPARAM)&t);
99+
if (txt)
100+
{
101+
memset(txt, 0, len);
102+
char *unesc_text = json_unescape(json_root->text);
103+
char *unesc_parent_text = json_unescape(json_root->parent->text);
104+
sprintf(txt, "%s : %s", unesc_parent_text, unesc_text);
105+
free(unesc_text);
106+
free(unesc_parent_text);
107+
108+
len = strlen(txt) + 1;
109+
wchar_t *w_txt = new wchar_t[len];
110+
if (w_txt)
111+
{
112+
memset(w_txt, 0, len);
113+
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
114+
115+
t.pszText = w_txt;
116+
t.mask = TVIF_TEXT;
117+
::SendDlgItemMessage(hWndDlg, IDC_TREE1, TVM_SETITEM, 0, (LPARAM)&t);
118+
delete[] w_txt; //fix memory leak
119+
}
120+
121+
delete[] txt; //fix memory leak
122+
}
110123
}
111124
}else
112125
{
@@ -129,19 +142,29 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
129142
{
130143
int len=strlen(json_root->parent->text)+3+strlen(json_root->text)+1;
131144
char *txt=new char[len];
132-
memset(txt, 0, len);
133-
char *unesc_parent_text=json_unescape(json_root->parent->text);
134-
sprintf(txt,"%s : %s",unesc_parent_text,json_root->text);
135-
free(unesc_parent_text);
136-
137-
len = strlen(txt) + 1;
138-
wchar_t *w_txt = new wchar_t[len];
139-
memset(w_txt, 0, len);
140-
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
141-
142-
t.pszText=w_txt;
143-
t.mask=TVIF_TEXT;
144-
::SendDlgItemMessage(hWndDlg,IDC_TREE1,TVM_SETITEM,0,(LPARAM)&t);
145+
if (txt)
146+
{
147+
memset(txt, 0, len);
148+
char *unesc_parent_text = json_unescape(json_root->parent->text);
149+
sprintf(txt, "%s : %s", unesc_parent_text, json_root->text);
150+
free(unesc_parent_text);
151+
152+
len = strlen(txt) + 1;
153+
wchar_t *w_txt = new wchar_t[len];
154+
if (w_txt)
155+
{
156+
memset(w_txt, 0, len);
157+
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
158+
159+
t.pszText = w_txt;
160+
t.mask = TVIF_TEXT;
161+
::SendDlgItemMessage(hWndDlg, IDC_TREE1, TVM_SETITEM, 0, (LPARAM)&t);
162+
163+
delete[] w_txt;// fix memory leak
164+
}
165+
166+
delete[] txt; // fix memory leak
167+
}
145168
}
146169
}else
147170
{
@@ -168,21 +191,30 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
168191
{
169192
int len=strlen(json_root->parent->text)+3+strlen("True")+1;
170193
char *txt=new char[len];
171-
memset(txt, 0, len);
172-
char *unesc_text=json_unescape("True");
173-
char *unesc_parent_text=json_unescape(json_root->parent->text);
174-
sprintf(txt,"%s : %s",unesc_parent_text,unesc_text);
175-
free(unesc_text);
176-
free(unesc_parent_text);
177-
178-
len = strlen(txt) + 1;
179-
wchar_t *w_txt = new wchar_t[len];
180-
memset(w_txt, 0, len);
181-
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
182-
183-
t.pszText=w_txt;
184-
t.mask=TVIF_TEXT;
185-
::SendDlgItemMessage(hWndDlg,IDC_TREE1,TVM_SETITEM,0,(LPARAM)&t);
194+
if (txt)
195+
{
196+
memset(txt, 0, len);
197+
char *unesc_text = json_unescape("True");
198+
char *unesc_parent_text = json_unescape(json_root->parent->text);
199+
sprintf(txt, "%s : %s", unesc_parent_text, unesc_text);
200+
free(unesc_text);
201+
free(unesc_parent_text);
202+
203+
len = strlen(txt) + 1;
204+
wchar_t *w_txt = new wchar_t[len];
205+
206+
if (w_txt)
207+
{
208+
memset(w_txt, 0, len);
209+
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
210+
211+
t.pszText = w_txt;
212+
t.mask = TVIF_TEXT;
213+
::SendDlgItemMessage(hWndDlg, IDC_TREE1, TVM_SETITEM, 0, (LPARAM)&t);
214+
delete[] w_txt;//fix memory leak
215+
}
216+
delete[] txt; //fix memory leak
217+
}
186218
}
187219
}else
188220
{
@@ -205,21 +237,29 @@ void JSONDialog::populateTree (HWND hWndDlg, HTREEITEM tree_root, json_t * json_
205237
{
206238
int len=strlen(json_root->parent->text)+3+strlen("False")+1;
207239
char *txt=new char[len];
208-
memset(txt, 0, len);
209-
char *unesc_text=json_unescape("False");
210-
char *unesc_parent_text=json_unescape(json_root->parent->text);
211-
sprintf(txt,"%s : %s",unesc_parent_text,unesc_text);
212-
free(unesc_text);
213-
free(unesc_parent_text);
214-
215-
len = strlen(txt) + 1;
216-
wchar_t *w_txt = new wchar_t[len];
217-
memset(w_txt, 0, len);
218-
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
219-
220-
t.pszText=w_txt;
221-
t.mask=TVIF_TEXT;
222-
::SendDlgItemMessage(hWndDlg,IDC_TREE1,TVM_SETITEM,0,(LPARAM)&t);
240+
if (txt)
241+
{
242+
memset(txt, 0, len);
243+
char *unesc_text = json_unescape("False");
244+
char *unesc_parent_text = json_unescape(json_root->parent->text);
245+
sprintf(txt, "%s : %s", unesc_parent_text, unesc_text);
246+
free(unesc_text);
247+
free(unesc_parent_text);
248+
249+
len = strlen(txt) + 1;
250+
wchar_t *w_txt = new wchar_t[len];
251+
if (w_txt)
252+
{
253+
memset(w_txt, 0, len);
254+
MultiByteToWideChar(CP_UTF8, NULL, txt, -1, w_txt, len);
255+
256+
t.pszText = w_txt;
257+
t.mask = TVIF_TEXT;
258+
::SendDlgItemMessage(hWndDlg, IDC_TREE1, TVM_SETITEM, 0, (LPARAM)&t);
259+
delete[] w_txt; // fix memory leak
260+
}
261+
delete[] txt; // fix memory leak
262+
}
223263
}
224264
}else
225265
{

NppJSONViewer/PluginDefinition.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ INT_PTR CALLBACK abtDlgProc(HWND hwndDlg,UINT uMsg,WPARAM wParam, LPARAM lParam)
8787
case WM_COMMAND:
8888
switch(LOWORD(wParam))
8989
{
90+
case IDCANCEL: // Close this dialog when clicking to close button
9091
case IDOK:
9192
EndDialog(hwndDlg,wParam);
9293
return TRUE;

0 commit comments

Comments
 (0)