Skip to content

Commit a86d7e1

Browse files
committed
added functions to free objects
1 parent 96d89b0 commit a86d7e1

File tree

4 files changed

+178
-23
lines changed

4 files changed

+178
-23
lines changed

configure.ac

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ AC_TYPE_SIZE_T
2929

3030
# Checks for library functions.
3131
AC_FUNC_MALLOC
32-
AC_CHECK_FUNCS([access
33-
getcwd
34-
mkdir])
32+
AC_CHECK_FUNCS([access getcwd mkdir])
3533

3634
# The info directory which will be read by the frontend
3735
CPDB_BACKEND_INFO_DIR="$datadir/print-backends"

cpdb/cpdb-frontend.c

Lines changed: 130 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,40 @@ cpdb_frontend_obj_t *cpdbGetNewFrontendObj(char *instance_name,
2323
f->add_cb = add_cb;
2424
f->rem_cb = rem_cb;
2525
f->num_backends = 0;
26-
f->backend = g_hash_table_new(g_str_hash, g_str_equal);
26+
f->backend = g_hash_table_new_full(g_str_hash,
27+
g_str_equal,
28+
free,
29+
g_object_unref);
2730
f->num_printers = 0;
28-
f->printer = g_hash_table_new(g_str_hash, g_str_equal);
31+
f->printer = g_hash_table_new_full(g_str_hash,
32+
g_str_equal,
33+
free,
34+
(GDestroyNotify) cpdbDeletePrinterObj);
2935
f->last_saved_settings = cpdbReadSettingsFromDisk();
3036
return f;
3137
}
3238

39+
void cpdbDeleteFrontendObj(cpdb_frontend_obj_t *f)
40+
{
41+
if (f == NULL)
42+
return;
43+
44+
cpdbDisconnectFromDBus(f);
45+
46+
if (f->skeleton)
47+
g_object_unref(f->skeleton);
48+
if (f->bus_name)
49+
free(f->bus_name);
50+
if (f->backend)
51+
g_hash_table_destroy(f->backend);
52+
if (f->printer)
53+
g_hash_table_destroy(f->printer);
54+
if (f->last_saved_settings)
55+
cpdbDeleteSettings(f->last_saved_settings);
56+
57+
free(f);
58+
}
59+
3360
static void on_printer_added(GDBusConnection *connection,
3461
const gchar *sender_name,
3562
const gchar *object_path,
@@ -169,6 +196,9 @@ void cpdbConnectToDBus(cpdb_frontend_obj_t *f)
169196

170197
void cpdbDisconnectFromDBus(cpdb_frontend_obj_t *f)
171198
{
199+
if (f->connection == NULL || g_dbus_connection_is_closed(f->connection))
200+
return;
201+
172202
print_frontend_emit_stop_listing(f->skeleton);
173203
g_dbus_connection_flush_sync(f->connection, NULL, NULL);
174204

@@ -272,6 +302,7 @@ gboolean cpdbAddPrinter(cpdb_frontend_obj_t *f,
272302
CPDB_DEBUG_LEVEL_ERR);
273303
return FALSE;
274304
}
305+
g_object_ref(p->backend_proxy);
275306

276307
g_hash_table_insert(f->printer, cpdbConcatSep(p->id, p->backend_name), p);
277308
f->num_printers++;
@@ -298,20 +329,17 @@ cpdb_printer_obj_t *cpdbRemovePrinter(cpdb_frontend_obj_t *f,
298329
return p;
299330
}
300331

301-
void
302-
cpdbRefreshPrinterList(cpdb_frontend_obj_t *f)
332+
void cpdbRefreshPrinterList(cpdb_frontend_obj_t *f)
303333
{
304334
print_frontend_emit_refresh_backend(f->skeleton);
305335
}
306336

307-
void
308-
cpdbHideRemotePrinters(cpdb_frontend_obj_t *f)
337+
void cpdbHideRemotePrinters(cpdb_frontend_obj_t *f)
309338
{
310339
print_frontend_emit_hide_remote_printers(f->skeleton);
311340
}
312341

313-
void
314-
cpdbUnhideRemotePrinters(cpdb_frontend_obj_t *f)
342+
void cpdbUnhideRemotePrinters(cpdb_frontend_obj_t *f)
315343
{
316344
print_frontend_emit_unhide_remote_printers(f->skeleton);
317345
}
@@ -659,6 +687,24 @@ cpdb_printer_obj_t *cpdbGetNewPrinterObj()
659687
return p;
660688
}
661689

690+
void cpdbDeletePrinterObj(cpdb_printer_obj_t *p)
691+
{
692+
if (p == NULL)
693+
return;
694+
695+
if (p->backend_name)
696+
free(p->backend_name);
697+
if (p->backend_proxy)
698+
g_object_unref(p->backend_proxy);
699+
if (p->options)
700+
cpdbDeleteOptions(p->options);
701+
702+
if (p->settings)
703+
cpdbDeleteSettings(p->settings);
704+
705+
free(p);
706+
}
707+
662708
void cpdbFillBasicOptions(cpdb_printer_obj_t *p,
663709
GVariant *gv)
664710
{
@@ -1277,7 +1323,7 @@ cpdb_settings_t *cpdbGetNewSettings()
12771323
{
12781324
cpdb_settings_t *s = g_new0(cpdb_settings_t, 1);
12791325
s->count = 0;
1280-
s->table = g_hash_table_new(g_str_hash, g_str_equal);
1326+
s->table = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
12811327
return s;
12821328
}
12831329

@@ -1464,12 +1510,13 @@ cpdb_settings_t *cpdbReadSettingsFromDisk()
14641510

14651511
void cpdbDeleteSettings(cpdb_settings_t *s)
14661512
{
1467-
if (s)
1468-
{
1469-
GHashTable *h = s->table;
1470-
free(s);
1471-
g_hash_table_destroy(h);
1472-
}
1513+
if (s == NULL)
1514+
return;
1515+
1516+
if (s->table)
1517+
g_hash_table_destroy(s->table);
1518+
1519+
free(s);
14731520
}
14741521
/**
14751522
________________________________________________ cpdb_options_t __________________________________________
@@ -1478,11 +1525,30 @@ cpdb_options_t *cpdbGetNewOptions()
14781525
{
14791526
cpdb_options_t *o = g_new0(cpdb_options_t, 1);
14801527
o->count = 0;
1481-
o->table = g_hash_table_new(g_str_hash, g_str_equal);
1482-
o->media = g_hash_table_new(g_str_hash, g_str_equal);
1528+
o->table = g_hash_table_new_full(g_str_hash,
1529+
g_str_equal,
1530+
NULL,
1531+
(GDestroyNotify) cpdbDeleteOption);
1532+
o->media = g_hash_table_new_full(g_str_hash,
1533+
g_str_equal,
1534+
NULL,
1535+
(GDestroyNotify) cpdbDeleteMedia);
14831536
return o;
14841537
}
14851538

1539+
void cpdbDeleteOptions(cpdb_options_t *opts)
1540+
{
1541+
if (opts == NULL)
1542+
return;
1543+
1544+
if (opts->table)
1545+
g_hash_table_destroy(opts->table);
1546+
if (opts->media)
1547+
g_hash_table_destroy(opts->media);
1548+
1549+
free(opts);
1550+
}
1551+
14861552
/**************cpdb_option_t************************************/
14871553
void cpdbPrintOption(const cpdb_option_t *opt)
14881554
{
@@ -1496,6 +1562,53 @@ void cpdbPrintOption(const cpdb_option_t *opt)
14961562
printf(" --> DEFAULT: %s\n\n", opt->default_value);
14971563
}
14981564

1565+
void cpdbDeleteOption(cpdb_option_t *opt)
1566+
{
1567+
if (opt == NULL)
1568+
return;
1569+
1570+
if (opt->option_name)
1571+
free(opt->option_name);
1572+
if (opt->supported_values)
1573+
free(opt->supported_values);
1574+
if (opt->default_value)
1575+
free(opt->default_value);
1576+
1577+
free(opt);
1578+
}
1579+
1580+
/**************cpdb_option_t************************************/
1581+
void cpdbPrintMedia(cpdb_media_t *media)
1582+
{
1583+
printf("[+] Media: %s\n", media->name);
1584+
printf(" * width = %d\n", media->width);
1585+
printf(" * length = %d\n", media->length);
1586+
printf(" --> Supported margins: %d\n", media->num_margins);
1587+
printf(" left, right, top, bottom\n");
1588+
for (int i = 0; i < media->num_margins; i++)
1589+
{
1590+
printf(" * %d, %d, %d, %d,\n",
1591+
media->margins[i].left,
1592+
media->margins[i].right,
1593+
media->margins[i].top,
1594+
media->margins[i].bottom);
1595+
}
1596+
printf("\n");
1597+
}
1598+
1599+
void cpdbDeleteMedia(cpdb_media_t *media)
1600+
{
1601+
if (media == NULL)
1602+
return;
1603+
1604+
if (media->name)
1605+
free(media->name);
1606+
if (media->margins)
1607+
free(media->margins);
1608+
1609+
free(media);
1610+
}
1611+
14991612
/**
15001613
* ________________________________ cpdb_job_t __________________________
15011614
*/

cpdb/cpdb-frontend.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ struct cpdb_frontend_obj_s
9191
*
9292
*/
9393
cpdb_frontend_obj_t *cpdbGetNewFrontendObj(char *instance_name, cpdb_event_callback add_cb, cpdb_event_callback remove_cb);
94+
void cpdbDeleteFrontendObj(cpdb_frontend_obj_t *f);
9495

9596
/**
9697
* Start the frontend D-Bus Service
@@ -259,6 +260,11 @@ struct cpdb_printer_obj_s
259260

260261
cpdb_printer_obj_t *cpdbGetNewPrinterObj();
261262

263+
/**
264+
* Frees up memory
265+
*/
266+
void cpdbDeletePrinterObj(cpdb_printer_obj_t *p);
267+
262268
/**
263269
* Fill the basic options of cpdb_printer_obj_t from the GVariant returned with the printerAdded signal
264270
*/
@@ -268,6 +274,7 @@ void cpdbFillBasicOptions(cpdb_printer_obj_t *, GVariant *);
268274
* Print the basic options of cpdb_printer_obj_t
269275
*/
270276
void cpdbPrintBasicOptions(cpdb_printer_obj_t *);
277+
271278
gboolean cpdbIsAcceptingJobs(cpdb_printer_obj_t *);
272279
char *cpdbGetState(cpdb_printer_obj_t *);
273280

@@ -509,6 +516,9 @@ void cpdbSaveSettingsToDisk(cpdb_settings_t *s);
509516
*/
510517
cpdb_settings_t *cpdbReadSettingsFromDisk();
511518

519+
/**
520+
* Frees up memory
521+
*/
512522
void cpdbDeleteSettings(cpdb_settings_t *);
513523

514524
/************************************************************************************************/
@@ -529,20 +539,27 @@ struct cpdb_options_s
529539
*/
530540
cpdb_options_t *cpdbGetNewOptions();
531541

542+
/**
543+
* Frees up memory
544+
*/
545+
void cpdbDeleteOptions(cpdb_options_t *);
546+
532547
/************************************************************************************************/
533548
/**
534549
______________________________________ cpdb_option_t __________________________________________
535550
536551
**/
537552
struct cpdb_option_s
538553
{
539-
const char *option_name;
554+
char *option_name;
540555
int num_supported;
541556
char **supported_values;
542557
char *default_value;
543558
};
544559
void cpdbPrintOption(const cpdb_option_t *opt);
545560

561+
void cpdbDeleteOption(cpdb_option_t *);
562+
546563
/************************************************************************************************/
547564

548565
/**
@@ -566,13 +583,16 @@ ______________________________________ cpdb_media_t ____________________________
566583

567584
struct cpdb_media_s
568585
{
569-
const char *name;
586+
char *name;
570587
int width;
571588
int length;
572589
int num_margins;
573590
cpdb_margin_t *margins;
574591
};
575592

593+
void cpdbPrintMedia(cpdb_media_t *);
594+
void cpdbDeleteMedia(cpdb_media_t *);
595+
576596
/************************************************************************************************/
577597
/**
578598
______________________________________ cpdb_job_t __________________________________________

demo/print_frontend.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static int add_printer_callback(cpdb_printer_obj_t *p)
1818
static int remove_printer_callback(cpdb_printer_obj_t *p)
1919
{
2020
g_message("Removed Printer %s : %s!\n", p->name, p->backend_name);
21+
cpdbDeletePrinterObj(p);
2122
}
2223

2324
static void acquire_details_callback(cpdb_printer_obj_t *p, int success, void *user_data)
@@ -58,7 +59,7 @@ gpointer parse_commands(gpointer user_data)
5859
scanf("%s", buf);
5960
if (strcmp(buf, "stop") == 0)
6061
{
61-
cpdbDisconnectFromDBus(f);
62+
cpdbDeleteFrontendObj(f);
6263
g_message("Stopping front end..\n");
6364
exit(0);
6465
}
@@ -115,6 +116,29 @@ gpointer parse_commands(gpointer user_data)
115116
cpdbPrintOption(value);
116117
}
117118
}
119+
else if (strcmp(buf, "get-all-media") == 0)
120+
{
121+
char printer_id[100];
122+
char backend_name[100];
123+
scanf("%s%s", printer_id, backend_name);
124+
g_message("Getting all attributes ..\n");
125+
cpdb_printer_obj_t *p = cpdbFindPrinterObj(f, printer_id, backend_name);
126+
127+
if(p == NULL)
128+
continue;
129+
130+
cpdb_options_t *opts = cpdbGetAllOptions(p);
131+
132+
printf("Retrieved %d medias.\n", opts->media_count);
133+
GHashTableIter iter;
134+
gpointer value;
135+
136+
g_hash_table_iter_init(&iter, opts->media);
137+
while (g_hash_table_iter_next(&iter, NULL, &value))
138+
{
139+
cpdbPrintMedia(value);
140+
}
141+
}
118142
else if (strcmp(buf, "get-default") == 0)
119143
{
120144
char printer_id[100], backend_name[100], option_name[100];

0 commit comments

Comments
 (0)