728x90
MFC 프로그램 종료
윈도우 CE를 사용하는 PDA 프로그램을 개발할 때 있었던 일이다.
프로그램이 실행되면 메인 함수가 쓰레드로 실행되고 있고, 메인 함수가 종료되면
SendMessage 함수로 WM_CLOSE 메시지르 보내 종료를 시켰었다.
따로 화면에 에러 메시지도 안나오고, 다시 프로그램을 실행해도 문제가 없어
그냥 넘어갔는데, 간헐적으로 제어판이나 PDA가 멈추는 증상이 나왔다.
이상해서 커널 메시지를 확인하니...
커널 메시지
| Exception 'User Stack Overflow' (0xfffffffd): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=400222e0(coredll.dll+0x000122e0) RA=400a4760(coredll.dll+0x00094760) SP=00431ff8, BVA=00431ffc Exception 'Raised Exception' (0x116): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=00400002(pprc=8526d5e0) 'NK.EXE', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=eff5ed8c(k.coredll.dll+0x0001ed8c) RA=81027648(kernel.dll+0x0000e648) SP=ae7cedfc, BVA=ffffffff Exception 'Data Abort' (0x4): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=400372b0(coredll.dll+0x000272b0) RA=00530000(???+0x00530000) SP=0052f554, BVA=38000018 Exception 'Raised Exception' (0xc0008002): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=4002b544(coredll.dll+0x0001b544) RA=81027648(kernel.dll+0x0000e648) SP=0052f564, BVA=38000279 Exception 'Data Abort' (0x4): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=400372b0(coredll.dll+0x000272b0) RA=00530000(???+0x00530000) SP=0052f554, BVA=38000018 Exception 'Raised Exception' (0xc0008002): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=4002b544(coredll.dll+0x0001b544) RA=81027648(kernel.dll+0x0000e648) SP=0052f564, BVA=3800027a Exception 'Data Abort' (0x4): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=400372b0(coredll.dll+0x000272b0) RA=00530000(???+0x00530000) SP=0052f554, BVA=38000018 Exception 'Raised Exception' (0xc0008002): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=4002b544(coredll.dll+0x0001b544) RA=81027648(kernel.dll+0x0000e648) SP=0052f564, BVA=3800027b Exception 'Data Abort' (0x4): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=400372b0(coredll.dll+0x000272b0) RA=00530000(???+0x00530000) SP=0052f554, BVA=38000018 Exception 'Raised Exception' (0xc0008002): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=4002b544(coredll.dll+0x0001b544) RA=81027648(kernel.dll+0x0000e648) SP=0052f564, BVA=38000274 Exception 'Data Abort' (0x4): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=400372b0(coredll.dll+0x000272b0) RA=00530000(???+0x00530000) SP=0052f554, BVA=38000018 Exception 'Raised Exception' (0xc0008002): Thread-Id=0635001e(pth=9b8bb2d0), Proc-Id=0630001e(pprc=9b88e31c) '내프로그램.exe', VM-active=0630001e(pprc=9b88e31c) '내프로그램.exe' PC=4002b544(coredll.dll+0x0001b544) RA=81027648(kernel.dll+0x0000e648) SP=0052f564, BVA=38000275 |
내 프로그램이 종료될때, 유저 스택 오버플로우와 Exception 에러들이 나오고 있었다.
메모리 free 이슈나 메모리 부족인 줄 알고 유저 스택을 크게 키워 봤지만 해결은 되지 않았다.
WM_CLOSE와 WM_DESTROY 차이
처음에는 send_message로 WM_CLOSE를 써서 종료를 했는데,
이는 윈도우의 닫기 버튼을 눌렀을 때와 같은 이벤트를 발생한다고 한다. 즉, 프로그램이 종료된다라는 메시지만 날리지
실제 메모리 초기화 등 종료에 대한 준비는 안 하며
WM_DESTROY 는 실제 프로그램이 종료될 때의 이벤트와 같으며, DestroyWindow함수를 호출해서 사용된 메모리를
반환한다.
그래서 WM_DESTROY 이벤트로 종료하게 하니, 유저 스택 오버플로우와 Exception 에러가 사라졌다.
항상 함수든 프로그램을 종료할 때는 메모리 반환에 주의하자.
728x90
'개발이야기' 카테고리의 다른 글
| [C언어/API] 변수의 비트 수 체크하기 (0) | 2021.08.01 |
|---|---|
| [C언어/API] 버튼 입력 채터링 관련한 팁 (0) | 2021.08.01 |
| 1분코딩강좌 #09 enum과 switch문 (0) | 2021.05.23 |
| 1분코딩강좌 #08 최적화와 volatile에 대한 강좌 | 어셈블리어 코드분석 (0) | 2021.05.09 |
| 펌웨어를 위한 C언어 1분코딩강좌 #06 포인터 기초 (0) | 2021.05.06 |