summaryrefslogtreecommitdiff
path: root/zend/delayedfree.h
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-02-05 21:00:17 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-02-05 21:00:17 +0100
commit45d59165cb6b4c80fd26d555eae8ca8f2a7d0d6f (patch)
treeb320b64f58758e12055689509a73407d980dc191 /zend/delayedfree.h
parentc169085cc9a9eb9c7c911d1b68db1f08a95c0d27 (diff)
when an exception was thrown from out of an __invoke() or __call() method, a segmentation fault occured because memory was too early freed (the exception constructor still relied on data that was already freed)
Diffstat (limited to 'zend/delayedfree.h')
-rw-r--r--zend/delayedfree.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/zend/delayedfree.h b/zend/delayedfree.h
new file mode 100644
index 0000000..598d4f5
--- /dev/null
+++ b/zend/delayedfree.h
@@ -0,0 +1,50 @@
+/**
+ * DelayedFree.h
+ *
+ * Sometimes a piece of data must be freed when a function gets out of
+ * scope. In stead of putting the efree() call right before every possible
+ * function end point (exceptions, returns, zend_errors()), we can use
+ * this simple class instead
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2015 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class DelayedFree
+{
+private:
+ /**
+ * The data that has to be free'd when the object falls out of scope
+ * @var void*
+ */
+ void *_data;
+
+public:
+ /**
+ * Constructor
+ * @param data Data that will be freed on destruction
+ */
+ DelayedFree(void *data) : _data(data) {}
+
+ /**
+ * Destructor
+ */
+ virtual ~DelayedFree()
+ {
+ // free the data
+ efree(_data);
+ }
+};
+
+/**
+ * End of namespace
+ */
+}