Commit 835ea5bc authored by lilp's avatar lilp
Browse files

fix: 优化内存泄漏

Showing with 27 additions and 3 deletions
+27 -3
......@@ -50,12 +50,26 @@ void TransactionManager::commit(Transaction *txn, LogManager *log_manager) {
// 1. 写入提交事务日志并设置事务的prev_lsn
auto *commit_log_ = new BACLogRecord(txn->get_transaction_id(), txn->get_prev_lsn(), LogType::commit);
txn->set_prev_lsn(log_manager->flush_log_to_disk(commit_log_));
delete commit_log_;
// 2. 释放事务持有的所有锁
for (auto const &lock: *(txn->get_lock_set())) {
lock_manager_->unlock(txn, lock);
}
auto table_write_set = txn->get_write_set();
// 释放table_write_rcd的rcd占有的内存
for (const auto &table_write_rcd: *table_write_set) {
delete table_write_rcd;
}
table_write_set->clear();
auto index_write_set = txn->get_index_write_set();
// 释放index_write_rcd的rcd占有的内存
for (const auto &index_write_rcd: *index_write_set) {
delete index_write_rcd;
}
index_write_set->clear();
delete commit_log_;
// 3. 更新事务状态为已提交
txn->set_state(TransactionState::COMMITTED);
......@@ -94,6 +108,11 @@ void TransactionManager::abort(Transaction *txn, LogManager *log_manager) {
// 避免内存泄露
delete write_rcd;
}
// 释放table_write_rcd的rcd占有的内存
for (const auto &table_write_rcd: *table_write_set) {
delete table_write_rcd;
}
table_write_set->clear();
auto index_write_set = txn->get_index_write_set();
......@@ -107,7 +126,7 @@ void TransactionManager::abort(Transaction *txn, LogManager *log_manager) {
case WType::INSERT_TUPLE: {
for (auto &index: indexes) {
auto ih = sm_manager_->ihs_.at(
sm_manager_->get_ix_manager()->get_index_name(table_name, index.cols)).get();
sm_manager_->get_ix_manager()->get_index_name(table_name, index.cols)).get();
ih->delete_entry(index_write_rcd->GetKey(), txn);
}
break;
......@@ -115,7 +134,7 @@ void TransactionManager::abort(Transaction *txn, LogManager *log_manager) {
case WType::DELETE_TUPLE: {
for (auto &index: indexes) {
auto ih = sm_manager_->ihs_.at(
sm_manager_->get_ix_manager()->get_index_name(table_name, index.cols)).get();
sm_manager_->get_ix_manager()->get_index_name(table_name, index.cols)).get();
ih->insert_entry(index_write_rcd->GetKey(), index_write_rcd->GetRid(), txn);
}
break;
......@@ -130,6 +149,11 @@ void TransactionManager::abort(Transaction *txn, LogManager *log_manager) {
// 避免内存泄露
delete index_write_rcd;
}
// 释放index_write_rcd的rcd占有的内存
for (const auto &index_write_rcd: *index_write_set) {
delete index_write_rcd;
}
index_write_set->clear();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment