• 15
• 15
• 11
• 9
• 10

# Logical comparison of a DWORD and #define 0xXXXXXXXX

This topic is 1889 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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++)
{
{
m_quit = true;
}
}
for(int i=0;(int)LoadThread_RequestFiles.size();i++)
{
{
m_quit = true;
}
}

##### Share on other sites

== 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?

##### Share on other sites
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.

##### Share on other sites

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:

Recieve:

##### Share on other sites

I did this

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

But the result is the same.

void MyLoadingClass::GetRequestedFiles()
{

for(size_t i=0;i<Mutex_RequestFiles.size();i++)
{
}

Mutex_RequestFiles.clear();										//Clear to prevent duplicates on next call
}

##### Share on other sites
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.

##### Share on other sites

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++)
{
{
m_quit = true;
}

##### Share on other sites

What do you mean exactly by broken and corrupted?

##### Share on other sites

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
{
m_quit = true;
}

##### Share on other sites
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.

Edited by Khatharr