Tag Archives: learning

Self-destructing objects

One day, recalling the programmed cell death I decided to create class that would self-destruct on a certain amount of copying.

Today’s little research resulted in the following project: https://github.com/d-led/selfdestructing

The question arises, what exactly copying stands for. I could think of four policies. The implementation of those policies is configurable and extensible. Right now there is no explicitly thread-safe policy implemented, but those would not be very complicated, if the purpose is debugging solely.

Usage

#include "crash_on_copy.hpp"
struct TestNumberCrash : public crashes::on<3>::copies {};

on_copiesCrashes on 3 total copies of the originally created object, but doesn’t crash on 3 total instances of the class

struct TestCopyNrCrash : public crashes::after<3>::copies {};

after_copiesCrashes on any third copy of the original object, but doesn’t crash on 3 total instances of the class

struct TestTotalNrCrash : public crashes::on_total<3,TestTotalNrCrash>::instances {};

on_total_instancesCrashes on instantiation of an object if 2 objects are alive, but doesn’t crash on any creation if the total amount of instances is below 2

struct TestAfterTotalNrCrash : public crashes::after_total<3,TestAfterTotalNrCrash>::instances {};

after_total_instancesCrashes after a third object instantiation of the class

Singular form aliases are also available, i.e. crashes::on<1>::copy.

The project at github contains the test showing the crashing pattern.