fixed the code to call the destructor when dispoing an object with mmgr
This commit is contained in:
parent
87e0cf1b40
commit
9cbd6cd7fe
@ -43,7 +43,7 @@ class QSE_EXPORT Exception
|
|||||||
public:
|
public:
|
||||||
Exception (
|
Exception (
|
||||||
const qse_char_t* name, const qse_char_t* msg,
|
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)
|
name(name), msg(msg)
|
||||||
#if !defined(QSE_NO_LOCATION_IN_EXCEPTION)
|
#if !defined(QSE_NO_LOCATION_IN_EXCEPTION)
|
||||||
, file(file), line(line)
|
, file(file), line(line)
|
||||||
@ -69,7 +69,7 @@ public:
|
|||||||
{ \
|
{ \
|
||||||
public: \
|
public: \
|
||||||
ex_name (const qse_char_t* name, const qse_char_t* msg, \
|
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) {} \
|
QSE::Exception (name, msg, file, line) {} \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -362,7 +362,7 @@ public:
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while (n)
|
while (n)
|
||||||
{
|
{
|
||||||
this->prepend (n->value);
|
this->prepend (n->value);
|
||||||
n = (Node*)n->prev;
|
n = (Node*)n->prev;
|
||||||
|
@ -54,9 +54,6 @@ public:
|
|||||||
|
|
||||||
QSE_EXCEPTION (MemoryError);
|
QSE_EXCEPTION (MemoryError);
|
||||||
|
|
||||||
protected:
|
|
||||||
bool raise_exception;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
///
|
///
|
||||||
/// The Mmgr() function builds a memory manager composed of bridge
|
/// 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
|
/// allocation. if it fails, it raise an exception if it's
|
||||||
/// configured to do so.
|
/// 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);
|
void* xptr = this->allocMem (n);
|
||||||
if (!xptr && raise_exception) QSE_THROW (MemoryError);
|
if (!xptr && raise_exception) QSE_THROW (MemoryError);
|
||||||
@ -95,14 +92,14 @@ public:
|
|||||||
/// The callocate() function allocates memory like allocate() and
|
/// The callocate() function allocates memory like allocate() and
|
||||||
/// clears the memory before returning.
|
/// 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
|
/// The reallocate() function calls reallocMem() for memory
|
||||||
/// reallocation. if it fails, it raise an exception if it's
|
/// reallocation. if it fails, it raise an exception if it's
|
||||||
/// configured to do so.
|
/// 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);
|
void* xptr = this->reallocMem (ptr, n);
|
||||||
if (!xptr && raise_exception) QSE_THROW (MemoryError);
|
if (!xptr && raise_exception) QSE_THROW (MemoryError);
|
||||||
@ -174,7 +171,7 @@ protected:
|
|||||||
QSE_END_NAMESPACE(QSE)
|
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)
|
#if defined(QSE_CPP_NO_OPERATOR_DELETE_OVERLOADING)
|
||||||
QSE_EXPORT void qse_operator_delete (void* ptr, QSE::Mmgr* mmgr);
|
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);
|
QSE_EXPORT void operator delete (void* ptr, QSE::Mmgr* mmgr);
|
||||||
#endif
|
#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
|
#if 0
|
||||||
// i found no way to delete an array allocated with
|
// 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);
|
void operator delete[] (void* ptr, QSE::Mmgr* mmgr);
|
||||||
#endif
|
#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
|
#endif
|
||||||
|
@ -224,11 +224,7 @@ public:
|
|||||||
|
|
||||||
~TcpServerL () QSE_CPP_NOEXCEPT
|
~TcpServerL () QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (this->__lfunc)
|
if (this->__lfunc) QSE_CPP_DELETE_WITH_MMGR (this->__lfunc, Callable, this->getMmgr()); //delete this->__lfunc;
|
||||||
{
|
|
||||||
//delete this->__lfunc;
|
|
||||||
this->getMmgr()->dispose (this->__lfunc);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@ -247,11 +243,7 @@ public:
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->__lfunc)
|
if (this->__lfunc) QSE_CPP_DELETE_WITH_MMGR (this->__lfunc, Callable, this->getMmgr()); //delete this->__lfunc;
|
||||||
{
|
|
||||||
//delete this->__lfunc;
|
|
||||||
this->getMmgr()->dispose (this->__lfunc);
|
|
||||||
}
|
|
||||||
this->__lfunc = lf;
|
this->__lfunc = lf;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -169,7 +169,7 @@ public:
|
|||||||
ThreadL (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr), __lfunc(nullptr) {}
|
ThreadL (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT: Thread(mmgr), __lfunc(nullptr) {}
|
||||||
~ThreadL () QSE_CPP_NOEXCEPT
|
~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)
|
static int call_func (qse_thr_t* thr, void* ctx)
|
||||||
@ -183,7 +183,7 @@ public:
|
|||||||
//int start (T f, int flags) QSE_CPP_NOEXCEPT
|
//int start (T f, int flags) QSE_CPP_NOEXCEPT
|
||||||
{
|
{
|
||||||
if (this->thr.__state == QSE_THR_RUNNING) return -1;
|
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
|
try
|
||||||
{
|
{
|
||||||
// TODO: are there any ways to achieve this without memory allocation?
|
// TODO: are there any ways to achieve this without memory allocation?
|
||||||
|
@ -47,9 +47,9 @@ void Mmgr::free_mem (mmgr_t* mmgr, void* ptr) QSE_CPP_NOEXCEPT
|
|||||||
((Mmgr*)mmgr->ctx)->freeMem (ptr);
|
((Mmgr*)mmgr->ctx)->freeMem (ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* Mmgr::callocate (qse_size_t n, bool raise_exception)
|
void* Mmgr::callocate (qse_size_t n, bool raise_exception) throw(MemoryError)
|
||||||
{
|
{
|
||||||
void* ptr = this->allocate (n, raise_exception);
|
void* ptr = this->allocate(n, raise_exception);
|
||||||
QSE_MEMSET (ptr, 0, n);
|
QSE_MEMSET (ptr, 0, n);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ void Mmgr::setDFL (Mmgr* mmgr) QSE_CPP_NOEXCEPT
|
|||||||
QSE_END_NAMESPACE(QSE)
|
QSE_END_NAMESPACE(QSE)
|
||||||
/////////////////////////////////
|
/////////////////////////////////
|
||||||
|
|
||||||
void* operator new (qse_size_t size, QSE::Mmgr* mmgr)
|
void* operator new (qse_size_t size, QSE::Mmgr* mmgr) throw(QSE::Mmgr::MemoryError)
|
||||||
{
|
{
|
||||||
return mmgr->allocate (size);
|
return mmgr->allocate (size);
|
||||||
}
|
}
|
||||||
@ -84,7 +84,7 @@ void operator delete (void* ptr, QSE::Mmgr* mmgr)
|
|||||||
mmgr->dispose (ptr);
|
mmgr->dispose (ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr)
|
void* operator new (qse_size_t size, QSE::Mmgr* mmgr, void* existing_ptr) throw(QSE::Mmgr::MemoryError)
|
||||||
{
|
{
|
||||||
// mmgr unused. i put it in the parameter list to make this function
|
// mmgr unused. i put it in the parameter list to make this function
|
||||||
// less conflicting with the stock ::operator new() that doesn't allocate.
|
// less conflicting with the stock ::operator new() that doesn't allocate.
|
||||||
|
@ -118,7 +118,8 @@ void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT
|
|||||||
qse_mux_delete (this->listener_list.mux, &evt);
|
qse_mux_delete (this->listener_list.mux, &evt);
|
||||||
|
|
||||||
lp->close ();
|
lp->close ();
|
||||||
this->getMmgr()->dispose(lp); //delete lp;
|
|
||||||
|
QSE_CPP_DELETE_WITH_MMGR(lp, Listener, this->getMmgr()); // delete lp
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->listener_list.mux_pipe[0] >= 0)
|
if (this->listener_list.mux_pipe[0] >= 0)
|
||||||
@ -190,7 +191,7 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
|
|||||||
// allocating the client object before accept is
|
// allocating the client object before accept is
|
||||||
// a bit awkward. but socket.accept() can be passed
|
// a bit awkward. but socket.accept() can be passed
|
||||||
// the socket field inside the client object.
|
// the socket field inside the client object.
|
||||||
try { client = new(server->getMmgr()) Client (lsck); }
|
try { client = new(server->getMmgr()) Client(lsck); }
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
// memory alloc failed. accept the connection and close it.
|
// memory alloc failed. accept the connection and close it.
|
||||||
@ -214,24 +215,28 @@ void TcpServer::dispatch_mux_event (qse_mux_t* mux, const qse_mux_evt_t* evt) QS
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
server->client_list.append (client);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
// TODO: logging.
|
||||||
|
QSE_CPP_DELETE_WITH_MMGR (client, Client, server->getMmgr()); // delete client
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
client->setStackSize (server->thread_stack_size);
|
client->setStackSize (server->thread_stack_size);
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
if (client->start(Thread::DETACHED) <= -1)
|
if (client->start(Thread::DETACHED) <= -1)
|
||||||
#else
|
#else
|
||||||
if (client->start(0) <= -1)
|
if (client->start(0) <= -1)
|
||||||
#endif
|
#endif
|
||||||
{
|
|
||||||
server->getMmgr()->dispose (client); //delete client;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try { server->client_list.append (client); }
|
|
||||||
catch (...)
|
|
||||||
{
|
{
|
||||||
// TODO: logging.
|
// TODO: logging.
|
||||||
server->getMmgr()->dispose (client); //delete client;
|
// don't delete the client object here. as it's int the client_list,
|
||||||
|
// this->delete_dead_clients() should delete this client later.
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -370,8 +375,6 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
|
|||||||
this->server_serving = true;
|
this->server_serving = true;
|
||||||
this->setStopRequested (false);
|
this->setStopRequested (false);
|
||||||
|
|
||||||
Client* client = QSE_NULL;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (this->setup_listeners(addrs) <= -1)
|
if (this->setup_listeners(addrs) <= -1)
|
||||||
@ -399,12 +402,10 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT
|
|||||||
}
|
}
|
||||||
|
|
||||||
this->delete_all_clients ();
|
this->delete_all_clients ();
|
||||||
if (client != QSE_NULL) this->getMmgr()->dispose (client); // delete client;
|
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
this->delete_all_clients ();
|
this->delete_all_clients ();
|
||||||
if (client != QSE_NULL) this->getMmgr()->dispose (client); //delete client;
|
|
||||||
|
|
||||||
this->setErrorCode (E_EEXCEPT);
|
this->setErrorCode (E_EEXCEPT);
|
||||||
this->server_serving = false;
|
this->server_serving = false;
|
||||||
@ -451,8 +452,7 @@ void TcpServer::delete_dead_clients () QSE_CPP_NOEXCEPT
|
|||||||
#if !defined(_WIN32)
|
#if !defined(_WIN32)
|
||||||
p->join ();
|
p->join ();
|
||||||
#endif
|
#endif
|
||||||
|
QSE_CPP_DELETE_WITH_MMGR (p, Client, this->getMmgr()); // delete p
|
||||||
this->getMmgr()->dispose (p); //delete p;
|
|
||||||
np2 = np; np = np->getNextNode();
|
np2 = np; np = np->getNextNode();
|
||||||
this->client_list.remove (np2);
|
this->client_list.remove (np2);
|
||||||
continue;
|
continue;
|
||||||
@ -484,7 +484,7 @@ void TcpServer::delete_all_clients () QSE_CPP_NOEXCEPT
|
|||||||
#else
|
#else
|
||||||
p->join ();
|
p->join ();
|
||||||
#endif
|
#endif
|
||||||
this->getMmgr()->dispose (p); //delete p;
|
QSE_CPP_DELETE_WITH_MMGR (p, Client, this->getMmgr()); // delete p
|
||||||
np2 = np; np = np->getNextNode();
|
np2 = np; np = np->getNextNode();
|
||||||
this->client_list.remove (np2);
|
this->client_list.remove (np2);
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ public:
|
|||||||
qse_ssize_t n;
|
qse_ssize_t n;
|
||||||
|
|
||||||
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
||||||
qse_printf (QSE_T("hello word..from %s\n"), addrbuf);
|
//qse_printf (QSE_T("hello word..from %s\n"), addrbuf);
|
||||||
|
|
||||||
while (!server->isStopRequested())
|
while (!server->isStopRequested())
|
||||||
{
|
{
|
||||||
@ -41,7 +41,7 @@ public:
|
|||||||
clisock->send (bb, n);
|
clisock->send (bb, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_printf (QSE_T("byte to %s\n"), addrbuf);
|
//qse_printf (QSE_T("byte to %s\n"), addrbuf);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -64,7 +64,7 @@ static int test1 (void)
|
|||||||
|
|
||||||
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf));
|
||||||
qse_printf (QSE_T("hello word..from %s\n"), addrbuf);
|
qse_printf (QSE_T("hello word..from %s\n"), addrbuf);
|
||||||
|
|
||||||
while (!server.isStopRequested())
|
while (!server.isStopRequested())
|
||||||
{
|
{
|
||||||
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
|
if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user