Skip to content

Instantly share code, notes, and snippets.

@randvoorhies
Created October 10, 2013 18:42
Show Gist options
  • Save randvoorhies/6923348 to your computer and use it in GitHub Desktop.
Save randvoorhies/6923348 to your computer and use it in GitHub Desktop.
Deadman gist
//! A deadman switch that will execute it's constructor parameter on destruction unless disarm()'d
struct deadman
{
deadman(std::function<void()> boom) : boom_(boom) {}
~deadman() { boom_(); }
void disarm() { boom_ = [](){}; }
std::function<void()> boom_;
};
@randvoorhies
Copy link
Author

uint8_t * myfunction()
{
  uint8_t * data;
  deadman onfailure([data]() { delete[] data; };

  data = new uint8_t[1024];

  if( try_to_fill_data(data) == false)
    throw std::runtime_error("Oh crap!");

  onfailure.disarm();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment