Home > Software engineering >  Amateur programming to the experts for help
Amateur programming to the experts for help

Time:09-18

With Visual Studio 2012 made up a string display function, the program runs in the call this function when the error will appear after thousands of times:
Thread 0 x1ca0 have withdrawn, the return value is zero (0 x0),
Thread 0 x2598 have withdrawn, the return value is zero (0 x0),
0 x0011e26c first chance exception (in KSD. Exe) : 0 xc0000005: access conflict while reading position 0 x00000000,
Debugging shows that the problem was with statements CFont * pFont=pDC - & gt; SelectObject (& amp; The font); Here,
Hard work a few hours not solve,
Please expert advice!
CSDN new posts for the first time, please forgive me,

Void CksdView: : ShowCString (int index, int irow, int icol, cstrings string, cstrings see
{
/* displays a known number
The index, plate number, 0.
Irow, icol rows and columns
String,
See, the color options: blue (implicit), red.
Example: see colour="blue";//or see colour="red"
ShowNumber (index, irow icol, STR, see colour;
*/

Cstrings STR.
Int ix, iy, k;
Int ired, igreen iblue;

Ired=0;
Igreen=0;
Iblue=255;//implied color is blue
If (see colour=="red")
{ired=255;
Igreen=0;
Iblue=0; }
//strin=string;
Ix rsize=* (200 + index1080 icol + 90-90);
Iy rsize=* (200 + 92 * irow - 92);

The CDC * pDC=GetDC ();//device context for painting
CFont font;//create a font object
The font. CreateFont (60,0,0,0, FW_MEDIUM, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
CFont * pFont=pDC - & gt; SelectObject (& amp; The font);
CFont * pOldFont=pDC - & gt; SelectObject (& amp; The font);//for device context
PDC - & gt; SelectObject (& amp; The font);//for equipment environment
PDC - & gt; SetTextColor (RGB (ired, igreen iblue));//set the text color
PDC - & gt; TextOut (ix, iy, string);//output!!!!!!


The font. The DeleteObject ();
//pDC - & gt; SelectStockObject (DEVICE_DEFAULT_FONT);//select the font//test the add

}

CodePudding user response:

Should be a deadlock in GDI resources leak
 
The CDC * pDC=GetDC ();//device context for painting
CFont font;//create a font object
The font. CreateFont (60,0,0,0, FW_MEDIUM, FALSE, FALSE, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, "Arial");
CFont * pOldFont=pDC - & gt; SelectObject (& amp; The font);//for device context
PDC - & gt; SelectObject (& amp; The font);//for equipment environment
PDC - & gt; SetTextColor (RGB (ired, igreen iblue));//set the text color
PDC - & gt; TextOut (ix, iy, string);//output!!!!!!

PDC - & gt; SelectObject (pOldFont);//resume font
ReleaseDC ();//release DC

CodePudding user response:

CFont * pFont=pDC - & gt; SelectObject (& amp; The font);
CFont * pOldFont=pDC - & gt; SelectObject (& amp; The font);//for device context
pDC - & gt; SelectObject (& amp; The font);//for equipment environment
Don't red!

CodePudding user response:

Collapsed in the pop-up dialog box, press the corresponding button to enter debugging press Alt + 7 key to view the Call Stack, namely "the Call Stack" from the inside to the following out of from the inner to outer function Call history, double-click a row to the cursor to the Call of the source code or assembly instruction, don't understand when double click on the next line, until we can read ,

Search "GDI leak detection"

CodePudding user response:

0 x0011e26c first chance exception (in KSD. Exe) : 0 xc0000005: access conflict while reading position 0 x00000000,
Debugging shows that the problem was with statements CFont * pFont=pDC - & gt; SelectObject (& amp; The font); Here,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Should be a pDC pointer is NULL,

CodePudding user response:

GetDC () must correspond to a ReleaseDC (... ) statement

CodePudding user response:

From the void CksdView: : ShowCString as you can see, this is a class member function,
In this way, some cost resources can be set to a class member, such as CFont * pFont, CDC * pDC
These only in the appropriate position initialization time, repeated use, not every time, new program at the end of the release, can speed up the efficiency, reduce the difficulty of resource management,
  • Related