原来我的鳖脚英语老外还是看的懂啊

2013年7月1日 由 Creater 留言 »

这是我最近阅读的一个开源项目,但是我发现一个BUG,所以向作者提交了该问题。

Thank you very much!
I will try to recreate your problem with using the standard SocketHandlerEp class. I have
only myself used it as a base class for specialized handlers, so there is a problem left to
be discovered!
Best regards
/ah

On Sat, Jul 6, 2013 at 5:23 AM, Unix8.net,Unix吧 wrote:
Dear autor!
When I read the source of “C++ Socket Library”, i wirted a test, but have a “Segmentation fault” error.

When i use SocketHandler as the handler, there is no error.
When i write a class which inherit from SocketHandlerEp as the handler, there is no error.
When i use SocketHandlerEp as the handler, there is a fatal error.

【I am a Chinese person, please forgive my English is not good】

Code:

#include <stdio.h>
#include <SocketHandler.h>
#include <SocketHandlerEp.h>
#include <TcpSocket.h>
#include <Mutex.h>
#include <StdoutLog.h>
#include <ListenSocket.h>
#include <Lock.h>


class MyHandler;
class InfoSocket : public TcpSocket
{
public:
	InfoSocket(ISocketHandler& h) : TcpSocket(h) {}
	~InfoSocket() {}

	void OnAccept() {
		printf("Accept\n");
	}

	//Don't use OnRawData
	void OnRead()
	{
		TcpSocket::OnRead();
		size_t n = ibuf.GetLength();
		printf("ibuf have %d bytes\n", n);
		if (n > 0)
		{
			char tmp[100];
			ibuf.Read(tmp,n);
			printf("Read %d bytes:\n",n);
			for (size_t i = 0; i < n; i++)
			{
				printf("%c",isprint(tmp[i]) ? tmp[i] : '.');
			}
			printf("\n");

		}
	}
};

class MyHandler : public SocketHandlerEp
{
public:
	MyHandler(IMutex& m,StdoutLog *p) : SocketHandlerEp(m, p) {}
	~MyHandler() {}
};

// use SocketHandlerEp
#define TEST_SOCKETHANDLEREP

//use SocketHandler
//#define TEST_SOCKETHANDLER

//use MyHandler, inherit SocketHandlerEp or SocketHandler
//#define TEST_INHERIT_SOCKETHANDLEREP


int main(int argc,char *argv[])
{
	Mutex lock;
	StdoutLog log;

#if  defined(TEST_SOCKETHANDLEREP)
	SocketHandlerEp h(lock, &log);
#elif defined(TEST_SOCKETHANDLER)
	SocketHandler h(lock, &log);
#elif defined(TEST_INHERIT_SOCKETHANDLEREP)
	MyHandler h(lock, &log);
#endif

	ListenSocket<InfoSocket> l(h);
	if (l.Bind(11002))
	{
		printf("Bind() failed\n");
		exit(-2);
	}
	h.Add(&l);
	
	bool quit = false;
	while (!quit)
	{
		h.Select(1, 0);
	}
}

When I debug found that cause of the error is “m_mutex.Lock()”.

Thank you!
Best Regards!
2013-07-06

广告位

发表评论

你必须 登陆 方可发表评论.