C++/VB - A generic error occurred in GDI+

Asked By RedLars
07-Nov-09 01:45 PM
Hello,

In a mixed-mode application (MFC and .NET version 1.1) an
System.Runtime.InteropServices.ExternalException has been thrown with
the message "A generic error occurred in GDI+".

Viewing the call stack of the crash dump, it starts with this C++
code;

void View.Draw()
{
CDC* pDC = new CDC();
System::Drawing::Graphics* pGraphics =
System::Drawing::Graphics::FromHdc(pDC.GetSafeHdc());
graph->Print(pGraphics);
}

Then inside the Print method the crash occurs inside this loop, on
iteration 6 out of 14. The values of startPoint is x=422 and y=5103
and stopPoint is x=442 and y = 5103.

for (int index = 1; index < noTicks; index++)
{
graphics.DrawLine(pen, StartPoint, StopPoint);
}

The input parameters to the DrawLine method seem to be valid. It seems
very strange that the graphics object would suddenly become invalid
after working for 5 iterations and also had been able to draw bits
before the loop.

The only property changes made to the graphics object is to set
TextRenderingHint to AntiAlias.

The application in question is using roughly 800 handles at the time
of the crash. The application had been running for more than 3 days
when it happend.

This part of the application has several years of runtime without this
problem ever occuring. So this appear to be a rare problem.

I have seen other people experience similar type of error message but
then it appear to be related to using bitmaps and metafile. This is
not the case in this situation.

Are there any known steps or advice on how to debug such a generic gdi
issue?
System.Drawing.SafeNativeMethods.GdipDrawLineI
(1)
System.Drawing.SafeNativeMethods.GetLastError
(1)
System.Drawing.Graphics.CheckErrorStatus
(1)
System.Drawing.Graphics.DrawLine
(1)
RtlGetLastWin32Error
(1)
View.Print
(1)
TextRenderingHint
(1)
LazyMachState
(1)
  RedLars replied to RedLars
07-Nov-09 01:45 PM
Here is a short call stack:

0012ee0c 79238b7d mscorwks!RaiseTheException+0xa0, calling kernel32!
RaiseException
0012ee2c 7c812aeb kernel32!RaiseException+0x53, calling ntdll!
RtlRaiseException
0012ee64 792f0d05 mscorwks!JIT_Throw+0x4d, calling mscorwks!
RaiseTheException
0012eea4 792f0cce mscorwks!JIT_Throw+0x16, calling mscorwks!
LazyMachState::captureState
0012eed0 7b518150 (MethodDesc 0x7b59de60 +0x90
System.Drawing.Graphics.CheckErrorStatus), calling mscorwks!JIT_Throw
0012eee8 7b5904b8  (stub for
System.Drawing.SafeNativeMethods.GetLastError), calling 0732a0f8
0012eef0 7b518117 (MethodDesc 0x7b59de60 +0x57
System.Drawing.Graphics.CheckErrorStatus), calling ntdll!
RtlGetLastWin32Error
0012eef4 7b594810  (stub for
System.Drawing.SafeNativeMethods.GdipDrawLineI), calling 01fc286c
0012eef8 7b51960d (MethodDesc 0x7b59e9e0 +0x7d
System.Drawing.Graphics.DrawLine), calling 7b59480b (MethodDesc
0x7b594810 System.Drawing.SafeNativeMethods.GdipDrawLineI)
0012ef10 7b519617 (MethodDesc 0x7b59e9e0 +0x87
System.Drawing.Graphics.DrawLine), calling 7b59de5b (MethodDesc
0x7b59de60 System.Drawing.Graphics.CheckErrorStatus)
0012ef3c 7b537576 (MethodDesc 0x7b59e9f0 +0x16
System.Drawing.Graphics.DrawLine), calling 7b59e9db (MethodDesc
0x7b59e9e0 System.Drawing.Graphics.DrawLine)
0012ef50 0ef1d63c (MethodDesc 0x1f55650 +0x6d4 View.Print), calling
7b59e9eb (MethodDesc 0x7b59e9f0 System.Drawing.Graphics.DrawLine)
  George replied to RedLars
07-Nov-09 05:13 PM
You are not freeing pDC with delete here, maybe is that.
  RedLars replied to George
08-Nov-09 10:45 AM
Thanks for the feedback.

I agree that pDC must be deleted and I will fix that.

So that will fix a memory leak. I am however not so sure that it will
affect the problem I described above.
help