Detected memory leaks!

Jun 26, 2015 at 12:17 PM
Edited Jun 26, 2015 at 4:50 PM
The following simple windows console program produces memory leaks:
#include <cpprest/json.h>
#include <crtdbg.h>
int _tmain(int argc, _TCHAR* argv[]) {
   _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);  //enable leak detection
   web::json::value js; 
   return 0;
}
Detected memory leaks!
Dumping objects ->
{304} normal block at 0x007FC810, 12 bytes long.
Data: < N $Z > 01 00 00 00 88 DC 4E 10 C4 24 5A 10
{303} normal block at 0x007FC7C0, 16 bytes long.
Data: < > 10 C8 7F 00 00 00 00 00 00 00 00 00 00 00 00 00
{302} normal block at 0x007FDA50, 20 bytes long.
Data: < > 01 00 00 00 C0 C7 7F 00 01 00 00 00 04 00 00 00
Object dump complete.


For mfc programs the leak detection is on per default.
Adding "web::json::value js; " somewhere in a mfc program results in the same dump as above.


Are there memory leaks or is it a false alarm?
Coordinator
Jun 26, 2015 at 5:27 PM
Hi Andy_zhang,

Yes it's true there are some consistent memory leaks here. They aren't coming from the C++ REST SDK library itself but rather OpenSSL. OpenSSL allocates a non growing consistent amount of memory that is never freed. The stance from OpenSSL appears to be to have no intentions of fixing or changing this. Here is a discussion thread talking about the leaks. Here is an actual bug with OpenSSL on the memory leaks.

I see how this is an annoyance, we have to deal with these on our leak runs for the library. But I don't know of any way to work around it. The leaks are small and always consistent so in terms of practical application impact there isn't a worry.

Steve
Jun 26, 2015 at 7:24 PM
Edited Jun 26, 2015 at 7:25 PM
Hi Steve,

thanks for your fast reply!
C++ rest sdk Casablanca is really a great framework!

When I use declare "web::json::value js" some where in the most simle mfc program I get around 3641 connected blocks leaking.
When I add some Rest calls and json parsing, I get 12 additional blocks, which are not connected to the first 3641 blocks.

The OpenSSL thread is 10 years old - long time for fixing...

The problem with this is, that it is pretty difficult to find potential memory leaks in the own code.
Coordinator
Jun 26, 2015 at 9:56 PM
Other than those 3 leaks in your original post, the rest are not real leaks. They are an issue with how MFC is detecting leaks, this comes up a lot but there is nothing the library can do about it. Take a look at prior discussions about this, for example one here.

Basically when 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.

Steve
Jun 27, 2015 at 11:05 AM
Thanks for the clarification!

Andy