fix memory leaks
Some checks are pending
/ sync-to-origin (push) Waiting to run

This commit is contained in:
PoliEcho 2025-04-14 09:10:56 +02:00
parent 9b8bdbf850
commit f01422eaf5
10 changed files with 151 additions and 89 deletions

View File

@ -6,6 +6,7 @@
#include <algorithm>
#include <codecvt>
#include <csignal>
#include <cstring>
#include <curses.h>
#include <dirent.h>
#include <filesystem>
@ -207,6 +208,11 @@ char *wchar_to_char(const wchar_t *src) {
size_t len = wcslen(src) + 1; // +1 for null terminator
char *dest = new char[len * MB_CUR_MAX];
current_allocated->push_back(allocation{
GENERIC_ARRAY,
dest,
len * MB_CUR_MAX,
});
std::wcstombs(dest, src, len * MB_CUR_MAX);
return dest;

View File

@ -26,7 +26,7 @@ using nlohmann::json;
std::vector<allocation> komens_allocated;
void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &resp_from_api);
const json &resp_from_api);
void komens_print_usage_message() {
attron(COLOR_PAIR(COLOR_BLUE));
@ -39,15 +39,15 @@ void komens_print_usage_message() {
void komens_page(koment_type type) {
current_allocated = &komens_allocated;
json resp_from_api;
{
const json resp_from_api = [&]() -> json {
const char *types[] = {"/api/3/komens/messages/received",
"/api/3/komens/messages/sent",
"/api/3/komens/messages/noticeboard"};
const std::string endpoint = types[type];
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, POST);
}
return bakaapi::get_data_from_endpoint(endpoint, POST);
}();
/* Initialize curses */
setlocale(LC_ALL, "");
@ -63,14 +63,16 @@ void komens_page(koment_type type) {
}
complete_menu komens_choise_menu;
komens_allocated.push_back({COMPLETE_MENU_TYPE, &komens_choise_menu, 1});
size_t num_of_komens = resp_from_api["Messages"].size();
komens_choise_menu.items = new ITEM *[num_of_komens + 1];
komens_allocated.push_back(
{ITEM_ARRAY, komens_choise_menu.items, num_of_komens});
komens_choise_menu.items_size = num_of_komens + 1;
char **title_bufs = new char *[num_of_komens];
komens_allocated.push_back({CHAR_PTR_ARRAY, title_bufs, num_of_komens});
char **name_bufs = new char *[num_of_komens];
komens_allocated.push_back({CHAR_PTR_ARRAY, name_bufs, num_of_komens});
size_t max_item_lenght;
{
size_t max_title_lenght = 0;
@ -78,11 +80,9 @@ void komens_page(koment_type type) {
size_t tmp_lenght;
char tmp_buf[1500];
for (size_t i = 0; i < num_of_komens; i++) {
wcstombs(tmp_buf,
string_to_wstring(
resp_from_api["Messages"][i]["Title"].get<std::string>())
.c_str(),
sizeof(tmp_buf));
strlcpy(tmp_buf,
resp_from_api["Messages"][i]["Title"].get<std::string>().c_str(),
sizeof(tmp_buf));
tmp_lenght =
resp_from_api["Messages"][i]["Title"].get<std::string>().length();
@ -93,12 +93,12 @@ void komens_page(koment_type type) {
title_bufs[i] = new char[strlen(tmp_buf) + 1];
strlcpy(title_bufs[i], tmp_buf, strlen(tmp_buf) + 1);
wcstombs(
tmp_buf,
string_to_wstring(
resp_from_api["Messages"][i]["Sender"]["Name"].get<std::string>())
.c_str(),
sizeof(tmp_buf));
strlcpy(tmp_buf,
resp_from_api["Messages"][i]["Sender"]["Name"]
.get<std::string>()
.c_str(),
sizeof(tmp_buf));
tmp_lenght = resp_from_api["Messages"][i]["Sender"]["Name"]
.get<std::string>()
@ -118,7 +118,6 @@ void komens_page(koment_type type) {
komens_choise_menu.items[num_of_komens] = nullptr;
komens_choise_menu.menu = new_menu(komens_choise_menu.items);
komens_allocated.push_back({MENU_TYPE, komens_choise_menu.menu, 1});
komens_choise_menu.win =
newwin(MAIN_WIN_HIGHT, max_item_lenght + 1, DEFAULT_OFSET, DEFAULT_OFSET);
@ -149,6 +148,7 @@ void komens_page(koment_type type) {
komens_allocated.push_back({WINDOW_TYPE, content_win, 1});
WINDOW *attachment_win = newwin(1, 1, LINES, COLS);
komens_allocated.push_back({WINDOW_TYPE, attachment_win, 1});
insert_content(content_win, attachment_win,
resp_from_api["Messages"][item_index(
@ -216,11 +216,13 @@ void komens_page(koment_type type) {
current_item(komens_choise_menu.menu))]);
wrefresh(komens_choise_menu.win);
}
unpost_menu(komens_choise_menu.menu);
endwin();
delete_all(&komens_allocated);
}
void insert_content(WINDOW *content_win, WINDOW *attachment_win,
json &message) {
const json &message) {
wclear(content_win);
mvwprintw(content_win, 0, 0, "%s",
html_to_string(message.at("Text")).c_str());

View File

@ -1,6 +1,7 @@
#include "komens_menu.h"
#include "helper_funcs.h"
#include "komens.h"
#include "memory.h"
#include "net.h"
#include "types.h"
#include <cstdlib>
@ -8,15 +9,19 @@
#include <curses.h>
#include <menu.h>
std::vector<allocation> komens_menu_allocated;
void komens_menu() {
current_allocated = &komens_menu_allocated;
wchar_t *choices[] = {
L"received", L"sent", L"noticeboard", L"Exit", nullptr,
};
complete_menu komens_menu;
komens_menu_allocated.push_back({COMPLETE_MENU_TYPE, &komens_menu, 1});
int c;
int n_choices, i;
int n_choices;
/* Initialize curses */
setlocale(LC_ALL, "");
@ -30,8 +35,11 @@ void komens_menu() {
/* Create items */
n_choices = ARRAY_SIZE(choices);
komens_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for (i = 0; i < n_choices; ++i)
komens_menu.items = new ITEM *[ARRAY_SIZE(choices)];
komens_menu.items_size = ARRAY_SIZE(choices);
for (int i = 0; i < n_choices; ++i)
komens_menu.items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
@ -89,6 +97,7 @@ void komens_menu() {
}
komens_page(
static_cast<koment_type>(item_index(current_item(komens_menu.menu))));
current_allocated = &komens_menu_allocated;
pos_menu_cursor(komens_menu.menu);
refresh();
wrefresh(komens_menu.win);
@ -100,8 +109,6 @@ close_menu:
/* Unpost and free all the memory taken up */
unpost_menu(komens_menu.menu);
free_menu(komens_menu.menu);
for (i = 0; i < n_choices; ++i)
free_item(komens_menu.items[i]);
delete_all(&komens_menu_allocated);
endwin();
}

View File

@ -5,6 +5,7 @@
#include "net.h"
#include "timetable.h"
#include "types.h"
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <curses.h>
@ -22,9 +23,7 @@ void main_menu() {
nullptr, nullptr, nullptr, nullptr};
complete_menu main_menu;
int c;
int n_choices;
main_menu_allocated.push_back({COMPLETE_MENU_TYPE, &main_menu, 1});
/* Initialize curses */
setlocale(LC_ALL, "");
@ -37,14 +36,15 @@ void main_menu() {
init_pair(2, COLOR_CYAN, COLOR_BLACK);
/* Create items */
n_choices = ARRAY_SIZE(choices);
main_menu.items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
for (int i = 0; i < n_choices; ++i)
main_menu.items = new ITEM *[ARRAY_SIZE(choices)];
main_menu.items_size = ARRAY_SIZE(choices);
for (size_t i = 0; i < ARRAY_SIZE(choices); ++i) {
main_menu.items[i] =
new_item(wchar_to_char(choices[i]), wchar_to_char(choices[i]));
}
/* Crate menu */
main_menu.menu = new_menu((ITEM **)main_menu.items);
main_menu.menu = new_menu(main_menu.items);
/* Create the window to be associated with the menu */
main_menu.win = newwin(12, 40, 4, 4);
@ -77,6 +77,7 @@ void main_menu() {
attroff(COLOR_PAIR(2));
refresh();
int c;
while ((c = getch()) != KEY_F(1)) {
switch (c) {
case KEY_DOWN:
@ -92,7 +93,7 @@ void main_menu() {
break;
case 10: // ENTER
clear();
if (item_index(current_item(main_menu.menu)) == n_choices - 1) {
if (item_index(current_item(main_menu.menu)) == ARRAY_SIZE(choices) - 1) {
goto close_menu;
}
choicesFuncs[item_index(current_item(main_menu.menu))]();
@ -108,8 +109,6 @@ close_menu:
/* Unpost and free all the memory taken up */
unpost_menu(main_menu.menu);
free_menu(main_menu.menu);
for (int i = 0; i < n_choices; ++i)
free_item(main_menu.items[i]);
delete_all(&main_menu_allocated);
endwin();
}

View File

@ -31,22 +31,25 @@ using nlohmann::json;
std::vector<allocation> marks_allocated;
void init_wins(WINDOW **wins, int n, json marks_json);
void win_show(WINDOW *win, wchar_t *label, int label_color, int width,
int height, json marks_json, int SubjectIndex);
void init_wins(WINDOW **wins, const int n, const json &marks_json);
void win_show(WINDOW *win, const wchar_t *label, const int label_color,
int width, int height, const json &marks_json,
const int SubjectIndex);
void marks_page() {
current_allocated = &marks_allocated;
json resp_from_api;
{
std::string endpoint = "api/3/marks";
resp_from_api = bakaapi::get_data_from_endpoint(endpoint, GET);
}
size_t size_my_wins = resp_from_api["Subjects"].size();
// thanks to lambda i can make this const
const json resp_from_api = [&]() -> json {
const std::string endpoint = "api/3/marks";
return bakaapi::get_data_from_endpoint(endpoint, GET);
}();
const size_t size_my_wins = resp_from_api["Subjects"].size();
WINDOW **my_wins = new (std::nothrow) WINDOW *[size_my_wins];
marks_allocated.push_back({WINDOW_ARRAY, my_wins, size_my_wins});
size_t size_my_panels = resp_from_api["Subjects"].size();
const size_t size_my_panels = size_my_wins;
PANEL **my_panels = new (std::nothrow) PANEL *[size_my_panels];
marks_allocated.push_back({PANEL_ARRAY, my_panels, size_my_panels});
@ -132,7 +135,7 @@ void marks_page() {
}
/* Put all the windows */
void init_wins(WINDOW **wins, int n, json marks_json) {
void init_wins(WINDOW **wins, const int n, const json &marks_json) {
int x, y, i;
wchar_t label[1500];
@ -145,12 +148,12 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
for (i = 0; i < n; ++i) {
// Calculate label and max_text_length to determine window width
std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"];
std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"];
const std::string sub_name = marks_json["Subjects"][i]["Subject"]["Name"];
const std::string sub_avg_s = marks_json["Subjects"][i]["AverageText"];
// Convert to wchar_t
std::wstring wsub_name = string_to_wstring(sub_name);
std::wstring wsub_avg_s = string_to_wstring(sub_avg_s);
const std::wstring wsub_name = string_to_wstring(sub_name);
const std::wstring wsub_avg_s = string_to_wstring(sub_avg_s);
// Using swprintf for wide character formatting
swprintf(label, sizeof(label) / sizeof(label[0]), L"%ls - avg: %ls",
@ -160,17 +163,18 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
for (unsigned int j = 0; j < static_cast<unsigned int>(
marks_json["Subjects"][i]["Marks"].size());
j++) {
std::string caption =
const std::string caption =
rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Caption"]);
std::string theme =
const std::string theme =
rm_tr_le_whitespace(marks_json["Subjects"][i]["Marks"][j]["Theme"]);
std::wstring wcaption = string_to_wstring(caption);
std::wstring wtheme = string_to_wstring(theme);
const std::wstring wcaption = string_to_wstring(caption);
const std::wstring wtheme = string_to_wstring(theme);
// Some code that does something and fixes some edge cases
std::string testCaption = caption + std::format(" {{{}}} [{}]", "X", 0);
std::wstring wTestCaption = string_to_wstring(testCaption);
const std::string testCaption =
caption + std::format(" {{{}}} [{}]", "X", 0);
const std::wstring wTestCaption = string_to_wstring(testCaption);
max_text_length =
std::max({max_text_length, wTestCaption.length(), wtheme.length()});
}
@ -203,8 +207,9 @@ void init_wins(WINDOW **wins, int n, json marks_json) {
}
/* Show the window with a border and a label */
void win_show(WINDOW *win, wchar_t *label, int label_color, int width,
int height, json marks_json, int SubjectIndex) {
void win_show(WINDOW *win, const wchar_t *label, const int label_color,
int width, int height, const json &marks_json,
const int SubjectIndex) {
// is the compiler smoking weed or something, why is it thinking starty is not
// used ??

View File

@ -1,9 +1,34 @@
#include "memory.h"
#include "color.h"
#include "types.h"
#include <curses.h>
#include <iostream>
#include <menu.h>
#include <ncurses.h>
#include <panel.h>
template <typename T> struct NcursesDeleter {
static void delete_element(T obj);
};
template <> void NcursesDeleter<WINDOW *>::delete_element(WINDOW *win) {
delwin(win);
}
template <> void NcursesDeleter<PANEL *>::delete_element(PANEL *pan) {
del_panel(pan);
}
template <> void NcursesDeleter<ITEM *>::delete_element(ITEM *item) {
free_item(item);
}
template <typename T> void delete_ncurses_arrays(void *ptr, std::size_t size) {
T *array = static_cast<T *>(ptr);
for (std::size_t j = 0; j < size; ++j) {
NcursesDeleter<T>::delete_element(array[j]);
}
delete[] array;
}
void delete_all(std::vector<allocation> *allocated) {
if (allocated == nullptr) {
return;
@ -11,27 +36,26 @@ void delete_all(std::vector<allocation> *allocated) {
for (long long i = allocated->size() - 1; i >= 0; i--) {
switch (allocated->at(i).type) {
case WINDOW_ARRAY: {
WINDOW **windows = static_cast<WINDOW **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; j++) {
delwin(windows[j]);
}
delete[] windows;
delete_ncurses_arrays<WINDOW *>(allocated->at(i).ptr,
allocated->at(i).size);
break;
}
case PANEL_ARRAY: {
PANEL **panels = static_cast<PANEL **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; j++) {
del_panel(panels[j]);
}
delete[] panels;
delete_ncurses_arrays<PANEL *>(allocated->at(i).ptr,
allocated->at(i).size);
break;
}
case ITEM_ARRAY: {
ITEM **items = static_cast<ITEM **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; j++) {
free_item(items[j]);
delete_ncurses_arrays<ITEM *>(allocated->at(i).ptr,
allocated->at(i).size);
break;
}
case CHAR_PTR_ARRAY: {
char **array = static_cast<char **>(allocated->at(i).ptr);
for (std::size_t j = 0; j < allocated->at(i).size; ++j) {
delete[] array[j];
}
delete[] items;
delete[] array;
break;
}
case GENERIC_ARRAY:
@ -46,6 +70,15 @@ void delete_all(std::vector<allocation> *allocated) {
case MENU_TYPE:
free_menu(static_cast<MENU *>(allocated->at(i).ptr));
break;
case COMPLETE_MENU_TYPE: {
free_menu(static_cast<complete_menu *>(allocated->at(i).ptr)->menu);
delwin(static_cast<complete_menu *>(allocated->at(i).ptr)->win);
delete_ncurses_arrays<ITEM *>(
static_cast<complete_menu *>(allocated->at(i).ptr)->items,
static_cast<complete_menu *>(allocated->at(i).ptr)->items_size);
break;
}
case GENERIC_TYPE:
delete static_cast<char *>(allocated->at(i).ptr);
break;

View File

@ -9,10 +9,12 @@ enum AllocationType {
WINDOW_ARRAY,
PANEL_ARRAY,
ITEM_ARRAY,
CHAR_PTR_ARRAY,
GENERIC_ARRAY,
WINDOW_TYPE,
PANEL_TYPE,
MENU_TYPE,
COMPLETE_MENU_TYPE,
GENERIC_TYPE
};

View File

@ -9,6 +9,7 @@
#include <cstdlib>
#include <cstring>
#include <curl/curl.h>
#include <curl/header.h>
#include <curses.h>
#include <dirent.h>
#include <fcntl.h>
@ -111,24 +112,29 @@ std::tuple<std::string, int> send_curl_request(
safe_exit(EINVAL);
}
CURLcode curl_return_code = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (curl_return_code != CURLE_OK) {
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
<< curl_easy_strerror(curl_return_code) << "\n";
safe_exit(21);
}
if (fileStream) {
fileStream->close();
}
int http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
return {response, http_code};
} else {
std::cerr << RED "[ERROR] " << RESET "curl not initialised\n";
safe_exit(20);
}
CURLcode curl_return_code = curl_easy_perform(curl);
if (curl_return_code != CURLE_OK) {
std::cerr << RED "[ERROR] " << RESET << "curl_easy_perform() failed: "
<< curl_easy_strerror(curl_return_code) << "\n";
safe_exit(21);
}
if (fileStream) {
fileStream->close();
}
int http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
return {response, http_code};
// prevent compiler warning
return {"", -1};
}
}
namespace bakaapi {

View File

@ -94,6 +94,7 @@ void timetable_page() {
current_allocated = &timetable_allocated;
auto dateSelected = std::chrono::system_clock::now();
reload_for_new_week:
clear();
std::time_t date_time_t = std::chrono::system_clock::to_time_t(dateSelected);
std::tm local_time = *std::localtime(&date_time_t);

View File

@ -148,6 +148,7 @@ struct SelectorType {
struct complete_menu {
WINDOW *win;
ITEM **items;
size_t items_size;
MENU *menu;
};