Skip to content

Commit

Permalink
good grief, fix this little tool's memory management.
Browse files Browse the repository at this point in the history
was using malloc to allocate and bu_free to release, yikes.  switched to bu.
  • Loading branch information
brlcad committed Jan 28, 2025
1 parent 72e47ed commit ff79d92
Showing 1 changed file with 13 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/util/pixmorph.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,26 +480,13 @@ main(int argc, char **argv)
}

/* Allocate memory for our bag o' pixels. */

pa = (unsigned char *)malloc(pa_width*pa_height*3);
pb = (unsigned char *)malloc(pa_width*pa_height*3);
wa = (unsigned char *)malloc(pa_width*pa_height*3);
wb = (unsigned char *)malloc(pa_width*pa_height*3);
morph = (unsigned char *)malloc(pa_width*pa_height*3);

if (pa == NULL || pb == NULL || wa == NULL || wb == NULL ||
morph == NULL) {
fprintf(stderr, "pixmorph: memory allocation failure\n");
bu_free(pa, "pa alloc from malloc");
bu_free(pb, "pb alloc from malloc");
bu_free(wa, "wa alloc from malloc");
bu_free(wb, "wb alloc from malloc");
bu_free(morph, "morph alloc from malloc");
return 1;
}
pa = (unsigned char *)bu_malloc(pa_width*pa_height*3, "pa");
pb = (unsigned char *)bu_malloc(pa_width*pa_height*3, "pb");
wa = (unsigned char *)bu_malloc(pa_width*pa_height*3, "wa");
wb = (unsigned char *)bu_malloc(pa_width*pa_height*3, "wb");
morph = (unsigned char *)bu_malloc(pa_width*pa_height*3, "morph");

/* The following is our memorizing table for weight calculation. */

for (i = 0; i < MAXLEN; i++)
weightlookup[i] = -1.0;

Expand All @@ -521,7 +508,7 @@ main(int argc, char **argv)
/* Process the lines file. */

lines_headerinfo(linesfile, &a, &b, &p, &numlines);
lines = (struct lineseg *)malloc(numlines * sizeof(struct lineseg));
lines = (struct lineseg *)bu_malloc(numlines * sizeof(struct lineseg), "lines");
numlines = lines_read(linesfile, numlines, lines,
pa_width, pa_height, warpfrac, p*b);
fprintf(stderr, "pixmorph: %ld line segments read\n", numlines);
Expand All @@ -548,7 +535,13 @@ main(int argc, char **argv)

fprintf(stderr, "pixmorph: Morph complete.\n");

/* System takes care of memory deallocation. */
/* memory deallocation. */
bu_free(pa, "pa");
bu_free(pb, "pb");
bu_free(wa, "wa");
bu_free(wb, "wb");
bu_free(morph, "morph");
bu_free(lines, "lines");

return 0;
}
Expand Down

0 comments on commit ff79d92

Please sign in to comment.