Skip to content

Commit

Permalink
fixed memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mjoelnir committed Apr 24, 2018
1 parent dd167b0 commit 67b53c3
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 53 deletions.
52 changes: 43 additions & 9 deletions cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void cache_entry_load_artists (cache *c)
sqlite3_stmt *res;
const char *tail = NULL;
int error;
error = sqlite3_prepare_v2(c->db, "SELECT name, id FROM artist ORDER BY name;", 1000, &res, &tail);
error = sqlite3_prepare_v2(c->db, "SELECT name, id FROM artist ORDER BY name;", -1, &res, &tail);

// fill array of artists with values from the database
int count = 0;
Expand All @@ -106,7 +106,8 @@ void cache_entry_load_artists (cache *c)
artists[count].id = atoi(sqlite3_column_text(res, 1));
count++;
}

sqlite3_finalize(res);

logcmd(LOG_DMSG, "nartists: %i", c->nartists);
c->artists = artists;
c->selectedArtist = 0;
Expand Down Expand Up @@ -140,15 +141,16 @@ void cache_entry_load_album (cache *c)
if (! album)
logcmd(LOG_ERROR_MALLOC, "cache: cache_entry_load_album: unalbe to allocate memory for album");

sqlite3_stmt *res;
sqlite3_stmt *res = NULL;
const char *tail = NULL;

size = snprintf(NULL, 0, "SELECT name, id FROM album WHERE artist_id=%i ORDER BY name;",
c->artists[c->selectedArtist].id);
sql = realloc(sql, sizeof(char) * size);
snprintf(sql, size, "SELECT name, id FROM album WHERE artist_id=%i ORDER BY name;",
c->artists[c->selectedArtist].id);
int error = sqlite3_prepare_v2(c->db, sql, 1000, &res, &tail);
int error = sqlite3_prepare_v2(c->db, sql, -1, &res, &tail);


// fill album array
int count = 0;
Expand All @@ -158,6 +160,8 @@ void cache_entry_load_album (cache *c)
album[count].id = atoi(sqlite3_column_text(res, 1));
count++;
}
sqlite3_finalize(res);


free(sql);
c->album = album;
Expand Down Expand Up @@ -207,7 +211,7 @@ void cache_entry_load_tracks (cache *c)
c->artists[c->selectedArtist].id,
c->sorting);

int error = sqlite3_prepare_v2(c->db, sql, 1000, &res, &tail);
int error = sqlite3_prepare_v2(c->db, sql, -1, &res, &tail);

// fill track array
int count = 0;
Expand All @@ -217,6 +221,7 @@ void cache_entry_load_tracks (cache *c)
tracks[count].id = atoi(sqlite3_column_text(res, 1));
count++;
}
sqlite3_finalize(res);

free(sql);
c->tracks = tracks;
Expand All @@ -227,17 +232,17 @@ void cache_entry_load_tracks (cache *c)
char *cache_load_filepath (cache *c)
{
logcmd(LOG_DMSG, "track_id: %i", c->tracks[c->selectedTrack].id);
char *err = NULL;
int size = snprintf(NULL, 0, "SELECT path FROM track WHERE id=%i;", c->tracks[c->selectedTrack].id);
char *sql = malloc(sizeof(char) * size);
snprintf(sql, size, "SELECT path FROM track WHERE id=%i;", c->tracks[c->selectedTrack].id);

char *string;
sqlite3_stmt *res;
const char *tail = NULL;
int error = sqlite3_prepare_v2(c->db, sql, 1000, &res, &tail);
int error = sqlite3_prepare_v2(c->db, sql, -1, &res, &tail);
if (sqlite3_step(res) == SQLITE_ROW)
string = copy_string(sqlite3_column_text(res, 0));
sqlite3_finalize(res);

free(sql);
return string;
Expand Down Expand Up @@ -275,16 +280,45 @@ void cache_close (cache *c)
c->currentTrack.id = -1;
// close members of cache
if (c->artists)
{
for (int i = 0; i < c->nartists; i++)
{
if (c->artists[i].name)
free(c->artists[i].name);
}
free(c->artists);
}

if (c->album)
{
for (int i = 0; i < c->nalbum; i++)
{
if (c->album[i].name)
free(c->album[i].name);
}
free(c->album);
}

if (c->tracks)
{
for (int i = 0; i < c->ntracks; i++)
{
if (c->tracks[i].name)
free(c->tracks[i].name);
}
free(c->tracks);
}

/*
if (c->lyrics_path)
free(c->lyrics_path);
if (c->sorting)
free(c->sorting);
* are freed during the freeing process of the configparser
*/

if (c->commands)
free(c->commands);
c->commands = cmd_table_free(c->commands);

free(c);
}
48 changes: 39 additions & 9 deletions clapl.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ void load_config (configparser *cp)
}

//initialize and load config file
configparser_init(cp, ".config/clapl/config", 2, '=');
configparser_init(cp, ".config/clapl/config", 5, '=');
configparser_load(cp);
}


int main (int argc, char **argv)
{
srand(120895);
configparser cp;
load_config(&cp); // load config file
srand(time(NULL));
configparser *cp = malloc(sizeof(configparser));
load_config(cp); // load config file

//LOGGER_PATH = ".config/clapl/log.txt";
LOGGER_PATH = configparser_get_string(&cp, "log_file"); // set path for log file
LOGGER_PATH = configparser_get_string(cp, "log_file"); // set path for log file
LOGGER_FILE = fopen(LOGGER_PATH, "w"); // open log file

playback_init(); // initialize libav and libao
sqlite3 *db = db_init();
ui_init(); // initialize userinterface
userinterface *ui = ui_create(db, &cp); // create userinterface
userinterface *ui = ui_create(db, cp); // create userinterface

ui->c->commands = load_commands();

Expand All @@ -49,16 +49,46 @@ int main (int argc, char **argv)
pthread_t thread;

char ch;
char running = 1;
while (1)
{
ch = getch();
input(ui, ui->c, a, &thread, ch);
if (ch == 'q')
break;
}
/*
while ((ch = getch()) != 'q') // main loop
input(ui, ui->c, a, &thread, ch);
*/

/*
cache_entry_load_album(ui->c);
cache_entry_load_tracks(ui->c);
*/
/*
input(ui, ui->c, a, &thread, 'p');
input(ui, ui->c, a, &thread, 'q');
char running = 1;
char ch;
while (running)
{
ch = getchar();
getchar();
if (ch == 'q')
running = 0;
input(ui, ui->c, a, &thread, ch);
}
*/


endwin(); // close ncurses
a = audio_destroy(a);
configparser_delete(cp);
cache_close(ui->c);
free(ui);
db_close(db);
if (db)
free(db);


fclose(LOGGER_FILE); // close log file
return 0;
}
Expand Down
25 changes: 25 additions & 0 deletions commandtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ int cmd_table_init (cmd_table *commands, const int size)
}


void cmd_table_entry_free (cmd_table_entry *e)
{
if (e->next)
cmd_table_entry_free(e->next);
free(e);
}


cmd_table *cmd_table_free (cmd_table *commands)
{
if (!commands)
return NULL;

for (int i = 0; i < commands->size; i++)
{
cmd_table_entry *e = commands->table[i];
if (e)
cmd_table_entry_free(e);
}
free(commands->table);
free(commands);
return NULL;
}


int cmd_table_hash (const cmd_table *commands, const char *key)
{
unsigned long int hashval = 0;
Expand Down
1 change: 1 addition & 0 deletions commandtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ typedef struct
cmd_table *cmd_table_create (const int size);

int cmd_table_init (cmd_table *commands, const int size);
cmd_table *cmd_table_free (cmd_table *commands);

int cmd_table_hash (const cmd_table *commands, const char *key);

Expand Down
29 changes: 23 additions & 6 deletions configparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ char *read_line (FILE *const file, const int buffer_size)


if (c == EOF && pos == 0)
{
free(line);
return NULL;
}

if (c == '\n' && pos == 0) // removes empty lines
{
free(line);
return read_line(file, buffer_size);
}

if (line[0] == '#')
{
free(line);
return read_line(file, buffer_size);
}

char *ptr = strrchr(line, '#');
if (ptr)
Expand All @@ -45,15 +54,23 @@ void configparser_init (configparser *const cp, char *const file, const int size
logcmd(LOG_ERROR_MALLOC, "configparser: configparser_init: could not malloc cp->file");

cp->delimiter = delimiter;
ht_init (&cp->ht, size); // create hash table
cp->ht = ht_create(size); // create hashtable
}


configparser *configparser_delete (configparser *cp)
{
cp->ht = ht_free(cp->ht);
free(cp);
return NULL;
}


void configparser_init_file (configparser *const cp, FILE *file, const int size, const char delimiter)
{
cp->file = file;
cp->delimiter = delimiter;
ht_init(&cp->ht, size);
cp->ht = ht_create(size);
}


Expand All @@ -64,7 +81,7 @@ void configparser_load (configparser *const cp)
while (line)
{
value = configparser_split_string(line, cp->delimiter);
ht_set(&cp->ht, line, value);
ht_set(cp->ht, line, value);
free(line);
value = NULL;
line = read_line(cp->file, 255);
Expand All @@ -87,7 +104,7 @@ char *configparser_split_string (char *const string, const char delimiter)

int configparser_get_bool (configparser *const cp, char *const key)
{
char *value = ht_get(&cp->ht, key);
char *value = ht_get(cp->ht, key);
switch(*value)
{
case 't': case 'T':
Expand All @@ -112,7 +129,7 @@ int configparser_get_bool (configparser *const cp, char *const key)

int configparser_get_int (configparser *const cp, char *const key)
{
char *value = ht_get(&cp->ht, key);
char *value = ht_get(cp->ht, key);
if (value)
return (atoi(value));

Expand All @@ -122,6 +139,6 @@ int configparser_get_int (configparser *const cp, char *const key)

char *configparser_get_string (configparser *const cp, char *const key)
{
return ht_get(&cp->ht, key);
return ht_get(cp->ht, key);
}

3 changes: 2 additions & 1 deletion configparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
typedef struct
{
FILE *file;
hashtable ht;
hashtable *ht;
char delimiter;
} configparser;


void configparser_init (configparser *const cp, char *const file, const int size, const char delimiter);
configparser *configparser_delete (configparser *cp);

void configparser_init_file (configparser *const cp, FILE *file, const int size, const char delimiter);

Expand Down
Loading

0 comments on commit 67b53c3

Please sign in to comment.