diff options
author | Michael Vrhel <michael.vrhel@artifex.com> | 2013-05-09 22:47:33 -0700 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2013-05-16 19:25:56 +0100 |
commit | 33280868285321bfa3e7e8cad7b77dbfa5ca26f6 (patch) | |
tree | 2ee5dd9e544dd4e62eba81dce989a0e108ce83a1 /winrt | |
parent | 68d059af76c2fe7fac0a0bbfa1a01942883b9d10 (diff) | |
download | mupdf-33280868285321bfa3e7e8cad7b77dbfa5ca26f6.tar.xz |
Fix for some memory leaks as well as a stack growth issue in the text search.
Also made text search a bit more robust in the viewer code.
Diffstat (limited to 'winrt')
-rw-r--r-- | winrt/mupdf_cpp/MainPage.xaml.cpp | 82 | ||||
-rw-r--r-- | winrt/mupdf_cpp/MainPage.xaml.h | 2 | ||||
-rw-r--r-- | winrt/mupdfwinrt/muctx.cpp | 128 | ||||
-rw-r--r-- | winrt/mupdfwinrt/mudocument.cpp | 1 |
4 files changed, 125 insertions, 88 deletions
diff --git a/winrt/mupdf_cpp/MainPage.xaml.cpp b/winrt/mupdf_cpp/MainPage.xaml.cpp index 9fede380..61079103 100644 --- a/winrt/mupdf_cpp/MainPage.xaml.cpp +++ b/winrt/mupdf_cpp/MainPage.xaml.cpp @@ -370,7 +370,7 @@ void mupdf_cpp::MainPage::CleanUp() /* With the ref counting this should not leak */ if (m_page_link_list != nullptr && m_page_link_list->Size > 0) m_page_link_list->Clear(); - if (m_text_list != nullptr && m_text_list->Size > 0) + if (m_text_list->Size > 0) m_text_list->Clear(); if (m_linkset != nullptr && m_linkset->Size > 0) m_linkset->Clear(); @@ -617,14 +617,16 @@ void mupdf_cpp::MainPage::OpenDocument(StorageFile^ file) }, task_continuation_context::use_current()); } - void mupdf_cpp::MainPage::RenderRange(int curr_page) { /* Render +/- the look ahead from where we are if blank page is present */ spatial_info_t spatial_info = InitSpatial(1); bool curr_page_rendered = true; + int range = LOOK_AHEAD; assert(IsMainThread()); + if (m_flip_from_searchlink) + range = 0; for (int k = curr_page - LOOK_AHEAD; k <= curr_page + LOOK_AHEAD; k++) { if (k >= 0 && k < m_num_pages) @@ -644,6 +646,12 @@ void mupdf_cpp::MainPage::RenderRange(int curr_page) { if (k == curr_page && this->m_links_on) AddLinkCanvas(); + if (k == curr_page && this->m_text_list->Size > 0 && + m_flip_from_searchlink) + { + AddTextCanvas(); + m_flip_from_searchlink = false; + } },task_continuation_context::use_current()); } else @@ -659,6 +667,11 @@ void mupdf_cpp::MainPage::RenderRange(int curr_page) m_currpage = curr_page; if (this->m_links_on && !curr_page_rendered) AddLinkCanvas(); + if (this->m_text_list->Size > 0 && !curr_page_rendered && m_flip_from_searchlink) + { + AddTextCanvas(); + m_flip_from_searchlink = false; + } } void mupdf_cpp::MainPage::Slider_ValueChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::Primitives::RangeBaseValueChangedEventArgs^ e) @@ -703,19 +716,14 @@ void mupdf_cpp::MainPage::FlipView_SelectionChanged(Object^ sender, SelectionCha { int pos = this->m_curr_flipView->SelectedIndex; - m_update_flip = true; - if (xaml_PageSlider->IsEnabled) - { - xaml_PageSlider->Value = pos; - } if (pos >= 0) { - if (m_flip_from_searchlink) + m_update_flip = true; + if (xaml_PageSlider->IsEnabled) { - m_flip_from_searchlink = false; - return; - } - else if (m_sliderchange) + xaml_PageSlider->Value = pos; + } + if (m_sliderchange) { m_sliderchange = false; return; @@ -776,15 +784,20 @@ void mupdf_cpp::MainPage::Searcher(Platform::Object^ sender, Windows::UI::Xaml:: } } +void mupdf_cpp::MainPage::ClearTextSearch() +{ + /* Clear out any old search result */ + if (m_text_list->Size > 0) + m_text_list->Clear(); +} + void mupdf_cpp::MainPage::ShowSearchResults(SearchResult_t result) { int height, width; int old_page = this->m_currpage; int new_page = result.page_num; - /* Clear out any old search result */ - if (m_text_list != nullptr && m_text_list->Size > 0) - m_text_list->Clear(); + ClearTextSearch(); /* Compute any scalings */ Point screenSize; @@ -821,16 +834,18 @@ void mupdf_cpp::MainPage::ShowSearchResults(SearchResult_t result) this->m_docPages->SetAt(old_page, doc_page); m_page_update = false; - /* Now get the upcoming page */ - auto doc_page_new = this->m_docPages->GetAt(new_page); - doc_page_new->TextBox = m_text_list; - m_page_update = true; - this->m_docPages->SetAt(new_page, doc_page_new); - m_page_update = false; - /* Go ahead and set our doc item to this in the vertical and horizontal view */ m_searchpage = new_page; - this->m_curr_flipView->SelectedIndex = new_page; + m_flip_from_searchlink = true; + + if (old_page == new_page) + { + FlipView_SelectionChanged(nullptr, nullptr); + } + else + { + this->m_curr_flipView->SelectedIndex = new_page; + } return; } @@ -882,6 +897,21 @@ void mupdf_cpp::MainPage::ResetSearch(void) #endif } +void mupdf_cpp::MainPage::AddTextCanvas() +{ + /* Go ahead and set our doc item to this in the vertical and horizontal view */ + auto doc_page = this->m_docPages->GetAt(m_currpage); + assert(doc_page->Content == FULL_RESOLUTION); + if (doc_page->Content == FULL_RESOLUTION) // We should not be doing links for thumbnails + { + doc_page->TextBox = m_text_list; + m_page_update = true; + this->m_docPages->SetAt(m_currpage, doc_page); + m_page_update = false; + } + this->m_search_active = false; +} + void mupdf_cpp::MainPage::SearchInDirection(int dir, String^ textToFind) { cancellation_token_source cts; @@ -958,7 +988,6 @@ void mupdf_cpp::MainPage::SearchInDirection(int dir, String^ textToFind) // xaml_Progress->Opacity = 0.0; this->ShowSearchResults(the_result); } - this->m_search_active = false; }, task_continuation_context::use_current()); } @@ -969,7 +998,6 @@ void mupdf_cpp::MainPage::GridSizeChanged() int width = this->ActualWidth; FlipView^ old_flip = m_curr_flipView; - if (TopAppBar1->IsOpen) { UpdateAppBarButtonViewState(); @@ -1132,9 +1160,7 @@ void mupdf_cpp::MainPage::AddLinkCanvas() if (doc_page->Content == FULL_RESOLUTION) // We should not be doing links for thumbnails { doc_page->LinkBox = m_page_link_list->GetAt(m_currpage); - m_page_update = true; this->m_docPages->SetAt(m_currpage, doc_page); - m_page_update = false; } } } @@ -1298,5 +1324,3 @@ void mupdf_cpp::MainPage::ScrollChanged(Platform::Object^ sender, } } - - diff --git a/winrt/mupdf_cpp/MainPage.xaml.h b/winrt/mupdf_cpp/MainPage.xaml.h index 257d9d4d..562ef704 100644 --- a/winrt/mupdf_cpp/MainPage.xaml.h +++ b/winrt/mupdf_cpp/MainPage.xaml.h @@ -124,6 +124,8 @@ namespace mupdf_cpp void CancelSearch(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e); void SearchInDirection(int dir, String^ textToFind); void ShowSearchResults(SearchResult_t result); + void ClearTextSearch(); + void AddTextCanvas(); void GridSizeChanged(); void UpDatePageSizes(); void ShowThumbnail(); diff --git a/winrt/mupdfwinrt/muctx.cpp b/winrt/mupdfwinrt/muctx.cpp index e3d56a0f..af1255ff 100644 --- a/winrt/mupdfwinrt/muctx.cpp +++ b/winrt/mupdfwinrt/muctx.cpp @@ -253,7 +253,7 @@ void muctx::FlattenOutline(fz_outline *outline, int level, int muctx::GetContents(sh_vector_content contents_vec) { fz_outline *root = NULL; - fz_context *ctx_clone; + fz_context *ctx_clone = NULL; int has_content = 0; if (mu_cookie->abort == 1) @@ -280,18 +280,20 @@ int muctx::GetContents(sh_vector_content contents_vec) } fz_catch(ctx_clone) { + fz_free_context(ctx_clone); return E_FAIL; } + fz_free_context(ctx_clone); return has_content; } int muctx::GetTextSearch(int page_num, char* needle, sh_vector_text texts_vec) { - fz_page *page; - fz_text_sheet *sheet; - fz_device *dev; - fz_context *ctx_clone; - fz_text_page *text; + fz_page *page = NULL; + fz_text_sheet *sheet = NULL; + fz_device *dev = NULL; + fz_context *ctx_clone = NULL; + fz_text_page *text = NULL; int hit_count = 0; int k; @@ -338,20 +340,26 @@ int muctx::GetTextSearch(int page_num, char* needle, sh_vector_text texts_vec) { fz_free_text_sheet(ctx_clone, sheet); } + if (text != NULL) + { + fz_free_text_page(ctx_clone, text); + } } fz_catch(ctx_clone) { - return E_FAIL; + fz_free_context(ctx_clone); + return E_FAIL; } + fz_free_context(ctx_clone); return hit_count; } /* Get the links and pack into a smart pointer structure */ int muctx::GetLinks(int page_num, sh_vector_link links_vec) { - fz_page *page; - fz_link *links; - fz_context *ctx_clone; + fz_page *page = NULL; + fz_link *links = NULL; + fz_context *ctx_clone = NULL; int k = 0; int num_links = 0; @@ -368,45 +376,45 @@ int muctx::GetLinks(int page_num, sh_vector_link links_vec) links = fz_load_links(mu_doc, page); fz_link *curr_link = links; - if (curr_link == NULL) - return num_links; - - /* Get our smart pointer structure filled */ - while (curr_link != NULL) + if (curr_link != NULL) { - fz_rect curr_rect = curr_link->rect; - sh_link link(new document_link_t()); - - link->upper_left.X = curr_rect.x0; - link->upper_left.Y = curr_rect.y0; - link->lower_right.X = curr_rect.x1; - link->lower_right.Y = curr_rect.y1; - - switch (curr_link->dest.kind) + /* Get our smart pointer structure filled */ + while (curr_link != NULL) { - case FZ_LINK_GOTO: - - link->type = LINK_GOTO; - link->page_num = curr_link->dest.ld.gotor.page; - break; - - case FZ_LINK_URI: - { - int lenstr = strlen(curr_link->dest.ld.uri.uri); - std::unique_ptr<char[]> uri(new char[lenstr + 1]); - strcpy_s(uri.get(), lenstr + 1, curr_link->dest.ld.uri.uri); - link->uri.swap(uri); - link->type = LINK_URI; - break; + fz_rect curr_rect = curr_link->rect; + sh_link link(new document_link_t()); + + link->upper_left.X = curr_rect.x0; + link->upper_left.Y = curr_rect.y0; + link->lower_right.X = curr_rect.x1; + link->lower_right.Y = curr_rect.y1; + + switch (curr_link->dest.kind) + { + case FZ_LINK_GOTO: + + link->type = LINK_GOTO; + link->page_num = curr_link->dest.ld.gotor.page; + break; + + case FZ_LINK_URI: + { + int lenstr = strlen(curr_link->dest.ld.uri.uri); + std::unique_ptr<char[]> uri(new char[lenstr + 1]); + strcpy_s(uri.get(), lenstr + 1, curr_link->dest.ld.uri.uri); + link->uri.swap(uri); + link->type = LINK_URI; + break; + } + + default: + link->type = NOT_SET; + + } + links_vec->push_back(link); + curr_link = curr_link->next; + num_links += 1; } - - default: - link->type = NOT_SET; - - } - links_vec->push_back(link); - curr_link = curr_link->next; - num_links += 1; } } fz_always(ctx_clone) @@ -422,8 +430,10 @@ int muctx::GetLinks(int page_num, sh_vector_link links_vec) } fz_catch(ctx_clone) { + fz_free_context(ctx_clone); return E_FAIL; } + fz_free_context(ctx_clone); return num_links; } @@ -431,12 +441,12 @@ int muctx::GetLinks(int page_num, sh_vector_link links_vec) HRESULT muctx::RenderPage(int page_num, int width, int height, unsigned char *bmp_data) { - fz_device *dev; - fz_pixmap *pix; - fz_page *page; + fz_device *dev = NULL; + fz_pixmap *pix = NULL; + fz_page *page = NULL; fz_matrix ctm, *pctm = &ctm; Point page_size; - fz_context *ctx_clone; + fz_context *ctx_clone = NULL; if (mu_cookie->abort == 1) return S_OK; @@ -478,24 +488,23 @@ HRESULT muctx::RenderPage(int page_num, int width, int height, } fz_catch(ctx_clone) { + fz_free_context(ctx_clone); return E_FAIL; } fz_free_context(ctx_clone); - return S_OK; } String^ muctx::GetHTML(int page_num) { - fz_output *out; - fz_rect bounds; - fz_device *dev; - fz_page *page; - fz_text_sheet *sheet; - fz_text_page *text; - fz_context *ctx_clone; - fz_buffer *buf; + fz_output *out = NULL; + fz_device *dev = NULL; + fz_page *page = NULL; + fz_text_sheet *sheet = NULL; + fz_text_page *text = NULL; + fz_context *ctx_clone = NULL; + fz_buffer *buf = NULL; String^ html; if (mu_cookie->abort == 1) @@ -540,6 +549,7 @@ String^ muctx::GetHTML(int page_num) } fz_catch(ctx_clone) { + fz_free_context(ctx_clone); return nullptr; } diff --git a/winrt/mupdfwinrt/mudocument.cpp b/winrt/mupdfwinrt/mudocument.cpp index c38c2096..bc21a994 100644 --- a/winrt/mupdfwinrt/mudocument.cpp +++ b/winrt/mupdfwinrt/mudocument.cpp @@ -191,6 +191,7 @@ int mudocument::ComputeTextSearch(String^ text, int page_num) new_link->Type = TEXTBOX; this->textsearch->Append(new_link); } + delete []text_char; return num_items; } |