![](/img/trans.png)
[英]Destructor to call free() to free dynamically allocated memory using malloc
[英]Not able to free memeory allocated by Malloc in
我有下面这段编写的代码,该代码在调用时从内部数据库中提取值。 数据库第三列包含BLOB格式的值。 因此,对该数据库的调用是提取开始时间和结束时间之间的所有列。 我正在使用malloc
unsigned char *valueBlobData = (unsigned char*)malloc(valueBlobSize);
为值bcoz保留内存一次可以有多个值。 现在,如果我使用:
free(valueBlobData)
列表historyRows
不包含任何值。 但是,如果我没有空闲内存,那么我会在stl列表中收到值。 但这会导致内存泄漏。
问题是,我在错误的位置释放了内存。 如果是,我如何使它工作?
bool SQHISDB::getHisVArray(unsigned long startTime, unsigned long endTime,
std::list<tHistoryRow> &historyRows)
{
int rc;
int counter = 0;
tHistoryRow tempHistoryRow; // Struct having four variables
unsigned int A1 =1, A2=2;
const char *zSql = "SELECT * "
"FROM node_values "
"WHERE A1 = ? "
"AND A2 = ? "
"AND (sourceTS BETWEEN ? AND ? ) "
";";
sqlite3_stmt *pStmt = NULL;
rc = sqlite3_prepare_v2(this->db, zSql, std::strlen(zSql), &pStmt, NULL);
if (rc != SQLITE_OK)
{
return rc;
}
sqlite3_bind_int(pStmt, 1, A1);
sqlite3_bind_int(pStmt, 2, A2);
sqlite3_bind_int64(pStmt, 3, startTime);
sqlite3_bind_int64(pStmt, 4, endTime;
while (sqlite3_step(pStmt) == SQLITE_ROW)
{
counter++;
unsigned long sourceTimestamp = sqlite3_column_int64(pStmt, 4);
unsigned int valueBlobSize = sqlite3_column_bytes(pStmt, 3);
unsigned char *valueBlobData = (unsigned char*)malloc(valueBlobSize);
std::memcpy(valueBlobData, sqlite3_column_blob(pStmt, SQLITE_HISTORYDB_INDEX_VALUE), valueBlobSize);
tempHistoryRow.sourceTimestamp = sourceTS;
tempHistoryRow.blobSize = valueBlobSize;
tempHistoryRow.blob = valueBlobData;
historyRows.push_back(tempHistoryRow);
free(valueBlobData);
}
sqlite3_finalize(pStmt);
return true;
}
实际上,您正在尝试将historyRows
悬挂指针悬空。 这绝对是错误的。
对我来说,您似乎想使用简单的向量来管理此缓冲区。
struct tHistoryRow {
std::vector<unsigned char> blob;
… … …
};
bool SQHISDB::getHisVArray(unsigned long startTime, unsigned long endTime,
std::list<tHistoryRow> &historyRows)
{
int rc;
int counter = 0;
unsigned int A1 =1, A2=2;
static const std::string zSql{ "SELECT * "
"FROM node_values "
"WHERE A1 = ? "
"AND A2 = ? "
"AND (sourceTS BETWEEN ? AND ? ) "
";" };
sqlite3_stmt *pStmt = NULL;
rc = sqlite3_prepare_v2(this->db, zSql.data(), zSql.length(), &pStmt, NULL);
if (rc != SQLITE_OK)
{
return rc;
}
sqlite3_bind_int(pStmt, 1, A1);
sqlite3_bind_int(pStmt, 2, A2);
sqlite3_bind_int64(pStmt, 3, startTime);
sqlite3_bind_int64(pStmt, 4, endTime;
while (sqlite3_step(pStmt) == SQLITE_ROW)
{
counter++;
unsigned long sourceTimestamp = sqlite3_column_int64(pStmt, 4);
unsigned int valueBlobSize = sqlite3_column_bytes(pStmt, 3);
tHistoryRow tempHistoryRow; // it would be nice to provide constructor
tempHistoryRow.blob.resize(valueBlobSize);
auto pBlob = sqlite3_column_blob(pStmt, SQLITE_HISTORYDB_INDEX_VALUE);
std::copy(pBlob, pBlob + valueBlobSize, tempHistoryRow.blob.begin());
tempHistoryRow.sourceTimestamp = sourceTS;
historyRows.push_back(std::move(tempHistoryRow));
}
sqlite3_finalize(pStmt);
return true;
}
释放内存并不意味着“ 清除 ”内存,当您释放内存时,它仅可用于其他分配,但是内存可能仍包含与调用free之前相同的内容( 或某些损坏的版本,这取决于方式)。分配器已实现 )。
尝试检查( 取消引用 )已传递给free()
的指针是未定义的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.