简体   繁体   English

如何修复poco Poco :: Net :: TCPServerParams()valgrind确定泄漏

[英]How to fix poco Poco::Net::TCPServerParams() valgrind definite leak

I am using Poco and I am creating TCP server params as below: Poco::Net::TCPServerParams *pParams = new Poco::Net::TCPServerParams(); 我正在使用Poco,我正在创建TCP服务器参数,如下所示:Poco :: Net :: TCPServerParams * pParams = new Poco :: Net :: TCPServerParams();

When I use Valgring, I get definite memory leak: 1271 ==00:00:01:37.131 48682== at 0x4C2903F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so ) 当我使用Valgring时,我得到明确的内存泄漏:1271 == 00:00:01:37.131 48682 ==在0x4C2903F:operator new(unsigned long)(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)

at line: Poco::Net::TCPServerParams *pParams = new Poco::Net::TCPServerParams(); 在线:Poco :: Net :: TCPServerParams * pParams = new Poco :: Net :: TCPServerParams();

Poco version: dpkg -l | Poco版:dpkg -l | grep poco ii libpoco-dev 1.6.1-AVA3 amd64 C++ Portable Components (POCO) Development files ii libpoconet31 1.6.1-AVA3 amd64 C++ Portable Components (POCO) Network library grep poco ii libpoco-dev 1.6.1-AVA3 amd64 C ++可移植组件(POCO)开发文件ii libpoconet31 1.6.1-AVA3 amd64 C ++可移植组件(POCO)网络库

To fix it, I used delete on the pointer and but I get an error 为了修复它,我在指针上使用了删除但是我收到了一个错误

delete pParams

'TCPServerParams.h:98:10: error: 'virtual Poco::Net::TCPServerParams::~TCPServerParams()' is protected' 'TCPServerParams.h:98:10:错误:'虚拟Poco :: Net :: TCPServerParams :: ~TCPServerParams()'受保护'

A common way to use TCPServerParams is that it works with TCPServer together. 使用TCPServerParams常用方法是它与TCPServer一起使用。 First you create TCPServerParams by new then it is passed into TCPServer which takes ownership of params. 首先,您使用new创建TCPServerParams ,然后将其传递到TCPServer ,后者拥有params的所有权。 It is described in reference of TCPServer . 它在TCPServer的参考文献中有所描述。

The server also takes ownership of the TCPServerParams object. 服务器还拥有TCPServerParams对象的所有权。

so TCPServer deletes param instance where it is no needed. 所以TCPServer删除不需要的param实例。


You cannot delete TCPServerParams manually by delete because destructor is protected. 您无法通过delete手动删除TCPServerParams ,因为析构函数受到保护。

In Poco library many classes have protected destructor, it forces you to use Poco::AutoPtr class to manage lifetime of instances. 在Poco库中,许多类都保护了析构函数,它强制您使用Poco::AutoPtr类来管理实例的生命周期。

TCPServerParams derives from RefCountedObject . TCPServerParams派生自RefCountedObject RefCountedObject provides reference counter mechanism. RefCountedObject提供了引用计数器机制。 It has release method which deletes an object when the lifetime of AutoPtr ends. 它具有release方法,当AutoPtr的生命周期结束时删除对象。

So you could write: 所以你可以写:

Poco::AutoPtr<Poco::Net::TCPServerParams> p(new Poco::Net::TCPServerParams());

and memory is released automatically by AutoPtr . 和内存由AutoPtr自动AutoPtr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM