Skip to content

Instantly share code, notes, and snippets.

@vittorioromeo
Last active December 18, 2015 13:48
Show Gist options
  • Save vittorioromeo/5792381 to your computer and use it in GitHub Desktop.
Save vittorioromeo/5792381 to your computer and use it in GitHub Desktop.
Strange lambda bug (machine-dependent compiler bug?)
// Test 1 works
// Test 2 segfaults... on some machines.
// Compiled with -std=c++11, GCC 4.8.1, tested both on native Linux, Windows and Wine
#include <iostream>
#include <functional>
#include <vector>
using namespace std;
struct Command
{
function<void()> func;
void reset() { }
};
struct Timeline
{
vector<Command*> commands;
void clear()
{
for(auto& c : commands) delete c;
commands.clear();
}
void reset() { for(auto& c : commands) c->reset(); }
};
struct Game
{
Timeline timeline;
void test1() { timeline.clear(); timeline.reset(); }
void run()
{
{
cout << "Starting test 1..." << endl;
Command* cmd{new Command};
cmd->func = [&]{ test1(); };
timeline.commands.push_back(cmd); cmd->func();
cout << "Successfully ending test 1..." << endl;
}
{
cout << "Starting test 2..." << endl;
Command* cmd{new Command};
cmd->func = [&]{ timeline.clear(); timeline.reset(); };
timeline.commands.push_back(cmd); cmd->func();
cout << "Successfully ending test 2..." << endl;
}
}
};
int main() { Game{}.run(); return 0; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment