-3

I wrote an simple code that should make 1000 of threads, do some job, join them, and replay everything 1000 times.

I have a memory leak with this piece of code and I don't understand why. I've been looking for solution pretty much everywhere and can't find one.

#include <iostream>
#include <thread>
#include <string>
#include <windows.h>


#define NUM_THREADS 1000

std::thread t[NUM_THREADS];

using namespace std;


//This function will be called from a threads
void checkString(string str)
{
    //some stuff to do
}

void START_THREADS(string text)
 {

    //Launch a group of threads
    for (int i = 0; i < NUM_THREADS; i++)
    {
        t[i] = std::thread(checkString, text);
    }


    //Join the threads with the main thread
    for (int i = 0; i < NUM_THREADS; i++) {

        if (t[i].joinable())
        {
            t[i].join();
        }

    }

    system("cls");
 }


int main() 
{

    for(int i = 0; i < 1000; i++)
    {
        system("cls");
        cout << i << "/1000" << endl;
        START_THREADS("anything");
    }


    cout << "Launched from the main\n";

    return 0;
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • You don't show any code with memory allocations, so how could there be any memory leaks? – crashmstr Jun 01 '15 at 13:44
  • 2
    *How* are you measuring the leak ? – Quentin Jun 01 '15 at 13:45
  • While I'm starting this application, the memory in task manager (private working set) filling very fast. If that's not memory leak, please tell me why it is happening, and how can I avoid that? – Frophemida Jun 01 '15 at 13:48
  • 1
    @Frophemida If the memory consumption stabilises, then that's not a memory leak. It's common that the runtime library holds on to freed memory in case you reallocate some later. There's no manual allocation in this code snippet, so it's most probably what's happening. – Quentin Jun 01 '15 at 13:51
  • The problem is that, the memory consumption does not stabilise, but keep rising till the program termination.What should I do? – Frophemida Jun 01 '15 at 13:57
  • 1
    @Frophemida I'd recommend running your program through a runtime analyzer, that will report any un-freed memory. Linux has Valgrind, [here](http://stackoverflow.com/questions/413477/is-there-a-good-valgrind-substitute-for-windows) is a list of alternatives for Windows. – Quentin Jun 01 '15 at 14:00
  • Does `checkString` do anything in your real code? – molbdnilo Jun 01 '15 at 14:03
  • Yes, it is, but even if I add an code to that function, the memory consumption of process still running out of control and reaches about 200k in the last seconds before termination. I would like to use this program about 3-5 times, so the consumption would be about 800-1kk, that's too much. – Frophemida Jun 01 '15 at 14:06
  • 2
    Well, you shouldn't spawn 1 million threads. – juanchopanza Jun 01 '15 at 14:12
  • I know that I can use thread pool, but it will take me too much time to learn how it's done, especially if people like @juanchopanza would "help" me. – Frophemida Jun 01 '15 at 14:18
  • Apart from the overhead of spawning all those threads, you are copying the `std::string` around a lot. Try replacing that with (const) `std::string&` in the `checkString` function. You can be sure about the lifetime, since you `.join()` your threads before the `START_THREADS` function exits – Alejandro Jun 01 '15 at 14:20
  • @Alejandro thanks for suggestion, changed it! :) – Frophemida Jun 01 '15 at 14:26
  • @Frophemida I almost forgot, you should also use `std::ref` or `std::cref` so that you can actually get reference semantics within your `checkString` function – Alejandro Jun 01 '15 at 14:36
  • Why did you remove the code? – Alejandro Jun 01 '15 at 14:59

1 Answers1

3

I'm not sure about memory leaks, but you certainly have a memory error. You shouldn't be doing this:

delete &t[i];

t[i] was not allocated with new and it can't be deleted. You can safely remove that line.

As for memory consumption, you need to ask yourself whether you really need to spawn 1 million threads. Spawning threads isn't cheap, and it is unlikely that your platform will be able to run more than a handful of them concurrently.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thank you, but your answer does not changes nothing. I wrote that line in hope of stop memory leak, and forgot to remove it. :) – Frophemida Jun 01 '15 at 13:44
  • 5
    @Frophemida It does. It removed the *undefined behaviour* from your code. Without that change, your code can be considered to be totally broken. You have not provided any evidence that you have a memory leak. – juanchopanza Jun 01 '15 at 13:50