Skip to content

Commit deea1c3

Browse files
committed
Fix memory leak
1 parent 0008b20 commit deea1c3

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -901,20 +901,6 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
901901
if (!lpnmh || lpnmh->idFrom != IDC_TREE)
902902
return; // Not click inside JsonTree
903903

904-
auto GetNodePosition = [this](HTREEITEM hItem)
905-
{
906-
Position* pPosition = nullptr;
907-
if (hItem != nullptr)
908-
{
909-
LPARAM nodePos = m_hTreeView->GetNodePos(hItem);
910-
if (nodePos != -1)
911-
{
912-
pPosition = reinterpret_cast<Position*>(nodePos);
913-
}
914-
}
915-
return pPosition;
916-
};
917-
918904
switch (lpnmh->code)
919905
{
920906
case TVN_SELCHANGED:
@@ -925,7 +911,7 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
925911
{
926912
UpdateNodePath(hItem);
927913

928-
auto pPosition = GetNodePosition(hItem);
914+
auto pPosition = m_hTreeView->GetNodePosition(hItem);
929915
if (pPosition != nullptr)
930916
{
931917
GoToLine(pPosition->nLine);
@@ -938,7 +924,7 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
938924
{
939925
HTREEITEM hItem = m_hTreeView->GetSelection();
940926

941-
auto pPosition = GetNodePosition(hItem);
927+
auto pPosition = m_hTreeView->GetNodePosition(hItem);
942928
if (pPosition != nullptr)
943929
{
944930
GoToPosition(pPosition->nLine, pPosition->nColumn);

src/NppJsonViewer/TreeViewCtrl.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
#include <memory>
2+
13
#include "TreeViewCtrl.h"
24
#include "Define.h"
35
#include "resource.h"
4-
#include <memory>
56

67

78
void TreeViewCtrl::OnInit(HWND hParent)
@@ -280,6 +281,20 @@ auto TreeViewCtrl::GetNodePath(HTREEITEM hti) const -> std::wstring
280281
return wstrJsonPath;
281282
}
282283

284+
auto TreeViewCtrl::GetNodePosition(HTREEITEM hti) const -> Position*
285+
{
286+
Position* pPosition = nullptr;
287+
if (hti != nullptr)
288+
{
289+
LPARAM nodePos = GetNodePos(hti);
290+
if (nodePos != -1)
291+
{
292+
pPosition = reinterpret_cast<Position*>(nodePos);
293+
}
294+
}
295+
return pPosition;
296+
}
297+
283298
HTREEITEM TreeViewCtrl::GetSelection() const
284299
{
285300
return TreeView_GetSelection(m_hTree);
@@ -357,18 +372,11 @@ void TreeViewCtrl::FreeNodeData(HTREEITEM hItem)
357372
if (hItem == nullptr)
358373
return;
359374

360-
TVITEM tvi {};
361-
tvi.hItem = hItem;
362-
tvi.mask = TVIF_PARAM;
363-
364-
if (SendDlgItemMessage(m_hParent, IDC_TREE, TVM_GETITEM, 0, reinterpret_cast<LPARAM>(&tvi)))
375+
Position* pNodeKeyPos = GetNodePosition(hItem);
376+
if (pNodeKeyPos)
365377
{
366-
/*JsonLastKey *pLastKey = reinterpret_cast<JsonLastKey *>(tvi.lParam);
367-
if (pLastKey)
368-
{
369-
delete pLastKey;
370-
pLastKey = nullptr;
371-
}*/
378+
delete pNodeKeyPos;
379+
pNodeKeyPos = nullptr;
372380
}
373381

374382
HTREEITEM hChild = TreeView_GetChild(m_hTree, hItem);

src/NppJsonViewer/TreeViewCtrl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#pragma once
2+
3+
#include <string>
4+
25
#include <Windows.h>
36
#include <CommCtrl.h>
4-
#include <string>
7+
8+
#include "JsonNode.h"
59

610
class TreeViewCtrl
711
{
@@ -56,6 +60,7 @@ class TreeViewCtrl
5660
auto GetNodeKey(HTREEITEM hti) const -> std::wstring;
5761
auto GetNodeValue(HTREEITEM hti) const -> std::wstring;
5862
auto GetNodePath(HTREEITEM hti) const -> std::wstring;
63+
auto GetNodePosition(HTREEITEM hti) const -> Position*;
5964

6065
private:
6166
void ExpandOrCollapse(HTREEITEM node, UINT_PTR code) const;

0 commit comments

Comments
 (0)