 <p>It is been a while since I fixed the issue but I thought I should write it here in case someone encounters a similar problem. </p> <p>There was not any bug, nor any memory leak in the code. The program works perfectly. The issue was that the process of creating new vectors was faster than than the one writing to the file. </p> <p>I solved it by having two separate threads to speed up the process. The first thread would create vectors and store them in a buffer. The second thread would take a vector from that buffer and write it.</p>