- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 MySQL C++ 连接器。 Valgrind 显示它在每个连接上泄漏 192 字节。它仅在没有线程的线程环境中泄漏内存它不会泄漏任何内存。我做错了什么?我需要调用一些其他函数来进行清理吗?示例代码:
#include <pthread.h>
#include <iostream>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
void* test(void* arg) {
try {
sql::Driver *driver;
sql::Connection *con;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
return NULL;
}
int main() {
pthread_t thread1, thread2, thread3, thread4;
pthread_create(&thread1, NULL, test, NULL);
pthread_create(&thread2, NULL, test, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
Valgrind 输出:
==10252== Memcheck, a memory error detector
==10252== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==10252== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==10252== Command: ./app/bin/app-test
==10252== Parent PID: 6312
==10252==
==10252==
==10252== HEAP SUMMARY:
==10252== in use at exit: 384 bytes in 2 blocks
==10252== total heap usage: 212 allocs, 210 frees, 208,400 bytes allocated
==10252==
==10252== 192 bytes in 1 blocks are definitely lost in loss record 1 of 2
==10252== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10252== by 0x5E1CB3E: my_thread_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5E1CE3C: my_thread_global_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5E1AA54: my_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5DF86CA: mysql_server_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x4EA3C08: sql::mysql::NativeAPI::getCApiHandle(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4EA40EA: sql::mysql::NativeAPI::MySQL_NativeDriverWrapper::MySQL_NativeDriverWrapper(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4EA4138: sql::mysql::NativeAPI::createNativeDriverWrapper(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4E6F2F3: sql::mysql::MySQL_Driver::MySQL_Driver(sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4E6F50B: sql::mysql::get_driver_instance_by_name(char const*) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x40AB8C: test(void*) (main.cc:17)
==10252== by 0x50D9E99: start_thread (pthread_create.c:308)
==10252==
==10252== 192 bytes in 1 blocks are definitely lost in loss record 2 of 2
==10252== at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10252== by 0x5E1CB3E: my_thread_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5DF86DC: mysql_server_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x5DFE85E: mysql_init (in /usr/lib/x86_64-linux-gnu/libmysqlclient.so.18.0.0)
==10252== by 0x4EA4A82: sql::mysql::NativeAPI::MySQL_NativeConnectionWrapper::MySQL_NativeConnectionWrapper(boost::shared_ptr<sql::mysql::NativeAPI::IMySQLCAPI>) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4EA4014: sql::mysql::NativeAPI::MySQL_NativeDriverWrapper::conn_init() (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x4E6F0DC: sql::mysql::MySQL_Driver::connect(sql::SQLString const&, sql::SQLString const&, sql::SQLString const&) (in /usr/lib/libmysqlcppconn.so.5.1.1.0)
==10252== by 0x40ABE8: test(void*) (main.cc:18)
==10252== by 0x50D9E99: start_thread (pthread_create.c:308)
==10252== by 0x5AFCCBC: clone (clone.S:112)
==10252==
==10252== LEAK SUMMARY:
==10252== definitely lost: 384 bytes in 2 blocks
==10252== indirectly lost: 0 bytes in 0 blocks
==10252== possibly lost: 0 bytes in 0 blocks
==10252== still reachable: 0 bytes in 0 blocks
==10252== suppressed: 0 bytes in 0 blocks
==10252==
==10252== For counts of detected and suppressed errors, rerun with: -v
==10252== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 2 from 2)
最佳答案
正如 WhozCraig 所建议的那样,您可以将 delete Driver;
添加到您的测试函数中,但我建议使用 auto_ptr
或 C++11 unique_ptr
或 shared_ptr
用于所有 MYSQL,您永远不必担心内存泄漏
以这个为例
使用 C++11
std::unique_ptr< sql::Connection > con( driver->connect("tcp://127.0.0.1:3306", "root", "root"));
或者 C++
std::auto_ptr< sql::Connection > con( driver->connect("tcp://127.0.0.1:3306", "root", "root"));
编辑
你不能只是删除驱动程序
,我有时间会仔细研究
更新
我查看了driver.h
源码,确实是保护的:
所以你不能只使用删除,但是在
virtual ~Driver() {}public:
中有两个
virtual void threadInit() = 0;
virtual void threadEnd() = 0;
这可能是你需要的
还有这个example这可能非常有用,并且与您的做法有所不同
关于c++ - MySQL C++ 连接器中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13082389/
我们被要求从 Tomcat 1.6 迁移到 TomEE。在我们的应用程序中,我们使用 tomcat 作为嵌入式服务器。当我们尝试根据以下教程“http://www.copperykeenclaws.c
我需要一些有关配置文件的帮助。我已经在我的项目中包含了MySql.Data.dll,但是我如何告诉配置文件这个dll包含MySQL连接器? NHibernate.Connection.Dr
我花了几个小时盯着这段代码。请新鲜的眼睛! 这是查询的简化版本: You have an error in your SQL syntax; check the manual that corresp
我正在开发一个应用程序,它可以在不使用任何网络服务的情况下将数据插入数据库。我用 MySQL Workbench 在我的笔记本上创建了一个数据库。我可以使用模拟器将数据插入数据库,但我无法使用手机将数
我刚刚发现了 Zimbra,并且有一个用 Java 编写的连接器。我一直在网上寻找其他人的一些文档或经验,但找不到任何东西。是否有任何关于 API 的良好文档,以便我可以开始并检查可以用它做什么? 谢
我正在使用 C++ mysql 连接器在我的 mysql 数据库中执行操作。 我的 C++ 程序是一个实时应用程序(rest api),它始终在云端运行,始终等待用户请求。 当我启动第一种类型的程序时
我有一个 C 进程正在快速写入 mysql 数据库~每秒 10 次。此过程使用 MySql C 连接器。 运行约2分钟后,进程挂起,系统监视器显示 "futex_wait_queue_me" ,还有
有谁知道使用 TraceListener 为 MySQL 连接器/网络启用跟踪的方法我希望它记录实际针对数据库运行的 SQL 查询,即查看参数值被替换的 SQL。 最佳答案 从 mysql 5.1.2
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
作为 EF 菜鸟,我正在尝试将 Entity Framework 6 Code First 与我安装在我的开发计算机上的 MySql Server 5.6 结合使用。 我做了一个非常小的测试控制台项目
我需要将 Google Spreadsheet 与 JasperReports Server 一起使用,为此我需要一个用于 Google Spreadsheet 的 JDBC 连接器。 我找到了这个
我已经安装了 Tycho m2e 连接器,如下所述:http://codeandme.blogspot.ru/2012/12/tycho-build-1-building-plug-ins.html
我被要求使用 SAP .NET 连接器。我目前使用 .NET 4.0 和 VS2010。有什么我需要降级的吗? 另外,有没有人知道有关如何使用它的任何当前在线教程?我所拥有的只是来自 SAP 的信息,
我想知道什么是 m2e 连接器。除了这个页面,我在互联网上没有找到太多描述它们的内容: http://objectledge.org/confluence/display/TOOLS/M2E+Conn
是否可以通过 VGA 连接器镜像屏幕?找不到任何关于此的内容。 最佳答案 我一直在寻找和你一样的东西。上周末我写了一个小的 UIApplication 类别来添加镜像支持。我在 Google Code
我正在开发从 SQL Server Db 提取 CDC 数据的逻辑应用程序。我正在使用“获取行”操作,但当我尝试使用过滤查询参数时,问题就出现了。 代码 eq '793'(有效) __$operati
用例: 应用程序使用spark处理数据5分钟,要处理的数据可能是数据存储中数十万条记录的数据。 数据存储的选择是Elastic Search。 问题: 我们在Elasticsearch中是否有用于 S
我已经安装了 hadoop 3 版本的 GCS 连接器,并将以下配置添加到 core-site.xml,如 Install.md 中所述.目的是将数据从本地集群中的 hdfs 迁移到云存储。 核心站点
如何删除 debezium 连接器。我正在关注本教程 https://debezium.io/documentation/reference/tutorial.html我看到了注册连接器的方法,但不知
如何删除 debezium 连接器。我正在关注本教程 https://debezium.io/documentation/reference/tutorial.html我看到了注册连接器的方法,但不知
我是一名优秀的程序员,十分优秀!