Many memory leaks with MFC based application

Jun 11, 2015 at 2:16 AM
Edited Jun 11, 2015 at 3:34 AM

Problem

Please give me hints to fix memory leaks.
Here's step by step procedure to reproduce problem.

Environment

Visual Studio 2013 Update 4
Window 7 Professional SP1 x64

Create new application

  • MFC based application
  • Dialog base
  • Use MFC Shared library
  • Unicode charset

Add latest "C++ REST SDK" from NuGet console.

Install-Package cpprestsdk.v120.windesktop.msvcstl.dyn.rt-dyn
Added package is "cpprestsdk.v120.windesktop.msvcstl.dyn.rt-dyn.2.6.0"

Add 3 files to project.

  • ClientInfo.h
  • ServerClientPackets.h
  • ServerClientPackets.cpp
These files are in ChatClientServer.zip attached at below page.
http://blogs.msdn.com/b/vcblog/archive/2014/06/25/c-rest-sdk-websocket-client.aspx

Then build and run with DEBUG configration,

Dialog will appear, so close it.

We found so many memory leaks on output pane.
Jun 11, 2015 at 2:31 AM
Detected memory leaks! Dumping objects -> {3808} normal block at 0x005CE710, 48 bytes long. Data: <S e c - W e b S > 53 00 65 00 63 00 2D 00 57 00 65 00 62 00 53 00 {3807} normal block at 0x005CE6C8, 8 bytes long. Data: < e > E0 65 05 10 00 00 00 00 {3806} normal block at 0x005CE678, 16 bytes long. Data: < > 00 00 00 00 07 00 00 00 08 00 00 00 0D 00 00 00 {3805} normal block at 0x005CE630, 8 bytes long. Data: < e > D0 65 05 10 00 00 00 00 {3804} normal block at 0x005CE5A0, 80 bytes long. Data: <ABCDEFGHIJKLMNOP> 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 {3803} normal block at 0x005CE558, 8 bytes long. Data: < e > 98 65 05 10 00 00 00 00 {3802} normal block at 0x005CE510, 8 bytes long. Data: <|e > 7C 65 05 10 00 00 00 00 {3801} normal block at 0x005CE4A0, 48 bytes long. Data: <R e s p o n s e > 52 00 65 00 73 00 70 00 6F 00 6E 00 73 00 65 00 {3800} normal block at 0x005CE458, 8 bytes long. Data: < d > 10 64 05 10 00 00 00 00 {3799} normal block at 0x005CE3E8, 48 bytes long. Data: <W w w - A u t h > 57 00 77 00 77 00 2D 00 41 00 75 00 74 00 68 00
Jun 11, 2015 at 5:37 PM
Hi qt6hy,

There are no actual memory leaks going on here, this has to do with how MFC decides to measure and report leaks. When the MFC dll unloads it reports on all the remaining objects that haven't been deallocated yet. The C++ REST SDK dll contains some global objects that get destructed when the dll unloads. Unfortunately if the MFC dll unloads before the C++ REST SDK, or any other dll containing global objects, these will be reported as memory leaks. When in fact they are destructed and freed right after when the C++ REST SDK dll is unloaded.

Long story short the dll unload ordering has an impact on whether or not MFC reports leaks on global objects that are about the be freed. Here is a prior issue/discussion about this problem that you can take a look at for more information.

Steve
Marked as answer by qt6hy on 6/11/2015 at 5:00 PM
Jun 12, 2015 at 1:00 AM
Thank you Steve.
I know the background of the problem.