Skip to content

Commit 95ff2be

Browse files
committed
Implemented explicit task memory ownership management in Monitor_Poll..
1 parent 50fae0b commit 95ff2be

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

lib/MySQL_Monitor.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ConsumerThread : public Thread {
119119
// available to process.
120120
for (int i = 0; (thrn ? i < thrn : 1); i++) {
121121
//VALGRIND_DISABLE_ERROR_REPORTING;
122-
WorkItem<T>* item = m_queue.remove();
122+
WorkItem<T>* item = static_cast<WorkItem<T>*>(m_queue.remove());
123123
//VALGRIND_ENABLE_ERROR_REPORTING;
124124
if (item == NULL) {
125125
if (thrn) {
@@ -5182,7 +5182,7 @@ void * MySQL_Monitor::run() {
51825182

51835183
__monitor_run:
51845184
while (queue->size()) { // this is a clean up in case Monitor was restarted
5185-
WorkItem<MySQL_Monitor_State_Data>* item = queue->remove();
5185+
WorkItem<MySQL_Monitor_State_Data>* item = static_cast<WorkItem<MySQL_Monitor_State_Data>*>(queue->remove());
51865186
if (item) {
51875187
for (auto ptr : item->data)
51885188
delete ptr;
@@ -7056,14 +7056,18 @@ class Monitor_Poll {
70567056
MySQL_Monitor* mysql_monitor_;
70577057
};
70587058

7059-
Monitor_Poll(unsigned int capacity) {
7059+
Monitor_Poll(unsigned int capacity, bool owns_task_memory = false) {
70607060
len_ = 0;
7061+
owns_task_memory_ = owns_task_memory; // if true, this object takes ownership of task memory and will delete unprocessed tasks on destruction
70617062
capacity_ = capacity;
70627063
fds_ = (struct pollfd*)malloc(capacity_ * sizeof(struct pollfd));
70637064
mmsds_ = (MySQL_Monitor_State_Data**)malloc(capacity_ * sizeof(MySQL_Monitor_State_Data*));
70647065
}
70657066

70667067
~Monitor_Poll() {
7068+
if (owns_task_memory_) {
7069+
cleanup_unprocessed_tasks(); // free remaining unprocessed tasks
7070+
}
70677071
free(fds_);
70687072
free(mmsds_);
70697073
}
@@ -7242,10 +7246,22 @@ class Monitor_Poll {
72427246
return i ? i : n;
72437247
}
72447248

7249+
// Deletes any task objects that were not processed
7250+
inline
7251+
void cleanup_unprocessed_tasks() {
7252+
if (len_ == 0) return;
7253+
for (unsigned int i = 0; i < len_; ++i) {
7254+
delete mmsds_[i];
7255+
mmsds_[i] = nullptr;
7256+
}
7257+
len_ = 0;
7258+
}
7259+
72457260
unsigned int len_;
72467261
unsigned int capacity_;
72477262
struct pollfd* fds_;
72487263
MySQL_Monitor_State_Data** mmsds_;
7264+
bool owns_task_memory_;
72497265
};
72507266

72517267
MySQL_Monitor_State_Data_Task_Result MySQL_Monitor_State_Data::task_handler(short event_, short& wait_event) {
@@ -7452,7 +7468,7 @@ bool MySQL_Monitor::monitor_ping_process_ready_tasks(const std::vector<MySQL_Mon
74527468
void MySQL_Monitor::monitor_ping_async(SQLite3_result* resultset) {
74537469
assert(resultset);
74547470

7455-
Monitor_Poll monitor_poll(resultset->rows_count);
7471+
Monitor_Poll monitor_poll(resultset->rows_count, true);
74567472

74577473
for (std::vector<SQLite3_row*>::iterator it = resultset->rows.begin(); it != resultset->rows.end(); ++it) {
74587474
const SQLite3_row* r = *it;

0 commit comments

Comments
 (0)