Проблема с очисткой данных, нужно првоерить и исправить все free()

This commit is contained in:
Alexander Zhirov 2022-07-18 18:38:38 +03:00
parent 5e743f843f
commit 6eac429923
4 changed files with 49 additions and 47 deletions

View File

@ -20,7 +20,7 @@ void settingsLoad(char *pathDB)
dbLoadData();
Monitors *monitors = dbGetMonitorsList();
Monitors *monitors = loadMonitors();
for (size_t i = 0; i < monitors->size; ++i)
{
addParameterValue(PARAMETER_MONITORS, i, monitors->monitor[i]->data[3], !i);

67
db.c
View File

@ -12,7 +12,7 @@
#include "db.h"
#include "node_settings.h"
char *getPathDB(char *path)
char* getPathDB(char *path)
{
static char *current = NULL;
if (path)
@ -23,7 +23,7 @@ char *getPathDB(char *path)
return current;
}
static sqlite3 *dbGetBase(char *path)
static sqlite3* dbGetBase(char *path)
{
sqlite3 *db = NULL;
@ -51,29 +51,17 @@ static sqlite3 *dbGetBase(char *path)
*/
static int dbLoad(void *NotUsed, int argc, char **argv, char **azColName)
{
NotUsed = 0;
char **parameters = (char **) malloc(sizeof(char*) * argc);
for (int i = 0; i < argc; i++)
if (!getParameter(atoi(argv[0])))
{
parameters[i] = argv[i];
addParameterKey(atoi(argv[0]), argv[1], atoi(argv[2]), atoi(argv[3]), argv[7] ? getParameter(atoi(argv[7])) : NULL,
argv[8] ? getParameter(atoi(argv[8])) : NULL);
}
if (!getParameter(atoi(parameters[0])))
if (argv[4] && argv[5])
{
addParameterKey(atoi(parameters[0]), parameters[1], atoi(parameters[2]), atoi(parameters[3]),
parameters[7] ? getParameter(atoi(parameters[7])) : NULL,
parameters[8] ? getParameter(atoi(parameters[8])) : NULL);
addParameterValue(atoi(argv[0]), atoi(argv[4]), argv[5], atoi(argv[6]));
}
if (parameters[4] && parameters[5])
{
addParameterValue(atoi(parameters[0]), atoi(parameters[4]), parameters[5], atoi(parameters[6]));
}
free(parameters);
return 0;
}
@ -109,17 +97,20 @@ bool dbLoadData()
static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColName)
{
Hosts *hosts = *(Hosts **)answer;
fprintf(stderr, "dbCreateHostsList 1\n");
Hosts *hosts = *(Hosts**) answer;
Host *host = (Host*) malloc(sizeof(Host));
host->data = (char **) malloc(sizeof(char*) * argc);
host->data = (char**) malloc(sizeof(char*) * argc);
host->size = argc;
for (int i = 0; i < argc; i++)
{
host->data[i] = (char *) malloc(sizeof(char) * strlen(argv[i]));
host->data[i] = (char*) malloc(sizeof(char) * strlen(argv[i]));
strcpy(host->data[i], argv[i]);
}
fprintf(stderr, "dbCreateHostsList 2\n");
Host **tmp = hosts->host;
hosts->host = (Host**) malloc(sizeof(Host*) * ++hosts->size);
for (size_t i = 0; i < hosts->size - 1; ++i)
@ -132,11 +123,14 @@ static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColNa
}
hosts->host[hosts->size - 1] = host;
fprintf(stderr, "dbCreateHostsList 3\n");
return 0;
}
Hosts *dbGetHostsList()
Hosts* dbGetHostsList()
{
fprintf(stderr, "dbGetHostsList 1\n");
sqlite3 *db = dbGetBase(getPathDB(NULL));
if (!db)
{
@ -145,8 +139,8 @@ Hosts *dbGetHostsList()
char *err_msg = 0;
char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `hosts`.`ip`) `item`, `hosts`.`dns` as `dns`, `hosts`.`set` as `set` FROM `hosts` as `hosts`";
Hosts *hosts = (Hosts *) malloc(sizeof(Hosts));
fprintf(stderr, "dbGetHostsList 2\n");
Hosts *hosts = (Hosts*) malloc(sizeof(Hosts));
hosts->size = 0;
hosts->host = NULL;
@ -157,9 +151,9 @@ Hosts *dbGetHostsList()
sqlite3_close(db);
exit(-1);
}
fprintf(stderr, "dbGetHostsList 3\n");
sqlite3_close(db);
fprintf(stderr, "dbGetHostsList 4\n");
return hosts;
}
@ -171,6 +165,7 @@ void dbFreeHosts(Hosts *hosts)
{
free(hosts->host[i]->data[j]);
}
free(hosts->host[i]->data);
free(hosts->host[i]);
}
free(hosts);
@ -193,7 +188,6 @@ bool dbWriteParameter(Parameter name, bool set)
sqlite3_bind_int(res, 2, name);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", set ? "включено" : "выключено", getParameter(name)->key);
@ -277,7 +271,7 @@ int dbAddServer(char *ip, char *dns)
if (sqlite3_step(res) == SQLITE_ROW)
{
int id = atoi((const char *)sqlite3_column_text(res, 0));
int id = atoi((const char*) sqlite3_column_text(res, 0));
sqlite3_finalize(res);
sql = "UPDATE `hosts` set `set` = 0";
@ -312,7 +306,7 @@ int dbAddServer(char *ip, char *dns)
result = -5;
}
}
else if(sqlite3_step(res) == SQLITE_DONE)
else if (sqlite3_step(res) == SQLITE_DONE)
{
sqlite3_finalize(res);
@ -398,14 +392,14 @@ bool dbSetUserNameCurrent(char *current)
static int dbCreateMonitorsList(void *answer, int argc, char **argv, char **azColName)
{
Monitors *monitors = *(Monitors **)answer;
Monitors *monitors = *(Monitors**) answer;
Monitor *monitor = (Monitor*) malloc(sizeof(Monitor));
monitor->data = (char **) malloc(sizeof(char*) * argc);
monitor->data = (char**) malloc(sizeof(char*) * argc);
monitor->size = argc;
for (int i = 0; i < argc; i++)
{
monitor->data[i] = (char *) malloc(sizeof(char) * strlen(argv[i]));
monitor->data[i] = (char*) malloc(sizeof(char) * strlen(argv[i]));
strcpy(monitor->data[i], argv[i]);
}
@ -424,7 +418,7 @@ static int dbCreateMonitorsList(void *answer, int argc, char **argv, char **azCo
return 0;
}
Monitors *dbGetMonitorsList()
Monitors* dbGetMonitorsList()
{
sqlite3 *db = dbGetBase(getPathDB(NULL));
if (!db)
@ -433,9 +427,10 @@ Monitors *dbGetMonitorsList()
}
char *err_msg = 0;
char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `monitors`.`set` DESC) `item`, `monitors`.`name` as `name`, `monitors`.`set` as `set`, (ROW_NUMBER () OVER (ORDER BY `monitors`.`id`)) - 1 `item` FROM `monitors` as `monitors`";
char *sql =
"SELECT ROW_NUMBER () OVER (ORDER BY `monitors`.`set` DESC) `item`, `monitors`.`name` as `name`, `monitors`.`set` as `set`, (ROW_NUMBER () OVER (ORDER BY `monitors`.`id`)) - 1 `item` FROM `monitors` as `monitors`";
Monitors *monitors = (Monitors *) malloc(sizeof(Monitors));
Monitors *monitors = (Monitors*) malloc(sizeof(Monitors));
monitors->size = 0;
monitors->monitor = NULL;

23
gui.c
View File

@ -58,7 +58,9 @@ static int guiConnect(Ihandle *self)
static void createHostsList(Ihandle *iupList)
{
fprintf(stderr, "ERROR 1\n");
Hosts *hosts = dbGetHostsList();
fprintf(stderr, "ERROR 2\n");
char *indexItem = NULL;
char *serverName = NULL;
@ -74,6 +76,7 @@ static void createHostsList(Ihandle *iupList)
if (atoi(host->data[2]))
IupSetAttribute(iupList, "VALUE", serverName);
}
fprintf(stderr, "ERROR 3\n");
dbFreeHosts(hosts);
}
@ -104,13 +107,15 @@ Ihandle* guiStart()
inputServer = IupList(NULL);
IupSetAttribute(inputServer, "NAME", "SERVER");
IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера (обязательно)");
fprintf(stderr, "guiStart 1\n");
createHostsList(inputServer);
fprintf(stderr, "guiStart 2\n");
IupSetAttribute(inputServer, "SIZE", "100");
IupSetAttribute(inputServer, "EDITBOX", "YES");
IupSetAttribute(inputServer, "DROPDOWN", "YES");
hBoxServer = IupHbox(labelServer, inputServer, NULL);
IupSetAttribute(hBoxServer, "MARGIN", "10x10");
fprintf(stderr, "guiStart 3\n");
labelUser = IupLabel("Пользователь:");
IupSetAttribute(labelUser, "SIZE", "80x16");
IupSetAttribute(labelUser, "ALIGNMENT", "ARIGHT:ACENTER");
@ -122,7 +127,7 @@ Ihandle* guiStart()
IupSetAttribute(inputUser, "SIZE", "100");
hBoxUser = IupHbox(labelUser, inputUser, NULL);
IupSetAttribute(hBoxUser, "MARGIN", "10x0");
fprintf(stderr, "guiStart 4\n");
labelPassword = IupLabel("Пароль:");
IupSetAttribute(labelPassword, "SIZE", "80x16");
IupSetAttribute(labelPassword, "ALIGNMENT", "ARIGHT:ACENTER");
@ -135,38 +140,38 @@ Ihandle* guiStart()
IupSetAttribute(inputPassword, "PASSWORD", "YES");
hBoxPassword = IupHbox(labelPassword, inputPassword, NULL);
IupSetAttribute(hBoxPassword, "MARGIN", "10x10");
fprintf(stderr, "guiStart 5\n");
btnConnect = IupButton("Подключение", NULL);
IupSetHandle("CONNECT", btnConnect);
IupSetAttribute(btnConnect, "NAME", "CONNECT");
IupSetAttribute(btnConnect, "TIP", "Выполнить подключение");
IupSetAttribute(btnConnect, "BGCOLOR", "0 179 0");
IupSetAttribute(btnConnect, "FGCOLOR", "255 255 255");
fprintf(stderr, "guiStart 6\n");
btnClose = IupButton("Закрыть", NULL);
IupSetHandle("CLOSE", btnClose);
IupSetAttribute(btnClose, "NAME", "CLOSE");
IupSetAttribute(btnClose, "TIP", "Закрыть FreeRDP");
IupSetAttribute(btnClose, "BGCOLOR", "204 0 0");
IupSetAttribute(btnClose, "FGCOLOR", "255 255 255");
fprintf(stderr, "guiStart 7\n");
btnSettings = IupButton("Настройки", NULL);
IupSetAttribute(btnSettings, "NAME", "SETTINGS");
IupSetAttribute(btnSettings, "TIP", "Перейти в настройки");
IupSetAttribute(btnSettings, "BGCOLOR", "0 136 204");
IupSetAttribute(btnSettings, "FGCOLOR", "255 255 255");
fprintf(stderr, "guiStart 8\n");
hBoxButtons = IupHbox(IupFill(), btnSettings, btnConnect, btnClose, IupFill(), NULL);
IupSetAttribute(hBoxButtons, "ALIGNMENT", "ACENTER:ACENTER");
IupSetAttribute(hBoxButtons, "GAP", "10");
IupSetAttribute(hBoxButtons, "MARGIN", "10x10");
fprintf(stderr, "guiStart 9\n");
vBoxMain = IupVbox(labelImage, hBoxServer, hBoxUser, hBoxPassword, hBoxButtons, NULL);
IupSetCallback(btnConnect, "ACTION", (Icallback) guiConnect);
IupSetCallback(btnClose, "ACTION", (Icallback) guiExit);
IupSetCallback(btnSettings, "ACTION", (Icallback) settingsMainWindow);
fprintf(stderr, "guiStart 10\n");
dlg = IupDialog(vBoxMain);
IupSetAttribute(dlg, "IMAGE", "BG_HEAD");
IupSetAttribute(dlg, "ICON", "icon");
@ -175,6 +180,6 @@ Ihandle* guiStart()
IupSetAttribute(dlg, "PARENTDIALOG", "MAIN_WINDOW");
IupSetAttribute(dlg, "DEFAULTENTER", "CONNECT");
IupSetAttribute(dlg, "DEFAULTESC", "CLOSE");
fprintf(stderr, "guiStart 11\n");
return dlg;
}

View File

@ -48,7 +48,9 @@ Monitors *loadMonitors()
x_info *pcMonitors = getXInfo();
if (!checkMonitor(dbMonitors, pcMonitors)) // Если строки не равны, произвести перезапись в БД
{
dbFreeMonitors(dbMonitors);
if (dbMonitors)
dbFreeMonitors(dbMonitors);
if (!deleteAllMonitors())
{
fprintf(stderr, "Не удалось удалить записи мониторов из БД\n");