- merge forgotten uninit from work branch - add set_str() function to free before overwriting - fix some other leaks
Unified · Split
| @@ -153,7 +153,6 @@ typedef struct MermaidContext { · 152 unmodified lines | ||
| 153 | 153 | } section_data[SECTION_MAX_NB_LEVELS]; |
| 154 | 154 | |
| 155 | 155 | unsigned nb_link_captions[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section, |
| 156 | − AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section, | |
| 157 | 156 | AVBPrint link_buf; ///< print buffer for writing diagram links |
| 158 | 157 | AVDictionary *link_dict; |
| 159 | 158 | } MermaidContext; |
| @@ -216,6 +215,32 @@ static av_cold int mermaid_init_html(AVTextFormatContext *tfc) · 56 unmodified lines | ||
| 216 | 215 | return 0; |
| 217 | 216 | } |
| 218 | 217 | |
| 218 | +static av_cold int mermaid_uninit(AVTextFormatContext *tfc) | |
| 219 | +{ | |
| 220 | + MermaidContext *mmc = tfc->priv; | |
| 221 | + | |
| 222 | + av_bprint_finalize(&mmc->link_buf, NULL); | |
| 223 | + av_dict_free(&mmc->link_dict); | |
| 224 | + | |
| 225 | + for (unsigned i = 0; i < SECTION_MAX_NB_LEVELS; i++) { | |
| 226 | + av_freep(&mmc->section_data[i].dest_id); | |
| 227 | + av_freep(&mmc->section_data[i].section_id); | |
| 228 | + av_freep(&mmc->section_data[i].src_id); | |
| 229 | + av_freep(&mmc->section_data[i].section_type); | |
| 230 | + } | |
| 231 | + | |
| 232 | + return 0; | |
| 233 | +} | |
| 234 | + | |
| 235 | +static void set_str(const char **dst, const char *src) | |
| 236 | +{ | |
| 237 | + if (*dst) | |
| 238 | + av_freep(dst); | |
| 239 | + | |
| 240 | + if (src) | |
| 241 | + *dst = av_strdup(src); | |
| 242 | +} | |
| 243 | + | |
| 219 | 244 | #define MM_INDENT() writer_printf(tfc, "%*c", mmc->indent_level * 2, ' ') |
| 220 | 245 | |
| 221 | 246 | static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *data) |
| @@ -266,6 +291,8 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 44 unmodified lines | ||
| 266 | 291 | break; |
| 267 | 292 | } |
| 268 | 293 | |
| 294 | + av_bprint_finalize(&css_buf, NULL); | |
| 295 | + av_freep(&directive); | |
| 269 | 296 | return; |
| 270 | 297 | } |
| 271 | 298 | |
| @@ -310,7 +337,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 38 unmodified lines | ||
| 310 | 337 | } |
| 311 | 338 | |
| 312 | 339 | mmc->section_data[tfc->level].subgraph_start_incomplete = 1; |
| 313 | − mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id); | |
| 340 | + set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id); | |
| 314 | 341 | } |
| 315 | 342 | |
| 316 | 343 | if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE) { |
| @@ -322,7 +349,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 5 unmodified lines | ||
| 322 | 349 | |
| 323 | 350 | if (sec_ctx->context_id) { |
| 324 | 351 | |
| 325 | − mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id); | |
| 352 | + set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id); | |
| 326 | 353 | |
| 327 | 354 | switch (mmc->diagram_config->diagram_type) { |
| 328 | 355 | case AV_DIAGRAMTYPE_GRAPH: |
| @@ -352,7 +379,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 23 unmodified lines | ||
| 352 | 379 | av_log(tfc, AV_LOG_ERROR, "Unable to write shape start. Missing id field. Section: %s", section->name); |
| 353 | 380 | } |
| 354 | 381 | |
| 355 | − mmc->section_data[tfc->level].section_id = av_strdup(sec_ctx->context_id); | |
| 382 | + set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id); | |
| 356 | 383 | } |
| 357 | 384 | |
| 358 | 385 | |
| @@ -371,7 +398,7 @@ static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *d · 12 unmodified lines | ||
| 371 | 398 | mmc->nb_link_captions[tfc->level] = 0; |
| 372 | 399 | |
| 373 | 400 | if (sec_ctx && sec_ctx->context_type) |
| 374 | − mmc->section_data[tfc->level].section_type = av_strdup(sec_ctx->context_type); | |
| 401 | + set_str(&mmc->section_data[tfc->level].section_type, sec_ctx->context_type); | |
| 375 | 402 | |
| 376 | 403 | ////if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) { |
| 377 | 404 | //// AVBPrint buf; |
| @@ -533,17 +560,17 @@ static void mermaid_print_value(AVTextFormatContext *tfc, const char *key, · 155 unmodified lines | ||
| 533 | 560 | int exit = 0; |
| 534 | 561 | |
| 535 | 562 | if (section->id_key && !strcmp(section->id_key, key)) { |
| 536 | − mmc->section_data[tfc->level].section_id = av_strdup(str); | |
| 563 | + set_str(&mmc->section_data[tfc->level].section_id, str); | |
| 537 | 564 | exit = 1; |
| 538 | 565 | } |
| 539 | 566 | |
| 540 | 567 | if (section->dest_id_key && !strcmp(section->dest_id_key, key)) { |
| 541 | − mmc->section_data[tfc->level].dest_id = av_strdup(str); | |
| 568 | + set_str(&mmc->section_data[tfc->level].dest_id, str); | |
| 542 | 569 | exit = 1; |
| 543 | 570 | } |
| 544 | 571 | |
| 545 | 572 | if (section->src_id_key && !strcmp(section->src_id_key, key)) { |
| 546 | − mmc->section_data[tfc->level].src_id = av_strdup(str); | |
| 573 | + set_str(&mmc->section_data[tfc->level].src_id, str); | |
| 547 | 574 | exit = 1; |
| 548 | 575 | } |
| 549 | 576 | |
| @@ -636,6 +663,7 @@ const AVTextFormatter avtextformatter_mermaid = { · 86 unmodified lines | ||
| 636 | 663 | .name = "mermaid", |
| 637 | 664 | .priv_size = sizeof(MermaidContext), |
| 638 | 665 | .init = mermaid_init, |
| 666 | + .uninit = mermaid_uninit, | |
| 639 | 667 | .print_section_header = mermaid_print_section_header, |
| 640 | 668 | .print_section_footer = mermaid_print_section_footer, |
| 641 | 669 | .print_integer = mermaid_print_int, |
| @@ -649,6 +677,7 @@ const AVTextFormatter avtextformatter_mermaidhtml = { · 7 unmodified lines | ||
| 649 | 677 | .name = "mermaidhtml", |
| 650 | 678 | .priv_size = sizeof(MermaidContext), |
| 651 | 679 | .init = mermaid_init_html, |
| 680 | + .uninit = mermaid_uninit, | |
| 652 | 681 | .print_section_header = mermaid_print_section_header, |
| 653 | 682 | .print_section_footer = mermaid_print_section_footer, |
| 654 | 683 | .print_integer = mermaid_print_int, |