Logical comparison of a DWORD and #define 0xXXXXXXXX

Started by
12 comments, last by L. Spiro 11 years, 3 months ago

Hi

I have a std::vector<DWORD> that I want to loop through and compare each DWORD against a defined value. The problem is that either "&", "&&" or "==" will currupt the vector.

How can I do this check without messing up the vector?


#define StopLoadingThread	0xffffffff
std::vector<DWORD> LoadThread_RequestFiles;


for(int i=0;(int)LoadThread_RequestFiles.size();i++)
{
	if(LoadThread_RequestFiles[i] && StopLoadingThread)			//Fix comparison
	{
		m_quit = true;
	}
}

for(int i=0;(int)LoadThread_RequestFiles.size();i++)
{
	if(LoadThread_RequestFiles[i] == StopLoadingThread)			//Fix comparison
	{
		m_quit = true;
	}
}

Advertisement

== should work fine.

Seeing the word "thread" appears in your code, the corruption is likely being caused by two threads accessing the vector at the same time... Can you explain what your threads are doing, and how you're synchronising them?


for(int i=0;(int)LoadThread_RequestFiles.size();i++)

Ought to be:


for(size_t i = 0; i < LoadThread_RequestFiles.size(); i++)

You'll have to give more details on this apparent vector corruption but as Hodgman has said, it's possibly something to do with your threading.

Well, there are 6 std::vectors<DWORD>, for buffered bi-directional communication. Two for Main thread, Two for Mutex and Two for the Load thread. The Main thread can call a function RequestFile(DWORD) which puts the DWORD value on the "send" vector. Another function (try)enters a mutex and copies from the "main_request" vector to the "mutex_request" vector. The loading thread will then enter the mutex and copy from the "mutex_vector" to the "LoadThread_RequestFiles" vector.

So the Main thread is the only one controlling the two main vectors and the loading thread is the only one controlling the thread_vectors. And there are two vectors in between main and thread can access via mutexes. The vector that gets corrupted is only accessed by the loading thread.

Send:

MainReq_vector -> MutexReq_vector -> ThreadReq_vector

Recieve:

MainCompleted_vector <- MutexCompleted_vector <- ThreadCompleted_vector

I did this


for(size_t i = 0; i < LoadThread_RequestFiles.size(); i++)

But the result is the same.


void MyLoadingClass::GetRequestedFiles()
{
	EnterCriticalSection(&RequestLoadMutex);

	for(size_t i=0;i<Mutex_RequestFiles.size();i++)
	{
		LoadThread_RequestFiles.push_back(Mutex_RequestFiles[i]);		//Retrieves all requests from shared vector to loading thread
	}

	Mutex_RequestFiles.clear();										//Clear to prevent duplicates on next call
	LeaveCriticalSection(&RequestLoadMutex);
}
Threading bugs are evil. Just because variable Foo appears to be clobbered doesn't mean that the problem lies in code that handles Foo.

Check all of your thread synchronization and design. Chances are the problem is someplace else and/or rooted more deeply than just this vector.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

hmmm, I tried to copy the element to a test variable to see if it was the checking. Turns out that the act of copying the DWORD to test broke the vector.... How can that be.


	for(size_t i=0;i<LoadThread_RequestFiles.size();i++)
	{
		DWORD test = LoadThread_RequestFiles[i];			//Breaks LoadThread_RequestFiles
		if(test == StopLoadingThread)
		{
			m_quit = true;
		}

What do you mean exactly by broken and corrupted?

Broken means that prior to


DWORD test = LoadThread_RequestFiles[i];

the vector containes the correct value and has a size of '1'. After this line of code the vector gets a size of '4260741497' and the elements value are just '(...,...,....)'.

I tried using iterator but the same thing happens:


	for(std::vector<DWORD>::iterator it = LoadThread_RequestFiles.begin(); it!= LoadThread_RequestFiles.end(); it++)
	{
		DWORD test = *it;						//does not work
		if(test == StopLoadingThread)			//Fix comparison
		{
			m_quit = true;
		}
Broken means that prior to

DWORD test = LoadThread_RequestFiles[i];

the vector containes the correct value and has a size of '1'. After this line of code the vector gets a size of '4260741497' and the elements value are just '(...,...,....)'.

?

Just a shot in the dark, but could you try cleaning and rebuilding the project? I want to say that I've run into this before but I can't remember what it was. I just vaguely remember stepping through the STL code with the debugger.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement