|
From: | lake |
Subject: | Serious memory leaks with TCPSession class |
Date: | Thu, 12 Dec 2002 16:54:34 +0800 |
hi all,
I'm using TCPSession in a multithread app under
Redhat 7.1,
and found the class derived from TCPSession had the
problem
of memory leak.so i made a test like below,it's
modified from the
tcpthread.cpp in demo.
i'm using CommonCPP2-1.0.5,as you can see from the code,
in the main(),the program listens to a specified
port 4096,
whenever a client attempts to connect to
it,it creates a new myTCPSession class,
then the myTCPSession class accepts the
connection,and sends sth. to the client
,print sth. in the screen and wait for 5 seconds to
return from its run().
the result is:
----------------------------------
address@hidden test]# ./tcp
testing addr: 255.255.255.255:4096
binding for: (null):4096 <----------a: begin to listen to the port 4096 before create
<----------b: process
the 1st client's connection
accepting from: team-2:2268 creating session client object after create enter thread 1 ending session <-----------c: quit from the 1st myTCPSession object before create <-----------d: process the 2nd client's connection accepting from: team-2:2310 creating session client object after create enter thread 2 ending session <------------e: quit from the 2nd myTCPSession object ----------------------------------
the usage of memory acquired from top
is:
----------------------------------
a:
PID USER PRI NI SIZE RSS
SHARE STAT %CPU %MEM TIME COMMAND
2798 root 9 0 1224 1224 1024 S 0.0 0.4 0:00 tcp b:
PID USER PRI NI SIZE RSS
SHARE STAT %CPU %MEM TIME COMMAND
2798 root 15 0 1260 1260 1040 S 0.1 0.4 0:00 tcp c:
PID USER PRI NI SIZE RSS
SHARE STAT %CPU %MEM TIME COMMAND
2798 root 9 0 1264 1264 1044 S 0.0 0.4 0:00 tcp d:
PID USER PRI NI SIZE RSS
SHARE STAT %CPU %MEM TIME COMMAND
2798 root 9 0 1272 1272 1044 S 0.0 0.4 0:00 tcp e:
PID USER PRI NI SIZE RSS
SHARE STAT %CPU %MEM TIME COMMAND
2798 root 9 0 1272 1272 1044 S 0.0 0.4 0:00 tcp ---------------------------------- The total amount of physical memory used by tcp
continiously increased and it never
decreased!!
and then everytime the client connected to it,the
RSS increased 8k,the amount is fixed!
i don't know whether it's a bug of TCPSession
class,who can help me??? :(
the modified code is:
-------------------------------------8<------------------------------------------------------
#include <cc++/socket.h>
#include <cstdlib> #ifdef CCXX_NAMESPACES
using namespace std; using namespace ost; #endif class myTCPSocket : public
TCPSocket
{ protected: bool onAccept(const InetHostAddress &ia, tpport_t port); public:
myTCPSocket(InetAddress &ia); }; class myTCPSession : public
TCPSession
{ private: static Mutex mutex; void run(void); void final(void); public:
myTCPSession(TCPSocket &server); static volatile int count; }; myTCPSocket::myTCPSocket(InetAddress &ia) :
TCPSocket(ia, 4096) {};
bool myTCPSocket::onAccept(const InetHostAddress
&ia, tpport_t port)
{ cout << "accepting from: " << ia.getHostname() << ":" << port << endl;; return true; } volatile int myTCPSession::count = 0;
Mutex myTCPSession::mutex;
myTCPSession::myTCPSession(TCPSocket &server)
:
TCPSession(server) { cout << "creating session client object" << endl; mutex.enterMutex(); ++count; mutex.leaveMutex(); // unsetf(ios::binary); } void
myTCPSession::run(void)
{ InetAddress addr = getLocal(); *tcp() << "welcome to " << addr.getHostname() << " from socket " << so << endl; mutex.enterMutex(); *tcp() << "called from thread " << count << endl; cout << "enter thread " << count << endl; mutex.leaveMutex(); sleep(5000); *tcp() << "ending session" << endl; cout << "ending session" << endl; } void myTCPSession::final(void)
{ delete this; } int main()
{ myTCPSession *tcp; BroadcastAddress addr; addr = "255.255.255.255"; cout << "testing addr: " << addr.getHostname() << ":" << 4096 << endl; addr = "*"; cout << "binding for: " << addr.getHostname() << ":" << 4096 << endl; try
{ myTCPSocket server(addr); while(1) { if(server.isPendingConnection(1000))
{ cout << "before create" << endl; tcp = new myTCPSession(server); cout << "after create" << endl; tcp->start(); } } } catch(Socket *socket) { tpport_t port; int err = socket->getErrorNumber(); InetAddress saddr = (InetAddress)socket->getPeer(&port); cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << err << endl; if(err == Socket::errBindingFailed) { cerr << "bind failed; port busy" << endl; ::exit(-1); } else cerr << "client socket failed" << endl; } cout << "timeout after 30 seconds inactivity, exiting" << endl; return 0; } ---------------------------------------------------->8------------------------------------------------------------------
|
[Prev in Thread] | Current Thread | [Next in Thread] |