-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix memory leak in RowTuple::set_schema #457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+3
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hnwyllmm
approved these changes
Oct 11, 2024
Member
|
Applied. Thanks. |
Willaaaaaaa
pushed a commit
to Willaaaaaaa/miniob
that referenced
this pull request
May 22, 2025
### What problem were solved in this pull request?
**Problem:**
When I attempted to join multiple tables, `libsanitizer` detected a
memory leak. The error log is as follows:
```
==23682==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 768 byte(s) in 12 object(s) allocated from:
#0 0x75f738946a2a in operator new(unsigned long) /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_new_delete.cpp:95
oceanbase#1 0x56870207d56f in RowTuple::set_schema(Table const*, std::vector<FieldMeta, std::allocator<FieldMeta> > const*) /home/pepefloyd/git/miniob/src/observer/sql/expr/tuple.h:184
oceanbase#2 0x56870208f737 in TableScanPhysicalOperator::open(Trx*) /home/pepefloyd/git/miniob/src/observer/sql/operator/table_scan_physical_operator.cpp:25
oceanbase#3 0x568702081888 in NestedLoopJoinPhysicalOperator::right_next() /home/pepefloyd/git/miniob/src/observer/sql/operator/join_physical_operator.cpp:113
oceanbase#4 0x568702083b42 in PredicatePhysicalOperator::next() /home/pepefloyd/git/miniob/src/observer/sql/operator/predicate_physical_operator.cpp:41
oceanbase#5 0x56870206f411 in HashGroupByPhysicalOperator::open(Trx*) /home/pepefloyd/git/miniob/src/observer/sql/operator/hash_group_by_physical_operator.cpp:44
oceanbase#6 0x568702085740 in ProjectPhysicalOperator::open(Trx*) /home/pepefloyd/git/miniob/src/observer/sql/operator/project_physical_operator.cpp:34
oceanbase#7 0x568702038e2c in PlainCommunicator::write_result_internal(SessionEvent*, bool&) /home/pepefloyd/git/miniob/src/observer/net/plain_communicator.cpp:193
oceanbase#8 0x56870203a7cc in PlainCommunicator::write_result(SessionEvent*, bool&) /home/pepefloyd/git/miniob/src/observer/net/plain_communicator.cpp:162
oceanbase#9 0x568701ee5e4b in SqlTaskHandler::handle_event(Communicator*) /home/pepefloyd/git/miniob/src/observer/net/sql_task_handler.cpp:45
oceanbase#10 0x56870202f0d7 in Worker::operator()() /home/pepefloyd/git/miniob/src/observer/net/one_thread_per_connection_thread_handler.cpp:96
oceanbase#11 0x75f7384f78b3 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:104
SUMMARY: AddressSanitizer: 768 byte(s) leaked in 12 allocation(s).
```
### What is changed and how it works?
To fix the memory leak, I identified that in the function
`Tuple::set_schema`:
```cpp
void set_schema(const Table *table, const std::vector<FieldMeta> *fields)
{
table_ = table;
// fix: During a join, the right table's open is called multiple times,
// which invokes set_schema repeatedly. This causes Tuple to store
// unnecessary fields and values, so it is necessary to clear the existing ones first.
this->speces_.clear();
this->speces_.reserve(fields->size());
for (const FieldMeta &field : *fields) {
speces_.push_back(new FieldExpr(table, &field));
}
}
```
The call to `this->speces_.clear()` does not destruct the existing
`FieldExpr*` objects. As a result, the memory was not released properly.
The fix I applied is to manually delete the `FieldExpr*` before clearing
the vector, as shown below:
```cpp
for (FieldExpr *spec : speces_) {
delete spec;
}
```
This ensures that memory allocated for `FieldExpr` is properly released,
thus fixing the memory leak.
jiahaonb
pushed a commit
to jiahaonb/miniob
that referenced
this pull request
Jun 3, 2025
### What problem were solved in this pull request?
**Problem:**
When I attempted to join multiple tables, `libsanitizer` detected a
memory leak. The error log is as follows:
```
==23682==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 768 byte(s) in 12 object(s) allocated from:
#0 0x75f738946a2a in operator new(unsigned long) /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_new_delete.cpp:95
#1 0x56870207d56f in RowTuple::set_schema(Table const*, std::vector<FieldMeta, std::allocator<FieldMeta> > const*) /home/pepefloyd/git/miniob/src/observer/sql/expr/tuple.h:184
oceanbase#2 0x56870208f737 in TableScanPhysicalOperator::open(Trx*) /home/pepefloyd/git/miniob/src/observer/sql/operator/table_scan_physical_operator.cpp:25
oceanbase#3 0x568702081888 in NestedLoopJoinPhysicalOperator::right_next() /home/pepefloyd/git/miniob/src/observer/sql/operator/join_physical_operator.cpp:113
oceanbase#4 0x568702083b42 in PredicatePhysicalOperator::next() /home/pepefloyd/git/miniob/src/observer/sql/operator/predicate_physical_operator.cpp:41
oceanbase#5 0x56870206f411 in HashGroupByPhysicalOperator::open(Trx*) /home/pepefloyd/git/miniob/src/observer/sql/operator/hash_group_by_physical_operator.cpp:44
oceanbase#6 0x568702085740 in ProjectPhysicalOperator::open(Trx*) /home/pepefloyd/git/miniob/src/observer/sql/operator/project_physical_operator.cpp:34
oceanbase#7 0x568702038e2c in PlainCommunicator::write_result_internal(SessionEvent*, bool&) /home/pepefloyd/git/miniob/src/observer/net/plain_communicator.cpp:193
oceanbase#8 0x56870203a7cc in PlainCommunicator::write_result(SessionEvent*, bool&) /home/pepefloyd/git/miniob/src/observer/net/plain_communicator.cpp:162
oceanbase#9 0x568701ee5e4b in SqlTaskHandler::handle_event(Communicator*) /home/pepefloyd/git/miniob/src/observer/net/sql_task_handler.cpp:45
oceanbase#10 0x56870202f0d7 in Worker::operator()() /home/pepefloyd/git/miniob/src/observer/net/one_thread_per_connection_thread_handler.cpp:96
oceanbase#11 0x75f7384f78b3 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:104
SUMMARY: AddressSanitizer: 768 byte(s) leaked in 12 allocation(s).
```
### What is changed and how it works?
To fix the memory leak, I identified that in the function
`Tuple::set_schema`:
```cpp
void set_schema(const Table *table, const std::vector<FieldMeta> *fields)
{
table_ = table;
// fix: During a join, the right table's open is called multiple times,
// which invokes set_schema repeatedly. This causes Tuple to store
// unnecessary fields and values, so it is necessary to clear the existing ones first.
this->speces_.clear();
this->speces_.reserve(fields->size());
for (const FieldMeta &field : *fields) {
speces_.push_back(new FieldExpr(table, &field));
}
}
```
The call to `this->speces_.clear()` does not destruct the existing
`FieldExpr*` objects. As a result, the memory was not released properly.
The fix I applied is to manually delete the `FieldExpr*` before clearing
the vector, as shown below:
```cpp
for (FieldExpr *spec : speces_) {
delete spec;
}
```
This ensures that memory allocated for `FieldExpr` is properly released,
thus fixing the memory leak.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem were solved in this pull request?
Problem:
When I attempted to join multiple tables,
libsanitizerdetected a memory leak. The error log is as follows:What is changed and how it works?
To fix the memory leak, I identified that in the function
Tuple::set_schema:The call to
this->speces_.clear()does not destruct the existingFieldExpr*objects. As a result, the memory was not released properly. The fix I applied is to manually delete theFieldExpr*before clearing the vector, as shown below:This ensures that memory allocated for
FieldExpris properly released, thus fixing the memory leak.