gpt4 book ai didi

c++ - free() :invalid size, 但没有调用 new() 或 free() - 使用 C++ 的 SQLite3

转载 作者:行者123 更新时间:2023-12-02 10:30:12 26 4
gpt4 key购买 nike

嗨,我正在使用 C++ 玩 sqlite3。我将数组存储为 BLOB 并尝试从数据库中提取它。但是,当我从数据库中选择数据时,在函数 selectTable() 完成后和 main() 完成之前出现 free():invalid size 错误。所以我有点困惑。
命令行的输出是


selectTable func
Opened database successfully
before finalzie
after close DB
free(): invalid size
Aborted

下面是来自 gdb 的回溯和代码
Program received signal SIGABRT, Aborted.
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1 0x00007ffff717a801 in __GI_abort () at abort.c:79
#2 0x00007ffff71c3897 in __libc_message (action=action@entry=do_abort,
fmt=fmt@entry=0x7ffff72f0b9a "%s\n") at ../sysdeps/posix/libc_fatal.c:181
#3 0x00007ffff71ca90a in malloc_printerr (
str=str@entry=0x7ffff72eeda0 "free(): invalid size") at malloc.c:5350
#4 0x00007ffff71d1e2c in _int_free (have_lock=0,
p=0x7ffff7de5990 <_dl_init+864>, av=0x7ffff7525c40 <main_arena>)
at malloc.c:4161
#5 __GI___libc_free (mem=0x7ffff7de59a0 <_dl_fini>) at malloc.c:3124
#6 0x0000555555555b48 in main ()

int main() {
const char* dir = "testBLOB.db";
sqlite3* DB;
//createDB(dir);
//createTable(dir);
//clearTable(dir);
for(int i = 0; i < 50; i++) {
//insertTable(dir);
}
cout << "selectTable func" << endl;
selectTable(dir);

cout << "Out of selectTable func" << endl;
return 0;
}

static int createDB(const char* s) {
sqlite3* DB;
int exit = 0;

exit = sqlite3_open(s, &DB);

if(!exit) {
//cout << "opened db" << endl;
}
sqlite3_close(DB);

return 0;
}

static int createTable(const char* s) {
sqlite3* DB;
string sql = "CREATE TABLE IF NOT EXISTS test("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name TEXT NOT NULL, "
"vector BLOB"
");";
try {
int exit = 0;
exit = sqlite3_open(s, &DB);
char* messageError;
exit = sqlite3_exec(DB, sql.c_str(), NULL, 0, &messageError);
//cout << "Hello, World!" << endl;
if(exit != SQLITE_OK) {
cerr << "Error creating table" << endl;
} else {
sqlite3_close(DB);
}
} catch (const exception & e) {
cerr << e.what();
}

return 0;
};


static int insertTable(const char* s) {
sqlite3* DB;
long double nums [100] = {63.3448897849,56.5751700134,75.6611442415,44.4839808971,12.1012110357,39.8311006389,55.4628689524,44.1856083913,31.8941372232,36.7439371495,64.1919152947,46.6041307466,16.8129922477,26.4797506344,33.0942127905,87.7671523992,76.4743660304,26.895728291,71.6418628151,39.4279836297,33.1511476055,21.9412133656,49.2985876344,54.7288568945,61.7644413811,39.5360580159,48.2049240243,15.0657890486,74.8453755862,3.9697162327,46.3309272981,3.6967671205,29.4109279366,35.2237159178,71.2944696538,83.2668276235,8.6042287278,29.018178646,53.9090162771,74.1875874704,13.362141479,85.534882156,46.1570578637,16.6535624939,60.4663891357,46.4002226626,43.5334234645,5.9192657242,6.4233909782,70.9674985846,16.7364288218,6.140383666,1.6295357671,41.5132888558,7.0105695897,93.7402324433,67.9067125381,79.2202639865,73.9691802822,83.7211616527,95.1748897322,77.6210081594,52.6691899363,45.9668407426,30.0850082804,99.8853106577,44.4116175004,66.7492926269,80.344211132,2.9125625332,13.1282558744,77.1323341686,31.6479987137,12.0439293147,52.34792652,10.0973593886,11.1281788415,42.3558223361,26.0832770714,78.8802218674,65.7943757958,39.4931075012,62.3669620437,40.5895497709,75.5671730948,32.0576337297,97.9215110968,9.9871548843,46.6131220896,11.0937537323,59.9746432863,25.2756546914,39.1604177152,21.5325516688,55.5211448299,29.0247732257,10.4586600007,53.0534949121,77.3997550972,48.2887426956};

int rc = sqlite3_open_v2(s, &DB, SQLITE_OPEN_READWRITE,NULL);
if(rc != SQLITE_OK) {

} else {
string sql = "INSERT INTO test (name, vector) VALUES ('test', ?)";
try {
sqlite3_stmt *stmt = NULL;
rc = sqlite3_prepare_v2(DB, sql.c_str(), -1, &stmt, NULL);

if(rc != SQLITE_OK) {
cerr << "Error preparing stmt" << endl;
} else {
rc = sqlite3_bind_blob(stmt,1, nums, 1600, SQLITE_STATIC);
if(rc != SQLITE_OK) {
cerr << "bind failed" << endl;
} else {
rc = sqlite3_step(stmt);
if(rc != SQLITE_DONE) {
cerr << "no" << endl;
}
}

}
sqlite3_finalize(stmt);
} catch (const exception & e) {
cerr << e.what();
}
}
sqlite3_close(DB);
return 0;
};
static string selectTable(const char* s) {
sqlite3 *db;
int rc;

/* Open database */
rc = sqlite3_open(s, &db);

if( rc ) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
} else {
fprintf(stderr, "Opened database successfully\n");
}

sqlite3_stmt *stmt;
/* Create SQL statement */
const char* sql = "SELECT * FROM test";
if (sqlite3_prepare_v2(db, sql, -1, &stmt, 0) != SQLITE_OK)
{
return 0;
}


int result = 0;
while (true)
{
result = sqlite3_step(stmt);
if (result == SQLITE_ROW)
{

// Get the size of the vector
int size = sqlite3_column_bytes(stmt, 2);
// Get the pointer to data
long double * p = (long double *)sqlite3_column_blob(stmt,2);
// Initialize the vector with the data
vector<long double> data(p, p+size);
allVects.push_back(data);
}
else
{
//cout << "not SQLITE_ROW" << endl;
break;
}
}

cout<< "before finalzie" << endl;
sqlite3_finalize(stmt);

while(sqlite3_close(db)!= SQLITE_OK) {
cout << "not ok" << endl;
}

cout<< "after close db" << endl;

}

最佳答案

我自己想通了。这是因为我的函数 selectTable() 的返回类型是字符串,但是在函数内部它不返回字符串。奇怪的是我的编译器没有提示它......

关于c++ - free() :invalid size, 但没有调用 new() 或 free() - 使用 C++ 的 SQLite3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62546269/

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