diff --git a/qse/include/qse/Exception.hpp b/qse/include/qse/Exception.hpp index 366e625e..715ffe57 100644 --- a/qse/include/qse/Exception.hpp +++ b/qse/include/qse/Exception.hpp @@ -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) {} \ } diff --git a/qse/include/qse/cmn/LinkedList.hpp b/qse/include/qse/cmn/LinkedList.hpp index 8781bcea..649c892a 100644 --- a/qse/include/qse/cmn/LinkedList.hpp +++ b/qse/include/qse/cmn/LinkedList.hpp @@ -362,7 +362,7 @@ public: } else { - while (n) + while (n) { this->prepend (n->value); n = (Node*)n->prev; diff --git a/qse/include/qse/cmn/Mmgr.hpp b/qse/include/qse/cmn/Mmgr.hpp index 48e6dd40..45f5f2e0 100644 --- a/qse/include/qse/cmn/Mmgr.hpp +++ b/qse/include/qse/cmn/Mmgr.hpp @@ -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 diff --git a/qse/include/qse/si/TcpServer.hpp b/qse/include/qse/si/TcpServer.hpp index 69f8da2d..45910e44 100644 --- a/qse/include/qse/si/TcpServer.hpp +++ b/qse/include/qse/si/TcpServer.hpp @@ -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 @@ -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; } diff --git a/qse/include/qse/si/Thread.hpp b/qse/include/qse/si/Thread.hpp index 1e01ee34..73a3a360 100644 --- a/qse/include/qse/si/Thread.hpp +++ b/qse/include/qse/si/Thread.hpp @@ -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? diff --git a/qse/lib/cmn/Mmgr.cpp b/qse/lib/cmn/Mmgr.cpp index 42ca380d..8b6de950 100644 --- a/qse/lib/cmn/Mmgr.cpp +++ b/qse/lib/cmn/Mmgr.cpp @@ -47,9 +47,9 @@ void Mmgr::free_mem (mmgr_t* mmgr, void* ptr) QSE_CPP_NOEXCEPT ((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); return ptr; } @@ -70,7 +70,7 @@ void Mmgr::setDFL (Mmgr* mmgr) QSE_CPP_NOEXCEPT 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); } @@ -84,7 +84,7 @@ void operator delete (void* ptr, QSE::Mmgr* mmgr) 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 // less conflicting with the stock ::operator new() that doesn't allocate. diff --git a/qse/lib/si/TcpServer.cpp b/qse/lib/si/TcpServer.cpp index ca0f40bb..8112295e 100644 --- a/qse/lib/si/TcpServer.cpp +++ b/qse/lib/si/TcpServer.cpp @@ -118,7 +118,8 @@ void TcpServer::free_all_listeners () QSE_CPP_NOEXCEPT qse_mux_delete (this->listener_list.mux, &evt); 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) @@ -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 // a bit awkward. but socket.accept() can be passed // the socket field inside the client object. - try { client = new(server->getMmgr()) Client (lsck); } + try { client = new(server->getMmgr()) Client(lsck); } catch (...) { // 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; } + 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); #if defined(_WIN32) if (client->start(Thread::DETACHED) <= -1) #else if (client->start(0) <= -1) #endif - { - server->getMmgr()->dispose (client); //delete client; - return; - } - - try { server->client_list.append (client); } - catch (...) { // 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; - } } } @@ -370,8 +375,6 @@ int TcpServer::start (const qse_char_t* addrs) QSE_CPP_NOEXCEPT this->server_serving = true; this->setStopRequested (false); - Client* client = QSE_NULL; - try { 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 (); - if (client != QSE_NULL) this->getMmgr()->dispose (client); // delete client; } catch (...) { this->delete_all_clients (); - if (client != QSE_NULL) this->getMmgr()->dispose (client); //delete client; this->setErrorCode (E_EEXCEPT); this->server_serving = false; @@ -451,8 +452,7 @@ void TcpServer::delete_dead_clients () QSE_CPP_NOEXCEPT #if !defined(_WIN32) p->join (); #endif - - this->getMmgr()->dispose (p); //delete p; + QSE_CPP_DELETE_WITH_MMGR (p, Client, this->getMmgr()); // delete p np2 = np; np = np->getNextNode(); this->client_list.remove (np2); continue; @@ -484,7 +484,7 @@ void TcpServer::delete_all_clients () QSE_CPP_NOEXCEPT #else p->join (); #endif - this->getMmgr()->dispose (p); //delete p; + QSE_CPP_DELETE_WITH_MMGR (p, Client, this->getMmgr()); // delete p np2 = np; np = np->getNextNode(); this->client_list.remove (np2); } diff --git a/qse/samples/si/tcpsvr01.cpp b/qse/samples/si/tcpsvr01.cpp index b31fc413..8ff70ebb 100644 --- a/qse/samples/si/tcpsvr01.cpp +++ b/qse/samples/si/tcpsvr01.cpp @@ -29,7 +29,7 @@ public: qse_ssize_t n; 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()) { @@ -41,7 +41,7 @@ public: clisock->send (bb, n); } - qse_printf (QSE_T("byte to %s\n"), addrbuf); + //qse_printf (QSE_T("byte to %s\n"), addrbuf); return 0; } }; @@ -64,7 +64,7 @@ static int test1 (void) cliaddr->toStrBuf(addrbuf, QSE_COUNTOF(addrbuf)); qse_printf (QSE_T("hello word..from %s\n"), addrbuf); - + while (!server.isStopRequested()) { if ((n = clisock->receive(bb, QSE_COUNTOF(bb))) <= 0)