jeudi 4 août 2016

Visual Studio 2013 doesn't cleanup properly in try/catch block

Consider the following code :

#include <functional>
#include <memory>
#include <iostream>

void FuncA(){}

void FuncB(std::function<void()> callback) {
  try {
    // Do something here...
  }
  catch(...) {
    return FuncA();
  }

  // Do something else...
}

void main() {
  auto foo = std::make_shared<bool>();
  auto callback = [foo]{};

  std::cout << foo.use_count() << std::endl;
  FuncB(callback);
  std::cout << foo.use_count() << std::endl;
}

When compiling this under Visual Studio 2013, the generated assembly code for FuncB() doesn't cleanup the callback properly, which increases the reference count of foo by 1 and causes a memory leak.

One thing I did notice, is if I change FuncB to be...

void FuncB(std::function<void()> callback) {
  try {
    // Do something here...
  }
  catch(...) {
    FuncA();
    return;
  }

  // Do something else...
}

... then everything works as expected and the count before and after are the same.

I'm using VS2013 version 12.0.31101.00 update 4 Compiler version is 18.00.31101 x64.

Does anyone have an idea what is causing this issue ?

Aucun commentaire:

Enregistrer un commentaire