Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:54:49+00:00 2026-06-15T15:54:49+00:00

I have a multi-threaded Win32 service written in C++ (VS2010) that makes extensive use

  • 0

I have a multi-threaded Win32 service written in C++ (VS2010) that makes extensive use of the standard template library. The business logic of the program operates properly, but when looking at the task manager (or resource manager) the program leaks memory like a sieve.

I have a test set that averages about 16 simultaneous requests/second. When the program is first started up it consumes somewhere in the neighborhood of 1.5Mb of ram. After a full test run (which take 12-15 minutes) the memory consumption ends up somewhere near 12Mb. Normally, this would not be a problem for a program that runs once and then terminates, but this program is intended to run continuously. Very bad, indeed.

To try and narrow down the problem, I created a very small test application that spins off worker threads at a rate of once every 250ms. The worker thread creates a map and populates it with pseudo-random data, empties the map, and then exits. This program, too, leaks memory in like fashion, so I’m thinking that the problem is with the STL not releasing the memory as expected.

I have tried VLD to search for leaks and it has found a couple which I have remedied, but still the problem remains. I have tried integrating Hoard, but that has actually made the problem worse (i’m probably not integrating it properly, but i can’t see how).

So I would like to pose the following question: is it possible to create a program that uses the STL in a multi-threaded environment that will not leak memory? Over the course of the last week I have made no less than 200 changes to this program. I have plotted the results of the changes and they all have the same basic profile. I don’t want to have to remove all of the STL goodness that has made developing this application so much easier. I would earnestly appreciate any suggestions on how I can get this app working without leaking memory like it’s going out of style.

Thanks again for any help!

P.S. I’m posting a copy of the memory test for inspection/personal edification.

#include <string>
#include <iostream>
#include <Windows.h>
#include <map>

using namespace std;

#define MAX_THD_COUNT 1000

DWORD WINAPI ClientThread(LPVOID param)
{
    unsigned int thdCount = (unsigned int)param;

    map<int, string> m;

    for (unsigned int x = 0; x < 1000; ++x)
    {
        string s;

        for (unsigned int y = 0; y < (x % (thdCount + 1)); ++y)
        {
            string z = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            unsigned int zs = z.size();

            s += z[(y % zs)];
        }

        m[x] = s;
    }

    m.erase(m.begin(), m.end());

    ExitThread(0);

    return 0;
}

int main(int argc, char ** argv)
{
    // wait for start
    string inputWait;
    cout << "type g and press enter to go: ";
    cin >> inputWait;

    // spawn many memory-consuming threads
    for (unsigned int thdCount = 0; thdCount < MAX_THD_COUNT; ++thdCount)
    {
        CreateThread(NULL, 0, ClientThread, (LPVOID)thdCount, NULL, NULL);

        cout
            << (int)(MAX_THD_COUNT - thdCount)
            << endl;

        Sleep(250);
    }

    // wait for end
    cout << "type e and press enter to end: ";
    cin >> inputWait;

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:54:51+00:00Added an answer on June 15, 2026 at 3:54 pm

    Use _beginthreadex() when using the std library (includes the C runtime as far as MS is concerned). Also, you’re going to experience a certain amount of fragmentation in the std runtime sub-allocator, especially in code designed to continually favor larger and larger requests like this.

    The MS runtime library has some functions that allow you to debug memory requests and determine if there is a solid leak once you have a sound algorithm and are confident you don’t see anything glaringly obvious. See the debug routines for more information.

    Finally, I made the following modifications to the test jig you wrote:

    1. Setup the proper _Crt report mode for spamming the debug window with any memory leaks after shutdown.
    2. Modified the thread-startup loop to keep the maximum number of threads running constantly at MAXIMUM_WAIT_OBJECTS (WIN32-defined currently as 64 handles)
    3. Threw in a purposeful leaked char array allocation to show the CRT will, in fact, catch it when dumping at program termination.
    4. Eliminated console keyboard interaction. Just run it.

    Hopefully this will make sense when you see the output log. Note: you must compile in Debug mode for this to make any proper dump for you.

    #include <windows.h>
    #include <dbghelp.h>
    #include <process.h>
    #include <string>
    #include <iostream>
    #include <map>
    #include <vector>
    
    using namespace std;
    
    #define MAX_THD_COUNT 250
    #define MAX_THD_LOOPS 250
    
    unsigned int _stdcall ClientThread(void *param)
    {
        unsigned int thdCount = (unsigned int)param;
        map<int, string> m;
    
        for (unsigned int x = 0; x < MAX_THD_LOOPS; ++x)
        {
            string s;
            for (unsigned int y = 0; y < (x % (thdCount + 1)); ++y)
            {
                string z = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
                size_t zs = z.size();
                s += z[(y % zs)];
            }
            m[x].assign(s);
        }
        return 0;
    }
    
    int main(int argc, char ** argv)
    {
        // setup reporting mode for the debug heap. when the program
        //  finishes watch the debug output window for any potential
        //  leaked objects. We're leaking one on purpose to show this
        //  will catch the leaks.
        int flg = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
        flg |= _CRTDBG_LEAK_CHECK_DF;
        _CrtSetDbgFlag(flg);
    
        static char msg[] = "Leaked memory.";
        new std::string(msg);
    
        // will hold our vector of thread handles. we keep this fully populated
        //  with running threads until we finish the startup list, then wait for
        //  the last set of threads to expire.
        std::vector<HANDLE> thrds;
        for (unsigned int thdCount = 0; thdCount < MAX_THD_COUNT; ++thdCount)
        {
            cout << (int)(MAX_THD_COUNT - thdCount) << endl;
            thrds.push_back((HANDLE)_beginthreadex(NULL, 0, ClientThread, (void*)thdCount, 0, NULL));
            if (thrds.size() == MAXIMUM_WAIT_OBJECTS)
            {
                // wait for any single thread to terminate. we'll start another one after,
                //  cleaning up as we detected terminated threads
                DWORD dwRes = WaitForMultipleObjects(thrds.size(), &thrds[0], FALSE, INFINITE);
                if (dwRes >= WAIT_OBJECT_0 && dwRes < (WAIT_OBJECT_0 + thrds.size()))
                {
                    DWORD idx = (dwRes - WAIT_OBJECT_0);
                    CloseHandle(thrds[idx]);
                    thrds.erase(thrds.begin()+idx, thrds.begin()+idx+1);
                }
            }
        }
    
        // there will be threads left over. need to wait on those too.
        if (thrds.size() > 0)
        {
            WaitForMultipleObjects(thrds.size(), &thrds[0], TRUE, INFINITE);
            for (std::vector<HANDLE>::iterator it=thrds.begin(); it != thrds.end(); ++it)
                CloseHandle(*it);
        }
    
        return 0;
    }
    

    Output Debug Window

    Note: there are two leaks reported. One is the std::string allocation, the other is the buffer within the std::string that held our message copy.

    Detected memory leaks!
    Dumping objects ->
    {80} normal block at 0x008B1CE8, 8 bytes long.
     Data: <09      > 30 39 8B 00 00 00 00 00 
    {79} normal block at 0x008B3930, 32 bytes long.
     Data: <    Leaked memor> E8 1C 8B 00 4C 65 61 6B 65 64 20 6D 65 6D 6F 72 
    Object dump complete.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-threaded Python application that makes use of the built in logging
I have a multi-threaded environment in .NET that needs to communicate to a library.
I have a multi-threaded server that handles client requests, and makes new threads for
I have a multi-threaded application that has a centrlaised list that is updated (written
i have a multi-threaded TCP server that is written in C#. Clients are accepted
I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to
I have a multi threaded python application communicating with a separate service trough UDP.
This is the strangest thing! I have a multi-threaded client application written in Python.
Possible Duplicate: Is stl vector concurrent read thread-safe? I have a multi-threaded program that
I have written a multi threaded java code, which when runs creates 8 threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.