gpt4 book ai didi

c++ - 双重释放或损坏错误 - valgrind

转载 作者:行者123 更新时间:2023-11-28 07:48:29 25 4
gpt4 key购买 nike

main 函数尝试多次运行某个类 (Rock) 并出现 double free or corruption glibc 错误时,我的程序退出。

Valgrind 返回:

==18672== Conditional jump or move depends on uninitialised value(s)
==18672== at 0x56F8554: std::ostreambuf_iterator<char, std::char_traits<char> > std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::_M_insert_int<unsigned long>(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, unsigned long) const (in /usr/lib64/libstdc++.so.6.0.17)
==18672== by 0x56F876C: std::num_put<char, std::ostreambuf_iterator<char, std::char_traits<char> > >::do_put(std::ostreambuf_iterator<char, std::char_traits<char> >, std::ios_base&, char, unsigned long) const (in /usr/lib64/libstdc++.so.6.0.17)
==18672== by 0x56FB945: std::ostream& std::ostream::_M_insert<unsigned long>(unsigned long) (in /usr/lib64/libstdc++.so.6.0.17)
==18672== by 0x431515: Rock::saveClustering(std::set<Attribution, std::less<Attribution>, std::allocator<Attribution> >) (rock.cpp:1406)
==18672== by 0x430C66: Rock::startRock() (rock.cpp:1321)
==18672== by 0x45DABC: main (main.cpp:207)
==18672== Uninitialised value was created by a stack allocation
==18672== at 0x42FF39: Rock::getFinalList(std::vector<Rock::BestLabel, std::allocator<Rock::BestLabel> >&) (rock.cpp:1139)
==18672==
==18672==
==18672== HEAP SUMMARY:
==18672== in use at exit: 292 bytes in 11 blocks
==18672== total heap usage: 86,558 allocs, 86,547 frees, 21,133,326 bytes allocated
==18672==
==18672== 292 (52 direct, 240 indirect) bytes in 1 blocks are definitely lost in loss record 11 of 11
==18672== at 0x4C2BCFB: malloc (vg_replace_malloc.c:270)
==18672== by 0x5F7FC94: nss_parse_service_list (in /lib64/libc-2.15.so)
==18672== by 0x5F80173: __nss_database_lookup (in /lib64/libc-2.15.so)
==18672== by 0xC1C15DB: ???
==18672== by 0x5F3592B: getpwuid_r@@GLIBC_2.2.5 (in /lib64/libc-2.15.so)
==18672== by 0x5050638: pqGetpwuid (in /opt/postgres_home/lib/libpq.so.5.3)
==18672== by 0x503C3BD: pqGetHomeDirectory (in /opt/postgres_home/lib/libpq.so.5.3)
==18672== by 0x503CCD4: getPgPassFilename (in /opt/postgres_home/lib/libpq.so.5.3)
==18672== by 0x503F78A: PasswordFromFile (in /opt/postgres_home/lib/libpq.so.5.3)
==18672== by 0x503FAAB: connectOptions2 (in /opt/postgres_home/lib/libpq.so.5.3)
==18672== by 0x503FD77: PQconnectStart (in /opt/postgres_home/lib/libpq.so.5.3)
==18672== by 0x503FDA5: PQconnectdb (in /opt/postgres_home/lib/libpq.so.5.3)
==18672==
==18672== LEAK SUMMARY:
==18672== definitely lost: 52 bytes in 1 blocks
==18672== indirectly lost: 240 bytes in 10 blocks
==18672== possibly lost: 0 bytes in 0 blocks
==18672== still reachable: 0 bytes in 0 blocks
==18672== suppressed: 0 bytes in 0 blocks

我不习惯 Valgring。它是否表示关于 postgres 数据库访问有未释放的未初始化内存?我用来获取数据的方法是:

void Comum::fetchDB ( string sql_statement )
{
string conn_str ( "dbname=" + getDbName() + " user=" + getDbUser() );
pqxx::connection conn (conn_str);
pqxx::work txn (conn); // ex: domain = "voice"
pqxx::result r = txn.exec (sql_statement);

if ( r.size () == 0 ) {
std::cerr << "No records found for '" << domain << "'." << endl;
exit (12);
}

txn.commit ();

for (unsigned int u = 0; u != getNrFields (); ++u) {
vector <string> v;
v.reserve (r.size());
db_fetched.push_back (v);
}

for (unsigned int rownum = 0; rownum != r.size(); ++rownum) {
const pqxx::result::tuple row = r[rownum];

for (unsigned int colnum = 0; colnum != row.size(); ++colnum) {
const pqxx::result::field f = row[colnum];
db_fetched [colnum].push_back( f.c_str() );
}
}
conn.disconnect();
}

关于 Valgrind 输出的第一行 Conditional jump or move depends on unitialized value(s) saveClustering 方法如下:

void Rock::saveClustering (const set<Attribution> result)
{
string rock_dir = "rock";
string parent_dir = "output";
string dir = parent_dir + "/" + rock_dir;

// Create directory.
createDir (parent_dir);
createDir (dir);

// Build filename.
string filename;

map<unsigned int, AttType>::const_iterator citype = att_type.begin();
while (citype != att_type.end()) {
filename += citype->second.getName();
if (++citype != att_type.end()) {
filename += "-";
}
}

filename = parent_dir + "/" + rock_dir + "/" + filename + "-" + currentDateTime() + ".txt";
ofstream myfile(filename.c_str());

if (myfile.is_open()) {
for ( set<Attribution>::const_iterator ci = result.begin();ci != result.end(); ++ci ) {
myfile << ci->id << "\t";

for (vector<unsigned int>::const_iterator enci = ci->encodings.begin(); enci != ci->encodings.end(); ++enci) {
myfile << *enci << "\t";
}

myfile << ci->type << "\t" << ci->assignment << endl; // 1406 LINE
}
myfile << endl;
myfile.close();

} else {
cerr << "It was not possible to save decoder filename " << filename << endl;
cerr << "Press any key <ENTER> to continue.";
}
}

我只是看不出哪里出了问题,无法提供一个小的可行程序。

最佳答案

valgrind 输出中有两个独立的问题。

首先是未初始化内存的使用,它告诉您它是在 rock.cpp1139

的堆栈上创建的
==18672==    at 0x42FF39: Rock::getFinalList(std::vector<Rock::BestLabel, std::allocator<Rock::BestLabel> >&) (rock.cpp:1139)

查看该行并初始化变量。

第二个问题是内存泄漏,显然是来自 Postgres API。这几乎肯定不是导致崩溃的原因。

这些看起来都不像是双重释放错误的原因,这很可能是由于您的类中没有实现正确的复制构造函数或复制赋值运算符造成的。

关于c++ - 双重释放或损坏错误 - valgrind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14349000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com