Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A problem about memory leak #103

Open
Emen-c opened this issue Feb 13, 2023 · 3 comments
Open

A problem about memory leak #103

Emen-c opened this issue Feb 13, 2023 · 3 comments

Comments

@Emen-c
Copy link

Emen-c commented Feb 13, 2023

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

@y11en
Copy link

y11en commented Feb 28, 2023

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

@bharsaklemukesh975
Copy link

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

@Abo-go
Copy link

Abo-go commented Mar 1, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants