Skip to content

Commit 5cccf98

Browse files
Fix memory leak
1 parent 3b69fd4 commit 5cccf98

5 files changed

Lines changed: 30 additions & 17 deletions

File tree

ConsoleScreenCapture/ConsoleScreenCapture/MessageLoop.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ bool bPressTermTime = false;
2424
bool bMecroPressed1 = false;
2525
bool bMecroPressed2 = false;
2626
HHOOK hHook;
27-
27+
// 키 처리도 클래스로 전체적으로 관리해야하는데 귀찮으니~pass
2828
DWORD pressMacroKey[2] = { MACRO_MAGIC_NUM1, MACRO_MAGIC_NUM2 }; //magic num
2929
void resetPressMacroKey() {
3030
pressMacroKey[0] = MACRO_MAGIC_NUM1;
@@ -154,7 +154,21 @@ void UnHook() {
154154
UnhookWindowsHookEx( hHook );
155155
}
156156

157+
BOOL ConsoleCtrlHandler( DWORD fdwCtrlType )
158+
{
159+
switch ( fdwCtrlType )
160+
{
161+
case CTRL_CLOSE_EVENT:
162+
case CTRL_SHUTDOWN_EVENT:
163+
default:
164+
CMonitor::GetInst()->DeleteMonitorData();
165+
UnHook();
166+
return FALSE;
167+
}
168+
}
169+
157170
int mainMessageLoop() {
171+
SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleCtrlHandler, TRUE );
158172
SetHook();
159173
MSG msg;
160174
CDeltaTime::GetInst()->Init();
@@ -184,8 +198,7 @@ int mainMessageLoop() {
184198
fPressTermAccTime = 0.f;
185199
}
186200
}
187-
188-
Sleep( 10 );
201+
Sleep( 20 );
189202
}
190203
}
191204
UnHook();

ConsoleScreenCapture/ConsoleScreenCapture/Monitor.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ int CMonitor::GetMonitorCount()
1313

1414
void CMonitor::DeleteMonitorData()
1515
{
16-
if ( m_MonitorData.pInfo != nullptr )
17-
{
18-
free( m_MonitorData.pInfo );
19-
m_MonitorData.pInfo = nullptr;
20-
}
2116
if ( m_MonitorData.pDpiAndScale != nullptr )
2217
{
23-
free( m_MonitorData.pDpiAndScale );
18+
delete m_MonitorData.pDpiAndScale;
2419
m_MonitorData.pDpiAndScale = nullptr;
2520
}
21+
if ( m_MonitorData.pInfo != nullptr )
22+
{
23+
delete m_MonitorData.pInfo;
24+
m_MonitorData.pInfo = nullptr;
25+
}
2626
if ( m_MonitorData.pDSF != nullptr )
2727
{
28-
free( m_MonitorData.pDSF );
28+
delete m_MonitorData.pDSF;
2929
m_MonitorData.pDSF = nullptr;
3030
}
3131
if ( m_MonitorData.pHMonitor != nullptr )
3232
{
33-
free( m_MonitorData.pHMonitor );
33+
delete m_MonitorData.pHMonitor;
3434
m_MonitorData.pHMonitor = nullptr;
3535
}
3636
}
@@ -95,11 +95,11 @@ bool CMonitor::Setting()
9595
{
9696
m_iMonitors = GetSystemMetrics( SM_CMONITORS );
9797
m_MonitorData.iCurrent = 0;
98-
m_MonitorData.pInfo = (MONITORINFOEXW*)calloc( m_iMonitors, sizeof( MONITORINFOEXW ) );
99-
m_MonitorData.pDpiAndScale = (DpiAndScale*)calloc( m_iMonitors, sizeof( DpiAndScale ) );
100-
m_MonitorData.pDSF = (DEVICE_SCALE_FACTOR*)calloc( m_iMonitors, sizeof( DEVICE_SCALE_FACTOR ) );
101-
m_MonitorData.pHMonitor = (HMONITOR*)calloc( m_iMonitors, sizeof( HMONITOR ) );
102-
98+
m_MonitorData.pInfo = new MONITORINFOEXW[m_iMonitors];
99+
m_MonitorData.pDpiAndScale = new DpiAndScale[m_iMonitors];
100+
m_MonitorData.pDSF = new DEVICE_SCALE_FACTOR[m_iMonitors];
101+
m_MonitorData.pHMonitor = new HMONITOR[m_iMonitors];
102+
103103
if ( !GetAllMonitorInfo( &m_MonitorData ) ) return false;
104104

105105
#ifdef TMP_SCALE_FACTOR

ConsoleScreenCapture/ConsoleScreenCapture/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using namespace std;
88

99
int main() {
1010
std::wcout.imbue( std::locale( "korean" ) );
11-
wcout << L"작성자: 김동일" << endl;
11+
wcout << L"작성자: ExtremeCode" << endl;
1212
wcout << L"이메일: extremecode716@gmail.com" << endl;
1313
wcout << L"촬영 단축키(기본): LCTRL + 1 / LCTRL + 2" << endl;
1414
wcout << L"환경 설정 파일: ExtremeScreenCapture_env.txt " << endl;
512 Bytes
Binary file not shown.
-512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)