Search

Dynamically-initialized static std::string leaking by Rodarin

Closed
as Fixed Help for as Fixed

1
0
Sign in
to vote
Type: Bug
ID: 622001
Opened: 11/15/2010 8:20:15 AM
Access Restriction: Public
0
Workaround(s)
0
User(s) can reproduce this bug
An internally-linked static array of std::string within a DLL shows as leaked memory when the hosting application shuts down.
Details (expand)

Visual Studio/Silverlight/Tooling version

Visual Studio 2010

What category (if any) best represents this feedback?

Reliability

Steps to reproduce

Create a solution containing a DLL and an exe. Within the DLL project source, create a static array of std::string initialized with C-style string constants. Consume the DLL from the exe. Run the exe in the debugger.

Product Language

English

Operating System

Windows 7

Operating System Language

English

Actual results

The std::string elements of the static array are reported as leaking.

Expected results

The CRT init code correctly cleans up the std::string array elements when the DLL is unloaded.
File Attachments
File Name Submitted By Submitted On File Size  
CRTTest2.zip 11/15/2010 136 KB
CRTTest2.zip 11/15/2010 136 KB
Sign in to post a comment.
Posted by Rodarin on 4/6/2011 at 6:35 AM
Thanks for looking into this. I am still concerned by this issue, since it was not present in VC8, but is in VC10. Also, I am assuming you meant that you fixed the const std::strings, not CStrings? If so, is this going to be in the next service pack?

Unfortunately, I cannot modify the actual production code where this manifests due to licensing restrictions, and disabling leak reporting is not a reasonable solution, as there are many other potential leaks that we don't want to miss.
Posted by Microsoft on 4/5/2011 at 2:58 PM
Hello,

Thanks for the report. I have fixed the const CStrings in the MFC DLLs, so they won't show as leaking, but otherwise MFC is doing the best it can in this situation. You have memory that was allocated during your DLL initialization (before the MFC DLL was loaded), so your memory will not have been freed when the MFC DLL is unloaded (this is Windows loader behavior). So the only way to get the leaks reported accurately will be to disable MFC leak reporting (by calling AfxEnableMemoryLeakDump(FALSE) somewhere in your application) and then calling _CrtDumpMemoryLeaks() during the unloading of your DLL. You may have to implement a DLLMain in your DLL and dump the memory leaks during the DLL_DETACH_PROCESS. Or you can add something like the following to one of your CPP modules to dump leaks:

struct _DEBUG_STATE
{
    _DEBUG_STATE() {}
    ~_DEBUG_STATE() { _CrtDumpMemoryLeaks(); }
};

_DEBUG_STATE g_ds;

I added the above code to your names.cpp file, and added a call to AfxEnableMemoryLeakDump(FALSE); in CCRTTestApp::InitInstance and no leaks were reported. I hope this helps.

Pat Brenner
Visual C++ Libraries Development
Posted by Microsoft on 11/15/2010 at 6:15 PM
Thanks for your feedback.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. These specialized experts will follow-up with your issue.
Posted by Microsoft on 11/15/2010 at 9:21 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Sign in to post a workaround.