Solved

Printing with MFC sometime result in missing parts

Posted on 2010-09-17
12
389 Views
Last Modified: 2013-11-20
I am writing a program in MFC that prints very large drawings. I am using standard printing methods, and uses the same drawing code for screen and for printing. Most of the time this works fine, but with very large drawings parts og my print is missing. The error never occurs on the screen. Missing parts appears to be random, and I cannot reproduce the problem. It happens also when using other printers, like printing to pdf. What could I be doing wrong?
0
Comment
Question by:Ruskialt
  • 7
  • 5
12 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33705278
You need to show the code.
You need to explain more. "standard printing methods" says nothing. I can guess that you use GDI (or an MFC wrapper) to draw an image. If it is so, probably, there is a GDI leak in the drawing procedure. For example, a bitmap forgotten in the device context.
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33705406
I have created a Doc/View project in VS6, and do all my drawing is in OnDraw(CDC* pDC). I use the standard printing functionality built into the template that creates the doc/view project. So I don't know what code to show, as I cannot point to anything special.

Is it possible there is a limit to how much data can be sent to the printer?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33705479
>>Is it possible there is a limit to how much data can be sent to the printer?
I don't think so. How big is the image you are drawing? If it's too big for the printing, it is for the screen view drawing too. Do you create an internal bitmap? How big is it?

Usually the problem is in OnDraw(CDC* pDC) method. Check that you don't have a GDI leak there. If you say that this is a random bug, it's most likely a leak.
Beside OnDraw you use other methods: OnPreparePrinting, OnBeginPrinting, OnPrepareDC?
There is a leak/s in one of these methods.


0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33705494
Here is an article about the MFC printing (VS6):
http://www.dotnetheaven.com/Uploadfile/mahesh/MFCPrinting05242005051835AM/MFCPrinting.aspx
There is a code attached too.
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33705504
How can I check for leaks, any typical GDI leaks/pitfalls?
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33706915
If you have Bound Checker, it will show everything.
You can simply check GDI objects in Task Manager, so you will see the counter for your process.
There is a special tool GDIView (?): http://www.nirsoft.net/utils/gdi_handles.html

You can verify your code:
1. If you cal GetDC, you need to call ReleaseDC when you already don't need this DC.
2. If you call CreateDC, CreateCompatibleDC, you call DeleteDC in the end.
3. If you create a bitmap or a font, delete them.
4. If you select a bitmap,a font in a DC, you save the old GDI object and restore it in the end.

The last item is very important. Almost always the leak is there. The DC object is not deleted if it contains a bitmap or a font. So if you have:
HDC hDC = GetDC(hWnd);
HDC hMemDC = CreateCompatibleBitmap(hDC);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, m_hBitmap);

do not forget:
SelectObject(hMemDC, hOldBitmap);
DeleteDC(hMemDC);
ReleaseDC(hWnd, hDC);
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Author Comment

by:Ruskialt
ID: 33732313
I got notified on inactive question. I am still working with your suggestions. I found no leaks in task managers gdi object count, does this mean that 1, 2 and 3 is no problem? (Still searching my code for violations to all of this) What is Bound checker?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33734695
It's a commercial product. Use Google. The price is about 1000$.

But... it's really inactive question. You say that you used a task manager and didn't find any leak with the GDI counter. Please do not think that it's sarcasm, but it's not serious. If you really tried that, you saw that this counter changes all the time. Probably its value is about 100-200. Let's say before this problematic print operation the value was 154. When the printing finished it's 154 again. If it is so, your application is perfect and you can be proud and show the printing code here.  
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33734769
You can use GDIView: http://www.nirsoft.net/utils/gdi_handles.html
It's not Bound Checker, but it will detect the leaks.
0
 
LVL 2

Author Closing Comment

by:Ruskialt
ID: 33763857
Brilliant thinking
0
 
LVL 2

Author Comment

by:Ruskialt
ID: 33763867
Thanks for all your help. I am still digging through my codes looking for any of your suggestions. It is a large project, and I have other tasks at work to, so progress on this is a little slow. I have found some leaks, but have yet to conclude that this fixes the printing problem. However I want to close this and dedicate my points to excellent answers and suggestions.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33765099
Thank you too.
Good luck.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now