ThreadPool icon indicating copy to clipboard operation
ThreadPool copied to clipboard

A problem about memory leak

Open Emen-c opened this issue 2 years ago • 3 comments

I write code like this:

struct thv
{
    long l[1024];
};

void thcall(vector<thv> l)
{
    usleep(10 * 1000);
}

int main(int, char **)
{
    ThreadPool thpool(12);
    vector<thv> v;
    vector<future<void>> res;
    for (int i = 0; i < 1000; i++)
    {
        v.push_back(thv());
    }
    for (int i = 0; i < 1000; i++)
    {
        res.push_back(thpool.enqueue(thcall, v));
        usleep(10 * 1000);
    }
    for (auto &it : res)
    {
        it.get();
    }
    vector<thv>().swap(v);
    cout << v.capacity() << endl;
    cout << "----------end----------" << endl;
    std::getchar();
}

when terminal printf

0 ----------end----------

I notice this demo used about 8G memory.

I run this code in ubuntu using ARM cpu and compiled with gcc 9.4.0. what coused this problem? It had serious impacts on my program.

Thanks very much

Emen-c avatar Feb 13 '23 08:02 Emen-c

std::future<return_type> res = task->get_future(); // will malloc memory to store function return val;

y11en avatar Feb 28 '23 02:02 y11en

Can I work on this if no one is working? Thanks!

bharsaklemukesh975 avatar Apr 05 '23 22:04 bharsaklemukesh975

void thcall(vector<thv> l)

Every time when you call thecall will copy a v.

void thcall(vector<thv>& l)

Use reference instead could help.

Abo-go avatar Mar 01 '24 09:03 Abo-go