1 / 11

C Run-time ( CRT) topics

C Run-time ( CRT) topics. Mahmoud Saleh Windows C++ Team. Help with memory leaks. CRT Debug heap Debug only, default in debug apps Allocates slightly larger blocks of memory Keeps track of memory state (through a linked list) Uses buffers on both sides of user data NoMansLand : 0xfd

tevin
Télécharger la présentation

C Run-time ( CRT) topics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Run-time (CRT) topics Mahmoud SalehWindows C++ Team

  2. Help with memory leaks • CRT Debug heap • Debug only, default in debug apps • Allocates slightly larger blocks of memory • Keeps track of memory state (through a linked list) • Uses buffers on both sides of user data • NoMansLand: 0xfd • New memory: 0xcd • Free’d memory: 0xdd

  3. Help with memory leaks • How to use Debug Heap to find memory leaks • Check state • Dump memory leaks

  4. Help with memory leaks • Debug heap state and functions are debug only! • What about release? • Track allocations • Even new operator goes through malloc(), and delete calls free() • Break at free and malloc • Other options: • _heapwalk • Application Verifier (with Debugging Tools for Windows) • …

  5. Help with unhandled exceptions • terminate() • set_terminate() • Handler must exit; otherwise abort will be called • Don’t throw in the handler!

  6. Assert and error reporting • Asserts (DEBUG only) are very useful in C++ and C • Other error reporting types: _CRT_WARN _CRT_ERROR _CRT_ASSERT • 3 modes for reporting _CRTDBG_MODE_FILE _CRTDBG_MODE_DEBUG _CRTDBG_MODE_WNDW

  7. Assert and error reporting • One step further, use a hook _CrtSetReportHook2 _CrtSetReportHookW2 Note: CrtSetReportHook2 and _CrtSetReportHookW2 let you hook or unhook a function, whereas _CrtSetReportHook only lets you hook a function. • Make sure to properly terminate the app, if you don’t want the CRT default handlers to kick off

  8. CRT entry points • Linker defined, based on /SUBSYSTEM • Windows apps (/SUBSYSTEM:WINDOWS) • (w)WinMain (called by WinMainCRTStartup) • Console apps (/SUBSYSTEM:CONSOLE) • (w)main (called by mainCRTStartup) • DllMain (/DLL) • _DllMainCRTStartup initializes the C run-time library then calls DllMain • If /DLL or /SUBSYSTEM not specified, linker selects subsystem and entry point based on whether main or WinMain is defined. • Or, define your entry point: /ENTRY

  9. CRT support for Unicode, SBCS, MBCS • Recommended: Use Unicode, unless there’s a specific reason not to. • Windows APIs use Unicode • In the CRT, if you want to change the character set: • Project properties: • Or, explicitly define _UNICODE or _MBCS before including <tchar.h> (or using /D compiler switch)

  10. Optimizing File IO in CRT • Buffered vs. low level IO functions • fopen (CRT buffered) • open (low level) • Setting buffer size • Default is is 0x1000 (4K) • setvbuf • Sometimes, buffering may result in perf reduction: use with caution!

  11. Questions

More Related