请系好安全带。
很奇怪,我在网上找不到关于这种错误的任何信息,但这让我发疯了。希望你们能对这个问题有所启发。
我使用MySQL++从表中获取一些基本数据。它连接到数据库很好,查询似乎工作,但运行mysql:: query::store()导致malloc错误。
mysqlpp::Connection conn(false);
if(conn.connect("demo", "127.0.0.1", "root", "")) // works
{
std::string sql = "SELECT * FROM `items`";
mysqlpp::Query query = conn.query(sql); // works
mysqlpp::StoreQueryResult res = query.store(); // fails
if(res)
{
mysqlpp::StoreQueryResult::const_iterator it;
for(it = res.begin(); it != res.end(); ++it)
{
mysqlpp::Row row = *it;
// Do some things
}
}
else
{
std::cerr<<"Failed to get item list: "<<query.error()<<std::endl;
return false;
}
}
else
{
std::cerr<<"DB connection failed: "<<conn.error()<<std::endl;
return false;
}
gdb backtrace给了我
(gdb) backtrace
#0 0x00007fff841ed499 in malloc_error_break ()
#1 0x00007fff84117183 in free ()
#2 0x000000010029d66c in mysqlpp::Field::~Field ()
#3 0x0000000100493e4d in mysqlpp::ResultBase::ResultBase (this=0x1004805c8, res=0x100480660, dbd=0x100480660, te=122) at result.cpp:40
#4 0x0000000100494690 in mysqlpp::StoreQueryResult::StoreQueryResult (this=0x100480730, res=0x100303e30, dbd=0x100802600) at result.cpp:103
#5 0x0000000100491242 in mysqlpp::Query::store (this=0x3, str=0x100303da0 "SELECT * FROM `items`", len=4298128944) at query.cpp:534
#6 0x00000001004916dc in mysqlpp::Query::store (this=0x3, s=@0x100480848) at query.cpp:508
#7 0x00000001004917c3 in mysqlpp::Query::store (this=0x3) at query.cpp:483
#8 0x0000000100297464 in Load ()
....
Load()是正在运行的函数。
如果我执行了两次查询(我这样做是出于好奇),
mysqlpp::Query query = conn.query(sql);
query = conn.query(sql);
mysqlpp::StoreQueryResult res = query.store();
我没有得到malloc错误,但我得到一个SQL错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM `items`' at line 1
我的g++版本是
g++ (MacPorts gcc47 4.7.3_0) 4.7.3
任何想法?我以前使用过MySQL++,我从来没有遇到过任何问题。
而且,这个Load()序列恰好位于动态链接的库中。(我有一个加载/卸载系统。)如果我注释掉MySQL部分,编译并加载库,一切都没问题。如果我取消注释,重新编译并重新加载库(主程序仍在运行),查询将成功运行!! wtf
任何帮助都是不可思议的。谢谢! !
同样的问题。我的解决方案,检查是否行:
mysqlpp::StoreQueryResult res = query.store(); // fails
if(res.num_rows()){
...
}