Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions include/MySQL_Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,19 @@ class MySQL_Monitor_State_Data {
template<typename T>
class WorkItem {
public:
T *data;
void *(*routine) (void *);
WorkItem(T*_data, void *(*start_routine) (void *)) {
data=_data;
routine=start_routine;
}
~WorkItem() {}
std::vector<T*> data;
using entry_point = void *(*)(const std::vector<T*>& data);
entry_point start_routine;
WorkItem(T*_data, entry_point _start_routine) {
data.push_back(_data);
start_routine = _start_routine;
}
WorkItem(std::vector<T*>&& _data, entry_point _start_routine)
: data(std::move(_data)), start_routine(_start_routine) {}
WorkItem(const std::vector<T*>& _data, entry_point _start_routine)
: data(_data), start_routine(_start_routine) {
}
~WorkItem() = default;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using = default for the destructor is a good modern C++ practice. However, the current design requires the user of WorkItem (like ConsumerThread) to manually delete the pointers stored in the data vector. This separates resource acquisition (new WorkItem(...)) from resource release (delete ptr; ... delete item;), which can be error-prone.

To improve encapsulation and adhere more closely to RAII principles, consider making WorkItem responsible for the memory it conceptually owns. You could implement a custom destructor to delete the pointers.

~WorkItem() {
    for (auto ptr : data) {
        delete ptr;
    }
}

};

struct p_mon_counter {
Expand Down
Loading