0
回答
boost::asio与PocoMySQL冲突,如何解决?
开发十年,就只剩下这套Java开发体系了   

    我访问数据库使用的是Poco的PocoMySQL库,与网络交互的使用的是Boost的asio库,在使用到asio库中的方法tcp::acceptor accpetor的async_accept方法时,在进行mysql_library_init函数调用的时候会报如下错误:

*** An Access Violation occurred in "D:\_fengbishikaifa\inforcn_src\ProtocolServer\Debug\DBTest.exe" :

The instruction at 0000000076E611F1 tried to read from an invalid address, 0000000000000250

 *** enter .exr 00000000000BDBF0 for the exception record
 ***  enter .cxr 00000000000BD700 for the context
 *** then kb to get the faulting stack

Unhandled exception at 0x745f9eb2 in DBTest.exe: 0xC000041D: 用户回调期间遇到未经处理的异常。



程序完整代码如下:

// DBTest.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <time.h>
#include <WinSock2.h>


#define POCO_NO_UNWINDOWS
#include "Poco/Data/MySQL/Connector.h"
#include "mysql.h"

#define WIN32_LEAN_AND_MEAN
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

using boost::asio::ip::udp;
using boost::asio::ip::tcp;
using boost::asio::ip::address;
using namespace boost::asio;

#define max_len 1024

class clientSession
	:public boost::enable_shared_from_this<clientSession>
{
public:
	clientSession(boost::asio::io_service& ioservice)
		:m_socket(ioservice)
	{
		memset(data_,'\0',sizeof(data_));
	}
	~clientSession()
	{}
	tcp::socket& socket()
	{
		return m_socket;
	}
	void start()
	{
		boost::asio::async_write(m_socket,
			boost::asio::buffer("link successed!"),
			boost::bind(&clientSession::handle_write,shared_from_this(),
			boost::asio::placeholders::error));

		/*async_read跟客户端一样,还是不能进入handle_read函数,如果你能找到问题所在,请告诉我,谢谢*/
		// --已经解决,boost::asio::async_read(...)读取的字节长度不能大于数据流的长度,否则就会进入
		// ioservice.run()线程等待,read后面的就不执行了。
		//boost::asio::async_read(m_socket,boost::asio::buffer(data_,max_len),

		//        boost::bind(&clientSession::handle_read,shared_from_this(),

		//        boost::asio::placeholders::error));


		//max_len可以换成较小的数字,就会发现async_read_some可以连续接收未收完的数据

		m_socket.async_read_some(boost::asio::buffer(data_,max_len),
			boost::bind(&clientSession::handle_read,shared_from_this(),
			boost::asio::placeholders::error));
	}
private:
	void handle_write(const boost::system::error_code& error)
	{

		if(error)
		{
			m_socket.close();
		}

	}
	void handle_read(const boost::system::error_code& error)
	{

		if(!error)
		{
			std::cout << data_ << std::endl;
			//boost::asio::async_read(m_socket,boost::asio::buffer(data_,max_len),

			//    boost::bind(&clientSession::handle_read,shared_from_this(),

			//    boost::asio::placeholders::error));

			m_socket.async_read_some(boost::asio::buffer(data_,max_len),
				boost::bind(&clientSession::handle_read,shared_from_this(),
				boost::asio::placeholders::error));
		}
		else
		{
			m_socket.close();
		}

	}
private:
	tcp::socket m_socket;
	char data_[max_len];
};

typedef boost::shared_ptr<clientSession> session_ptr;


class test
{
public:
	test(){

	}
	void init()
	{	short port = 8100/*argv[1]*/;
		boost::asio::io_service ioservice;
		boost::asio::io_service& m_ioservice(ioservice);
			tcp::endpoint endPoint(tcp::v4(),port);
		session_ptr new_session(new clientSession(ioservice));
		tcp::acceptor acceptor_(ioservice,endPoint);

		acceptor_.async_accept(new_session->socket(),
			boost::bind(&test::handle_accept_xxx_1_3,
			this,boost::asio::placeholders::error,
			new_session));
	}
	void handle_accept_xxx_1_3(const boost::system::error_code& error,session_ptr& session)
	{

	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	test test;
	test.init();

	std::cout<<"T_main"<<std::endl;

	getchar();
	return 0;
	
}



举报
风水月
发帖于5年前 0回/682阅
顶部