From 24dc461099b9e50fea6d724a27979bb6e331d4aa Mon Sep 17 00:00:00 2001 From: zeniko Date: Sat, 1 Jun 2013 21:33:12 +0200 Subject: prevent memory leak and unexpected exception fz_free_device usually doesn't throw (throwing in clean-up code is quite pointless anyway) but freeing the text extraction device might do so under memory pressure. This patch catches any potential exception to guarantee a proper clean-up. --- fitz/text_extract.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'fitz') diff --git a/fitz/text_extract.c b/fitz/text_extract.c index 3bb0db56..45e0395a 100644 --- a/fitz/text_extract.c +++ b/fitz/text_extract.c @@ -951,18 +951,29 @@ fz_text_free_user(fz_device *dev) fz_context *ctx = dev->ctx; fz_text_device *tdev = dev->user; - add_span_to_soup(tdev->spans, tdev->cur_span); - tdev->cur_span = NULL; + fz_try(ctx) + { - strain_soup(ctx, tdev); - free_span_soup(tdev->spans); + add_span_to_soup(tdev->spans, tdev->cur_span); + tdev->cur_span = NULL; - /* TODO: smart sorting of blocks in reading order */ - /* TODO: unicode NFC normalization */ + strain_soup(ctx, tdev); - fz_bidi_reorder_text_page(ctx, tdev->page); + /* TODO: smart sorting of blocks in reading order */ + /* TODO: unicode NFC normalization */ - fz_free(dev->ctx, tdev); + fz_bidi_reorder_text_page(ctx, tdev->page); + } + fz_always(ctx) + { + free_span_soup(tdev->spans); + fz_free(dev->ctx, tdev); + } + fz_catch(ctx) + { + /* TODO: mark fz_free_device as "doesn't throw" (else rethrowing would + have to be caught/rethrown again in fz_free_device) */ + } } fz_device * -- cgit v1.2.3