Click here to Skip to main content
16,014,860 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMFC + Threading = C2665 Pin
Killiconn19-Mar-09 14:43
Killiconn19-Mar-09 14:43 
AnswerRe: MFC + Threading = C2665 Pin
«_Superman_»19-Mar-09 19:03
professional«_Superman_»19-Mar-09 19:03 
GeneralRe: MFC + Threading = C2665 Pin
Killiconn20-Mar-09 10:50
Killiconn20-Mar-09 10:50 
GeneralRe: MFC + Threading = C2665 Pin
«_Superman_»21-Mar-09 3:34
professional«_Superman_»21-Mar-09 3:34 
GeneralRe: MFC + Threading = C2665 Pin
Killiconn21-Mar-09 10:01
Killiconn21-Mar-09 10:01 
AnswerRe: MFC + Threading = C2665 Pin
_AnsHUMAN_ 19-Mar-09 19:08
_AnsHUMAN_ 19-Mar-09 19:08 
AnswerRe: MFC + Threading = C2665 Pin
Sunil Lanke19-Mar-09 19:09
Sunil Lanke19-Mar-09 19:09 
QuestionAnother Post from Bob about Threads, Pointers and Locks[modified] Pin
BobInNJ19-Mar-09 10:45
BobInNJ19-Mar-09 10:45 
I am currently writing a multi-threaded application using C++ and MFC. I am trying to write some code to keep track of my memory allocations (new) and my memory frees (delete). I have written aclass called MemoryChecker that is suppose to do this. To store the list of pointers that are allocated,
I am using STL which is not thread safe. To make it thread safe, I put a call to WaitForSingleObject
around the body of the function. I would expect to make this thread safe. Inside the member function
deleteItem, there is a static variable. It is initialized to true but later set to false. Before it is set to false, there is an assert statement that checks that the value is still true. However,
that assert statement is failing (sometimes). Therefore, I am thinking my lock is not working properly.

<br />
void<br />
MemoryChecker::deleteItem( void *ptr )<br />
{<br />
    if ( WaitForSingleObject(mutexDeleteHandle, INFINITE) == WAIT_OBJECT_0 ) {<br />
	if ( ptr == 0 ) {<br />
	    ReleaseMutex(mutexDeleteHandle);<br />
	    returneturn;<br />
	}<br />
	static bool go = true;<br />
	bool found = false;<br />
	NewList::iterator it = newList.begin();<br />
	while ( it != newList.end() ) {<br />
		if ( it->ptrValue == ptr ) {<br />
			found = true;<br />
			break;<br />
		}<br />
		it ++;<br />
	}<br />
	if ( found == false )<br />
		found = true;<br />
	else {<br />
		assert( found );<br />
		newList.erase( it );<br />
	}<br />
	assert( go );<br />
	go = false;<br />
	ReleaseMutex(mutexDeleteHandle);<br />
}<br />
}


I should also tell you that I create the lock, in another thread, with the following call:
mutexDeleteHandle = CreateMutex( NULL, FALSE, NULL );

Please note that the second parameter in CreateMutex is to specify who owns the lock and the
lock may not be owned by the thread making the call to WaitForSingleObject.

I am hoping that somebody here can tell me what I am doing wrong.

Thanks

Bob

modified on Thursday, March 19, 2009 6:26 PM

AnswerRe: Another Post from Bob about Threads and Points Pin
Code-o-mat19-Mar-09 12:31
Code-o-mat19-Mar-09 12:31 
GeneralRe: Another Post from Bob about Threads and Points Pin
BobInNJ19-Mar-09 12:39
BobInNJ19-Mar-09 12:39 
GeneralRe: Another Post from Bob about Threads and Points Pin
bulg19-Mar-09 13:39
bulg19-Mar-09 13:39 
GeneralRe: Another Post from Bob about Threads and Points Pin
BobInNJ19-Mar-09 13:58
BobInNJ19-Mar-09 13:58 
QuestionThreads and Pointers Pin
BobInNJ19-Mar-09 7:36
BobInNJ19-Mar-09 7:36 
AnswerRe: Threads and Pointers Pin
led mike19-Mar-09 7:41
led mike19-Mar-09 7:41 
AnswerRe: Threads and Pointers Pin
Eytukan19-Mar-09 7:41
Eytukan19-Mar-09 7:41 
GeneralRe: Threads and Pointers Pin
led mike19-Mar-09 7:58
led mike19-Mar-09 7:58 
GeneralRe: Threads and Pointers Pin
Eytukan19-Mar-09 8:12
Eytukan19-Mar-09 8:12 
AnswerRe: Threads and Pointers Pin
CPallini19-Mar-09 7:46
mveCPallini19-Mar-09 7:46 
GeneralRe: Threads and Pointers Pin
led mike19-Mar-09 9:42
led mike19-Mar-09 9:42 
AnswerRe: Threads and Pointers Pin
cmk19-Mar-09 13:17
cmk19-Mar-09 13:17 
QuestionEdit control disappears from dialog [modified] Pin
rp_suman19-Mar-09 7:07
rp_suman19-Mar-09 7:07 
AnswerRe: Edit control disappears from dialog Pin
Code-o-mat19-Mar-09 7:27
Code-o-mat19-Mar-09 7:27 
QuestionC4244 conversion from 'double' to 'float' warning Pin
penny black19-Mar-09 6:47
penny black19-Mar-09 6:47 
AnswerRe: C4244 conversion from 'double' to 'float' warning Pin
Eytukan19-Mar-09 6:56
Eytukan19-Mar-09 6:56 
QuestionRe: C4244 conversion from 'double' to 'float' warning Pin
penny black19-Mar-09 7:02
penny black19-Mar-09 7:02 

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.