From a7927d1dba01769af1869e7e4a704f38e9600759 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 21 Feb 2015 18:28:58 +0100 Subject: fixed memory leak in classimpl --- zend/classimpl.cpp | 13 +++++++++---- zend/classimpl.h | 9 +++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/zend/classimpl.cpp b/zend/classimpl.cpp index ab95455..82cbc66 100644 --- a/zend/classimpl.cpp +++ b/zend/classimpl.cpp @@ -20,6 +20,11 @@ ClassImpl::~ClassImpl() { // destruct the entries if (_entries) delete[] _entries; + +#if PHP_VERSION_ID >= 50400 + // on newer php versions, we have allocated the command to hide a pointer in it + if (_self) free(_self); +#endif } /** @@ -1419,16 +1424,16 @@ zend_class_entry *ClassImpl::initialize(ClassBase *base, const std::string &pref #if PHP_VERSION_ID >= 50400 // allocate doc comment to contain an empty string + a hidden pointer - char *_comment = (char *)malloc(1 + sizeof(ClassImpl *)); + _self = (char *)malloc(1 + sizeof(ClassImpl *)); // empty string on first position - _comment[0] = '\0'; + _self[0] = '\0'; // copy the 'this' pointer to the doc-comment - memcpy(_comment+1, &impl, sizeof(ClassImpl *)); + memcpy(_self+1, &impl, sizeof(ClassImpl *)); // set our comment in the actual class entry - _entry->info.user.doc_comment = _comment; + _entry->info.user.doc_comment = _self; #else diff --git a/zend/classimpl.h b/zend/classimpl.h index 32f0e1f..5d0ab60 100644 --- a/zend/classimpl.h +++ b/zend/classimpl.h @@ -42,13 +42,13 @@ private: * @var zend_class_entry */ zend_class_entry *_entry = nullptr; - + /** * Pointer to the entries * @var zend_function_entry[] */ zend_function_entry *_entries = nullptr; - + /** * All class methods * @var std::list @@ -91,6 +91,11 @@ private: */ bool _initialized = false; + /** + * Memory allocated by this object to hide a pointer + * @var char* + */ + char *_self = nullptr; /** * Retrieve an array of zend_function_entry objects that hold the -- cgit v1.2.3