Free symbol->rendered structures on ZBarcode_Clear and repeated calls of ZBarcode_Render

This commit is contained in:
Jojakim Stahl 2017-10-16 09:49:44 +02:00
parent 1be3ac4d5a
commit c7d6256a6e
2 changed files with 57 additions and 40 deletions

View File

@ -76,6 +76,8 @@ struct zint_symbol *ZBarcode_Create() {
return symbol; return symbol;
} }
extern void render_free(struct zint_symbol *symbol); /* Free render structures */
void ZBarcode_Clear(struct zint_symbol *symbol) { void ZBarcode_Clear(struct zint_symbol *symbol) {
int i, j; int i, j;
@ -94,6 +96,9 @@ void ZBarcode_Clear(struct zint_symbol *symbol) {
} }
symbol->bitmap_width = 0; symbol->bitmap_width = 0;
symbol->bitmap_height = 0; symbol->bitmap_height = 0;
// If there is a rendered version, ensure its memory is released
render_free(symbol);
} }
void ZBarcode_Delete(struct zint_symbol *symbol) { void ZBarcode_Delete(struct zint_symbol *symbol) {
@ -101,47 +106,8 @@ void ZBarcode_Delete(struct zint_symbol *symbol) {
free(symbol->bitmap); free(symbol->bitmap);
// If there is a rendered version, ensure its memory is released // If there is a rendered version, ensure its memory is released
if (symbol->rendered != NULL) { render_free(symbol);
struct zint_render_line *line;
struct zint_render_string *string;
struct zint_render_ring *ring;
struct zint_render_hexagon *hexagon;
// Free lines
line = symbol->rendered->lines;
while (line) {
struct zint_render_line *l = line;
line = line->next;
free(l);
}
// Free Strings
string = symbol->rendered->strings;
while (string) {
struct zint_render_string *s = string;
string = string->next;
free(s->text);
free(s);
}
// Free Rings
ring = symbol->rendered->rings;
while (ring) {
struct zint_render_ring *r = ring;
ring = ring->next;
free(r);
}
// Free Hexagons
hexagon = symbol->rendered->hexagons;
while (hexagon) {
struct zint_render_hexagon *h = hexagon;
hexagon = hexagon->next;
free(h);
}
// Free Render
free(symbol->rendered);
}
free(symbol); free(symbol);
} }

View File

@ -55,6 +55,7 @@ struct zint_render_hexagon *render_plot_create_hexagon(float x, float y);
int render_plot_add_hexagon(struct zint_symbol *symbol, struct zint_render_hexagon *hexagon, struct zint_render_hexagon **last_hexagon); int render_plot_add_hexagon(struct zint_symbol *symbol, struct zint_render_hexagon *hexagon, struct zint_render_hexagon **last_hexagon);
int render_plot_add_string(struct zint_symbol *symbol, unsigned char *text, float x, float y, float fsize, float width, struct zint_render_string **last_string); int render_plot_add_string(struct zint_symbol *symbol, unsigned char *text, float x, float y, float fsize, float width, struct zint_render_string **last_string);
void render_free(struct zint_symbol *symbol);
int render_plot(struct zint_symbol *symbol, const float width, const float height) { int render_plot(struct zint_symbol *symbol, const float width, const float height) {
struct zint_render *render; struct zint_render *render;
@ -78,6 +79,8 @@ int render_plot(struct zint_symbol *symbol, const float width, const float heigh
float x_dimension; float x_dimension;
int upceanflag = 0; int upceanflag = 0;
// Free any previous rendering structures
render_free(symbol);
// Allocate memory for the rendered version // Allocate memory for the rendered version
render = symbol->rendered = (struct zint_render *) malloc(sizeof (struct zint_render)); render = symbol->rendered = (struct zint_render *) malloc(sizeof (struct zint_render));
if (!symbol->rendered) return ZINT_ERROR_MEMORY; if (!symbol->rendered) return ZINT_ERROR_MEMORY;
@ -778,3 +781,51 @@ int render_plot_add_string(struct zint_symbol *symbol,
return 1; return 1;
} }
/*
* Free the data structures created by render_plot
*/
void render_free(struct zint_symbol *symbol) {
if (symbol->rendered != NULL) {
struct zint_render_line *line;
struct zint_render_string *string;
struct zint_render_ring *ring;
struct zint_render_hexagon *hexagon;
// Free lines
line = symbol->rendered->lines;
while (line) {
struct zint_render_line *l = line;
line = line->next;
free(l);
}
// Free Strings
string = symbol->rendered->strings;
while (string) {
struct zint_render_string *s = string;
string = string->next;
free(s->text);
free(s);
}
// Free Rings
ring = symbol->rendered->rings;
while (ring) {
struct zint_render_ring *r = ring;
ring = ring->next;
free(r);
}
// Free Hexagons
hexagon = symbol->rendered->hexagons;
while (hexagon) {
struct zint_render_hexagon *h = hexagon;
hexagon = hexagon->next;
free(h);
}
// Free Render
free(symbol->rendered);
symbol->rendered = NULL;
}
}