Click here to Skip to main content
15,914,074 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Directory Date/Time Pin
Atlence2-Apr-05 12:55
Atlence2-Apr-05 12:55 
GeneralRe: Directory Date/Time Pin
Toni7829-Apr-05 13:30
Toni7829-Apr-05 13:30 
GeneralRe: Directory Date/Time Pin
Toni789-May-05 6:42
Toni789-May-05 6:42 
GeneralDialog Problem Pin
Kunal Shetye18-Jul-04 2:44
Kunal Shetye18-Jul-04 2:44 
GeneralRe: Dialog Problem Pin
Alexander Wiseman18-Jul-04 3:22
Alexander Wiseman18-Jul-04 3:22 
GeneralRe: Dialog Problem Pin
Kunal Shetye18-Jul-04 8:27
Kunal Shetye18-Jul-04 8:27 
GeneralRe: Dialog Problem Pin
Bob Stanneveld18-Jul-04 3:24
Bob Stanneveld18-Jul-04 3:24 
QuestionDevice Contexts usage? Pin
0v3rloader17-Jul-04 23:02
0v3rloader17-Jul-04 23:02 
Hello,

My question today has to do with how you should implement DCs. I am having some debug assertions caused by an apparent DC misusage and would therefore like to know if there is anything wrong with my implementation.

Would you say it is alright to have a global pointer/object (in, say, a custom control's class) for a DC which is used by any painting (graphical output) functions. For example, consider the following:

class CustomControl : public ...
{
private:
	CDC *pDC;
.
:
.
};
CustomControl::OutputLine(char *szStr)
{
	// initialise DC
	BeginOutput();
	// output is done now
	.
	:	// this function uses <code>DCHandle</code> in all the GDI calls
	.
	// terminate DC
	EndOutput();
}
CustomControl::OnPaint(LPPAINTSTRUCT lpPaint)
{
	BeginPaint(lpPaint);	// MFC call
	BeginOutput(lpPaint->hdc);	// init our DC
	.
	:
	.
	EndOutput();			// terminate our objects
	EndPaint(lpPaint);		// MFC call
}
CustomControl::BeginOutput(HDC hdc /*= NULL */)
{
	if(!DCHandle)
	{
		// attach DC
		DCHandle = new CDC;
		ASSERT(DCHandle != NULL);
		// are we supposed to get us another DC?
		if(!hdc)
			DCHandle -> Attach(GetDC() -> GetSafeHdc());
		else	// apparently not (we were given one)
			DCHandle -> Attach(hdc);
		// perform viewport inits
		// etc
		.
		:
		.
}
CustomControl::EndOutput()
{
	// protection:
	ASSERT(DCHandle != NULL);
	// release the DC
	ReleaseDC(DCHandle);
	DCHandle->Detach();

	// delete the object
	delete DCHandle;
	DCHandle = NULL;	// null it!
}


Now imagine a worker thread running ,which invokes OutputLine, above defined, multiple times per second. The debug assertion happens everytime the control has to repaint (OnPaint invoked), whilst it is outputting.

Any ideas on what is happening here and on how to solve this?

Thank you very much.

David


[EDIT:] By the way, this particular problem is occurring inside a worker thread, which is not derived from CWinThread but interacts with several controls.
AnswerRe: Device Contexts usage? Pin
Jörgen Sigvardsson18-Jul-04 0:07
Jörgen Sigvardsson18-Jul-04 0:07 
GeneralRe: Device Contexts usage? Pin
0v3rloader18-Jul-04 0:27
0v3rloader18-Jul-04 0:27 
GeneralRe: Device Contexts usage? Pin
Jörgen Sigvardsson18-Jul-04 0:35
Jörgen Sigvardsson18-Jul-04 0:35 
Generaldata mining Pin
Yulianto.17-Jul-04 22:23
Yulianto.17-Jul-04 22:23 
GeneralShellExecute Question Pin
otrcomm17-Jul-04 21:42
otrcomm17-Jul-04 21:42 
GeneralRe: ShellExecute Question Pin
vcplusplus18-Jul-04 2:26
vcplusplus18-Jul-04 2:26 
GeneralRe: ShellExecute Question Pin
User 665818-Jul-04 7:53
User 665818-Jul-04 7:53 
GeneralRe: ShellExecute Question Pin
otrcomm18-Jul-04 8:32
otrcomm18-Jul-04 8:32 
GeneralSlider Pin
smack_2k217-Jul-04 21:34
smack_2k217-Jul-04 21:34 
Generalhooking text from another app Pin
swiftrhett17-Jul-04 20:54
swiftrhett17-Jul-04 20:54 
GeneralRe: hooking text from another app Pin
Alexander Wiseman18-Jul-04 3:26
Alexander Wiseman18-Jul-04 3:26 
GeneralRe: hooking text from another app Pin
swiftrhett18-Jul-04 22:20
swiftrhett18-Jul-04 22:20 
GeneralRe: hooking text from another app Pin
Alexander Wiseman19-Jul-04 16:21
Alexander Wiseman19-Jul-04 16:21 
GeneralWM_GETTEXT will not work across process boundaries Pin
bikram singh18-Jul-04 3:42
bikram singh18-Jul-04 3:42 
GeneralDetecting Win32 memory leaks in VS6 IDE Pin
Hesham Amin17-Jul-04 19:47
Hesham Amin17-Jul-04 19:47 
GeneralRe: Detecting Win32 memory leaks in VS6 IDE Pin
Johan Rosengren17-Jul-04 20:35
Johan Rosengren17-Jul-04 20:35 
GeneralRe: Detecting Win32 memory leaks in VS6 IDE Pin
Michael Dunn18-Jul-04 5:14
sitebuilderMichael Dunn18-Jul-04 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.