Skip to content

Commit

Permalink
ScrollView: Fix memory leak and modernize code
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <[email protected]>
  • Loading branch information
stweil committed Nov 14, 2021
1 parent 371ee22 commit 37b3374
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/viewer/scrollview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <cstdarg>
#include <cstring>
#include <map>
#include <memory> // for std::unique_ptr
#include <mutex> // for std::mutex
#include <string>
#include <thread> // for std::thread
Expand Down Expand Up @@ -97,7 +98,7 @@ void ScrollView::MessageReceiver() {
// the events accordingly.
while (true) {
// The new event we create.
auto *cur = new SVEvent;
std::unique_ptr<SVEvent> cur(new SVEvent);
// The ID of the corresponding window.
int window_id;

Expand Down Expand Up @@ -143,11 +144,12 @@ void ScrollView::MessageReceiver() {

// In case of an SVET_EXIT event, quit the whole application.
if (ev_type == SVET_EXIT) {
ScrollView::Exit();
SendRawMessage("svmain:exit()");
break;
}

// Place two copies of it in the table for the window.
cur->window->SetEvent(cur);
cur->window->SetEvent(cur.get());

// Check if any of the threads currently waiting want it.
std::pair<ScrollView *, SVEventType> awaiting_list(cur->window, cur->type);
Expand All @@ -156,17 +158,14 @@ void ScrollView::MessageReceiver() {
SVET_ANY);
waiting_for_events_mu->lock();
if (waiting_for_events.count(awaiting_list) > 0) {
waiting_for_events[awaiting_list].second = cur;
waiting_for_events[awaiting_list].second = cur.get();
waiting_for_events[awaiting_list].first->Signal();
} else if (waiting_for_events.count(awaiting_list_any) > 0) {
waiting_for_events[awaiting_list_any].second = cur;
waiting_for_events[awaiting_list_any].second = cur.get();
waiting_for_events[awaiting_list_any].first->Signal();
} else if (waiting_for_events.count(awaiting_list_any_window) > 0) {
waiting_for_events[awaiting_list_any_window].second = cur;
waiting_for_events[awaiting_list_any_window].second = cur.get();
waiting_for_events[awaiting_list_any_window].first->Signal();
} else {
// No one wanted it, so delete it.
delete cur;
}
waiting_for_events_mu->unlock();
// Signal the corresponding semaphore twice (for both copies).
Expand All @@ -175,8 +174,6 @@ void ScrollView::MessageReceiver() {
sv->Signal();
sv->Signal();
}
} else {
delete cur; // Applied to no window.
}
svmap_mu->unlock();

Expand Down

2 comments on commit 37b3374

@amitdo
Copy link
Collaborator

@amitdo amitdo commented on 37b3374 Nov 14, 2021

Choose a reason for hiding this comment

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

@stweil
Copy link
Member Author

@stweil stweil commented on 37b3374 Nov 14, 2021

Choose a reason for hiding this comment

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

clang-tidy with modernize-make-unique has no effect, but I now used clang-tidy for some performance improvements (see commit d8d63fd).

Please sign in to comment.