Skip to content

Commit 38b08ab

Browse files
authored
Merge pull request #20 from krychu/#19-memory-leak
Properly free duplicate tiles
2 parents b1b2c42 + 25a50fa commit 38b08ab

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

wfc.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,11 +776,18 @@ static int wfc__remove_duplicate_tiles(struct wfc__tile **tiles, int *tile_cnt)
776776
}
777777

778778
if (unique) {
779-
(*tiles)[unique_cnt] = (*tiles)[j];
779+
if (unique_cnt != j) {
780+
struct wfc__tile tmp = (*tiles)[unique_cnt];
781+
(*tiles)[unique_cnt] = (*tiles)[j];
782+
(*tiles)[j] = tmp;
783+
}
780784
unique_cnt++;
781785
}
782786
}
783787

788+
for (int i=unique_cnt; i<*tile_cnt; i++)
789+
wfc_img_destroy((*tiles)[i].image);
790+
784791
struct wfc__tile *unique_tiles = realloc(*tiles, sizeof(**tiles) * unique_cnt);
785792
if (unique_tiles == NULL) {
786793
p("wfc__remove_duplicate_tiles: error\n");

0 commit comments

Comments
 (0)