Print Page | Close Window

Memory leak drawing RichTextBox to Canvas - SOLVED

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3918
Printed Date: 30 Apr 24 at 12:52AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Memory leak drawing RichTextBox to Canvas - SOLVED
Posted By: Sopracenery
Subject: Memory leak drawing RichTextBox to Canvas - SOLVED
Date Posted: 21 May 21 at 9:44PM
Hi all,

I have a memory leak using 18.11 when drawing content of a RichTextBox via Canvas to a new PDF doc.
No problem with the first 15 pages. But reaching 20 or more pages the library on windows needs more and more time for each page and above 25-30 pages the system fails with Out Of Memory.

Here are the commands I am using:

fr.hdc = GetCanvasDC  // get a canvas device context from the library and set it into the format range structure fr

SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr)    // Send text from a RichTextBox RTF to DC in fr

LoadFromCanvasDC                1.422          1 


fr.hdc = GetCanvasDC

SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr) 

NewPageFromCanvasDC         0.203          2 


fr.hdc = GetCanvasDC

SendMessage(RTF.hWnd, EM_FORMATRANGE, True, fr) 

...


Repeat every page: (GetCanvasDC + SendMessage between every line not listed here)


Method                  Time in seconds          Page

============================================

NewPageFromCanvasDC         0.031          3 

NewPageFromCanvasDC         0.031          4 

NewPageFromCanvasDC         0.023          5 

NewPageFromCanvasDC         0.031          6 

NewPageFromCanvasDC         0.031          7 

NewPageFromCanvasDC         0.039          8 

NewPageFromCanvasDC         0.070          9 

NewPageFromCanvasDC         0.094          10 

NewPageFromCanvasDC         0.172          11 

NewPageFromCanvasDC         0.305          12 

NewPageFromCanvasDC         0.633          13 

NewPageFromCanvasDC         1.102          14 

NewPageFromCanvasDC         2.391          15 

NewPageFromCanvasDC         4.344          16 

NewPageFromCanvasDC         9.547          17 

NewPageFromCanvasDC         17.383         18 

NewPageFromCanvasDC         38.266         19 

NewPageFromCanvasDC         69.586         20 


Can anybody help?
Martin

Some more hints:
I guess that each GetCanvasDC allocates memory and does not release it after NewPageFromCanvasDC.
Calling CoTaskMemFree in ole32 with the pointer of fr.hdc has no effect.
Only RemoveDocument or MergeDocument has effect in releasing memory.




Replies:
Posted By: mLipok
Date Posted: 26 May 21 at 7:45AM
Question.
Do this works well when you try it with single page ?
I mean do you have properly created PDF ?



-------------
Here you can find description how to test my examples:
http://www.quickpdf.org/forum/forum_posts.asp?TID=2932&PID=12600&title=drawcapturedpagematrix-matrix-howto#12600


Posted By: Sopracenery
Date Posted: 26 May 21 at 11:16PM
Hi Michał,

Sorry, I do not understand what you mean with „single page“. If I produce only one page then everything is fine. I can close the PDF and it is OK. My program is doing this now for months on thousands of documents. Normally producing 1-5 pages. But now that some documents need more than 20 pages I get this new error.

Martin


Posted By: mLipok
Date Posted: 27 May 21 at 8:01AM
you said: "No problem with the first 15 pages"
What if your source document have only 1 page ?



-------------
Here you can find description how to test my examples:
http://www.quickpdf.org/forum/forum_posts.asp?TID=2932&PID=12600&title=drawcapturedpagematrix-matrix-howto#12600


Posted By: Sopracenery
Date Posted: 27 May 21 at 9:13AM
If the source document in the RichTextBox has only one page then the output in PDF is only one page and anything is ok.

If I repeat sending this one page 20 times to to same PDF to generate more payload I get the same memory leak.


Posted By: Sopracenery
Date Posted: 28 May 21 at 7:34PM
After seaching in the completely wrong direction I finally found the reason why the library goes into trouble.

It is the option for font embeding in NewPageFromCanvasDC(dpi, Options)
It can have the following values:

Options-1 = Convert the drawing commands to a single image using GDI+
0 = Process the drawing commands as vector graphics, fonts are not embedded
1 = Process the drawing commands as vector graphics, fonts are embedded but not compressed
2 = Process the drawing commands as vector graphics, fonts are embedded and compressed
3 = Process the drawing commands as vector graphics, fonts not in the no embed font list are embedded and compressed
4 = Same as 3 but fonts already added during previous calls to this function or the  https://www.debenu.com/docs/pdf_library_reference/LoadFromCanvasDC.php" rel="nofollow - LoadFromCanvasDC  function are reused
and I have chosen FontOption=4 which is the only one which leads into trouble. All others are working.
So the library probably has a bug with option 4.

Even when I do nothing else than repeated

GetCanvasDC

NewPageFromCanvasDC(dpi, 4)


without printing anything to the canvas the memory runs out.

Now I will use option 3 instead.





Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk