Z
Z
Zakhar Alekseenko2015-02-11 14:28:53
C++ / C#
Zakhar Alekseenko, 2015-02-11 14:28:53

Memory leak on std::mutex lock?

Hello.
Faced a memory leak while working with threads.
I needed to make a class that counts data in a separate thread. To do this, I created an object of the std::thread type in the class and passed a class member function to it, in which continuous processing is carried out. Other function periodically I take away the received data.
Below is a code example that is more obvious than any explanation. I set Intel Inspector XE 2015 on it.
At the output I got
ID Type Sources Modules Object Size State Missing allocation xthread
:68 threadtest.exe Not fixed

void mycl::start(){
    <b>m.lock();</b>
....

class mycl{
public:
  mycl():m_thread()
  {
    for (int i = 0; i < 5; i++)
      data.push_back(i);
  }
  ~mycl(){
    if (m_thread.joinable())
      m_thread.join();
  }
  std::vector<float> mycl::getData()
  {
std::vector<float> d;
    m.lock();
    printf("get data\n");
    for (unsigned int i = 0; i < data.size(); i++){
      printf("%f ", data.at(i));
    }
    printf("\n");
    d = data;
    m.unlock();
    return d;
  }

  void mycl::process(bool& stat)
  {
    while (true){
      std::vector<float> d;
      m.lock();
      if (!stat)
      {
        m.unlock();
        return;
      }
      for (int i = 0; i < data.size(); i++){
        d.push_back(50 * 20 * rand());
      }
      data = d;
      m.unlock();
    }
  }

  void mycl::start(){
    m.lock();
    started = true;
    m.unlock();
    m_thread = std::thread(&mycl::process, this, std::ref(started));

  }
  void mycl::stop(){
    m.lock();
    started = false;
    m.unlock();
    if (m_thread.joinable())
      m_thread.join();
  }
private:
  std::vector<float> data;
  bool started = false;
  std::thread m_thread;
  std::mutex m;
};

void main(){
  mycl cl;
  srand(time(NULL));
  cl.start();
  std::vector<float> vc;
  for (int i = 0; i < 1000; i++){
    vc = cl.getData();
  }
  cl.stop();
}

I would like to see an example of a truly thread-safe class.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2015-02-11
@gbg Curated C++ Tag

Frankly, the code is crappy, from the int/uint confusion and ignoring iterators to the boolean variable wrapped in a mutex.
The meaning of this logical variable is currently not visible.
Here is that piece:

printf("get data\n");
    for (unsigned int i = 0; i < data.size(); i++){
      printf("%f ", data.at(i));
    }
    printf("\n");
    std::vector<float> d;
    m.lock();
    d = data;
    m.unlock();
    return d;

asks for a well-known site with the letter G, because here at first they are impudently poking around in the data array, not deigning to lock it on a mutex. Perhaps this degeneration happened during debugging.
Question to the author - why do we need the flag started?
This code cannot generate a "leakage" of any resource, because it does not create dynamic objects.

T
tsarevfs, 2015-02-11
@tsarevfs

While you are printing data in getData(), the thread can change it. Use a local copy of d for this. It is not entirely clear why to pass started as a parameter.

A
AxisPod, 2015-02-11
@AxisPod

As an option, instead of a mutex and setting a value, it would be better to use std::atomic_flag or std::atomic, the first is simpler in features, but 100% atomic on any systems, the second has more features. Well, in some cases, a variable marked as volatile may suffice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question