Проблема с очисткой данных, нужно првоерить и исправить все 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(); dbLoadData();
Monitors *monitors = dbGetMonitorsList(); Monitors *monitors = loadMonitors();
for (size_t i = 0; i < monitors->size; ++i) for (size_t i = 0; i < monitors->size; ++i)
{ {
addParameterValue(PARAMETER_MONITORS, i, monitors->monitor[i]->data[3], !i); addParameterValue(PARAMETER_MONITORS, i, monitors->monitor[i]->data[3], !i);

67
db.c
View File

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

23
gui.c
View File

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

View File

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