fixed the code to call the destructor when dispoing an object with mmgr

This commit is contained in:
2018-07-01 07:33:56 +00:00
parent 87e0cf1b40
commit 9cbd6cd7fe
8 changed files with 43 additions and 48 deletions

View File

@ -43,7 +43,7 @@ class QSE_EXPORT Exception
public:
Exception (
const qse_char_t* name, const qse_char_t* msg,
const qse_char_t* file, qse_size_t line):
const qse_char_t* file, qse_size_t line) QSE_CPP_NOEXCEPT:
name(name), msg(msg)
#if !defined(QSE_NO_LOCATION_IN_EXCEPTION)
, file(file), line(line)
@ -69,7 +69,7 @@ public:
{ \
public: \
ex_name (const qse_char_t* name, const qse_char_t* msg, \
const qse_char_t* file, qse_size_t line): \
const qse_char_t* file, qse_size_t line) QSE_CPP_NOEXCEPT: \
QSE::Exception (name, msg, file, line) {} \
}

View File

@ -362,7 +362,7 @@ public:
}
else
{
while (n)
while (n)
{
this->prepend (n->value);
n = (Node*)n->prev;

View File

@ -54,9 +54,6 @@ public:
QSE_EXCEPTION (MemoryError);
protected:
bool raise_exception;
public:
///
/// The Mmgr() function builds a memory manager composed of bridge
@ -84,7 +81,7 @@ public:
/// allocation. if it fails, it raise an exception if it's
/// configured to do so.
///
void* allocate (qse_size_t n, bool raise_exception = true)
void* allocate (qse_size_t n, bool raise_exception = true) throw(MemoryError)
{
void* xptr = this->allocMem (n);
if (!xptr && raise_exception) QSE_THROW (MemoryError);
@ -95,14 +92,14 @@ public:
/// The callocate() function allocates memory like allocate() and
/// clears the memory before returning.
///
void* callocate (qse_size_t n, bool raise_exception = true);
void* callocate (qse_size_t n, bool raise_exception = true) throw(MemoryError);
///
/// The reallocate() function calls reallocMem() for memory
/// reallocation. if it fails, it raise an exception if it's
/// configured to do so.
///
void* reallocate (void* ptr, qse_size_t n, bool raise_exception = true)
void* reallocate (void* ptr, qse_size_t n, bool raise_exception = true) throw(MemoryError)
{
void* xptr = this->reallocMem (ptr, n);
if (!xptr && raise_exception) QSE_THROW (MemoryError);
@ -174,7 +171,7 @@ protected:
QSE_END_NAMESPACE(QSE)
/////////////////////////////////
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr);
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr) throw(QSE::Mmgr::MemoryError);
#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING)
QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr);
@ -182,7 +179,7 @@ QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr);
QSE_EXPORT void operator delete (void* ptr, QSE::Mmgr* mmgr);
#endif
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr);
QSE_EXPORT void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr) throw(QSE::Mmgr::MemoryError);
#if 0
// i found no way to delete an array allocated with
@ -196,4 +193,10 @@ void* operator new[] (qse_size_t size, QSE::Mmgr* mmgr);
void operator delete[] (void* ptr, QSE::Mmgr* mmgr);
#endif
#define QSE_CPP_DELETE_WITH_MMGR(ptr, class_name, mmgr) \
do { \
QSE_CPP_CALL_DESTRUCTOR (ptr, class_name); \
QSE_CPP_CALL_PLACEMENT_DELETE1(ptr, mmgr); \
} while(0);
#endif

View File

@ -224,11 +224,7 @@ public:
~TcpServerL () QSE_CPP_NOEXCEPT
{
if (this->__lfunc)
{
//delete this->__lfunc;
this->getMmgr()->dispose (this->__lfunc);
}
if (this->__lfunc) QSE_CPP_DELETE_WITH_MMGR (this->__lfunc, Callable, this->getMmgr()); //delete this->__lfunc;
}
template <typename T>
@ -247,11 +243,7 @@ public:
return -1;
}
if (this->__lfunc)
{
//delete this->__lfunc;
this->getMmgr()->dispose (this->__lfunc);
}
if (this->__lfunc) QSE_CPP_DELETE_WITH_MMGR (this->__lfunc, Callable, this->getMmgr()); //delete this->__lfunc;
this->__lfunc = lf;
return 0;
}

View File

@ -169,7 +169,7 @@ public:
ThreadL (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr), __lfunc(nullptr) {}
~ThreadL () QSE_CPP_NOEXCEPT
{
if (this->__lfunc) this->getMmgr()->dispose(this->__lfunc); //delete this->__lfunc;
if (this->__lfunc) QSE_CPP_CALL_DESTRUCTOR (this->__lfunc, Callable, this->getMmgr()); //delete this->__lfunc;
}
static int call_func (qse_thr_t* thr, void* ctx)
@ -183,7 +183,7 @@ public:
//int start (T f, int flags) QSE_CPP_NOEXCEPT
{
if (this->thr.__state == QSE_THR_RUNNING) return -1;
if (this->__lfunc) this->getMmgr()->dispose (this->__lfunc); //delete this->__lfunc;
if (this->__lfunc) QSE_CPP_CALL_DESTRUCTOR (this->__lfunc, Callable, this->getMmgr()); //delete this->__lfunc;
try
{
// TODO: are there any ways to achieve this without memory allocation?