Problem with Multithreads
-
Hi, I am programming a multithreaded application however I am having lots of difficulties this week. I programmed many multithreaded applications but in this program I am really stucked. There is a main View class (say CMyView). This class have a CTaskManager class, which controls all the threads. The threads are called CTaskThread. Each CTaskThread need to call methods of CTaskManager. Here is the problematic part of the code:
class CTaskManager
{
public:
CTaskManager ();
virtual ~CTaskManager ();
protected:
CString m_sSharedVars [MAX_VARS];...
};
void CTaskManager::ReplaceVariables (CString &str)
{
for (int i = 0 ; i < MAX_VARS ; i++)
{
if (m_sSharedVarNames [i] != m_sSharedVars [i])
{
while (str.Replace (m_sSharedVarNames [i], m_sSharedVars [i]) != 0);
}
}
}When I run the program, it crashes at line
m_sSharedVarNames [i] != m_sSharedVars [i]
. In fact, it crashes whenever it tries to acces the elements of CTaskManager. However, when I remove the definitionCString m_sSharedVars [MAX_VARS];
from the class and write it to the beginning of the CPP file (i.e. make it global), the program works fine. But the problem is that, I have to access other variables of CTaskManager, and I cannot make all of them global. The error message is the following:The intruction at "0x5f47714c" referenced memeory at "0xcdcdcde1". The memory could not be "read". Click on OK to terminate the program Click on Cancel to debug the program
When I click on Cancel and debug the program, it jumps to the following line in AFX.INL_AFX_INLINE CString::operator LPCTSTR() const { return m_pchData; }
The error isUnhandled exception in MyProgram.exe (MFC42D.DLL):0xC0000005:Access Violation
Please HELP. I am stucked and I dont know what to do. I tried to use CCriticalSection to protect the data to be read/written by more than one threads at the same time but it didnt worked. In fact, when I run only one thread, the program crashes again! Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix -
Hi, I am programming a multithreaded application however I am having lots of difficulties this week. I programmed many multithreaded applications but in this program I am really stucked. There is a main View class (say CMyView). This class have a CTaskManager class, which controls all the threads. The threads are called CTaskThread. Each CTaskThread need to call methods of CTaskManager. Here is the problematic part of the code:
class CTaskManager
{
public:
CTaskManager ();
virtual ~CTaskManager ();
protected:
CString m_sSharedVars [MAX_VARS];...
};
void CTaskManager::ReplaceVariables (CString &str)
{
for (int i = 0 ; i < MAX_VARS ; i++)
{
if (m_sSharedVarNames [i] != m_sSharedVars [i])
{
while (str.Replace (m_sSharedVarNames [i], m_sSharedVars [i]) != 0);
}
}
}When I run the program, it crashes at line
m_sSharedVarNames [i] != m_sSharedVars [i]
. In fact, it crashes whenever it tries to acces the elements of CTaskManager. However, when I remove the definitionCString m_sSharedVars [MAX_VARS];
from the class and write it to the beginning of the CPP file (i.e. make it global), the program works fine. But the problem is that, I have to access other variables of CTaskManager, and I cannot make all of them global. The error message is the following:The intruction at "0x5f47714c" referenced memeory at "0xcdcdcde1". The memory could not be "read". Click on OK to terminate the program Click on Cancel to debug the program
When I click on Cancel and debug the program, it jumps to the following line in AFX.INL_AFX_INLINE CString::operator LPCTSTR() const { return m_pchData; }
The error isUnhandled exception in MyProgram.exe (MFC42D.DLL):0xC0000005:Access Violation
Please HELP. I am stucked and I dont know what to do. I tried to use CCriticalSection to protect the data to be read/written by more than one threads at the same time but it didnt worked. In fact, when I run only one thread, the program crashes again! Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrixI create the threads as follows (may help):
bool CTaskManager::LaunchTask (const CString &sTaskName, const CString &sTaskFolder)
{
CTaskObject *pTask = FindTask (sTaskName, sTaskFolder);
if (pTask)
{
if (pTask->m_bRunning)
return true;pTask->m\_bRunning = true; CTaskThread \*pThread = new CTaskThread (); pThread->m\_pTaskObject = pTask; pThread->m\_pTaskManager = this; AddTaskThread (pThread); // adds the thread pointer to a list pThread->CreateThread (); return true; } else return false; return true;
}
Thanks for any helps. Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix
-
Hi, I am programming a multithreaded application however I am having lots of difficulties this week. I programmed many multithreaded applications but in this program I am really stucked. There is a main View class (say CMyView). This class have a CTaskManager class, which controls all the threads. The threads are called CTaskThread. Each CTaskThread need to call methods of CTaskManager. Here is the problematic part of the code:
class CTaskManager
{
public:
CTaskManager ();
virtual ~CTaskManager ();
protected:
CString m_sSharedVars [MAX_VARS];...
};
void CTaskManager::ReplaceVariables (CString &str)
{
for (int i = 0 ; i < MAX_VARS ; i++)
{
if (m_sSharedVarNames [i] != m_sSharedVars [i])
{
while (str.Replace (m_sSharedVarNames [i], m_sSharedVars [i]) != 0);
}
}
}When I run the program, it crashes at line
m_sSharedVarNames [i] != m_sSharedVars [i]
. In fact, it crashes whenever it tries to acces the elements of CTaskManager. However, when I remove the definitionCString m_sSharedVars [MAX_VARS];
from the class and write it to the beginning of the CPP file (i.e. make it global), the program works fine. But the problem is that, I have to access other variables of CTaskManager, and I cannot make all of them global. The error message is the following:The intruction at "0x5f47714c" referenced memeory at "0xcdcdcde1". The memory could not be "read". Click on OK to terminate the program Click on Cancel to debug the program
When I click on Cancel and debug the program, it jumps to the following line in AFX.INL_AFX_INLINE CString::operator LPCTSTR() const { return m_pchData; }
The error isUnhandled exception in MyProgram.exe (MFC42D.DLL):0xC0000005:Access Violation
Please HELP. I am stucked and I dont know what to do. I tried to use CCriticalSection to protect the data to be read/written by more than one threads at the same time but it didnt worked. In fact, when I run only one thread, the program crashes again! Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix -
1.Try init the m_sSharedVars[MAX_VARS] first before operations. 2.Or maybe an error occurs internally of CString in String-Length. I Guess. I would try.
-
Hi, I am programming a multithreaded application however I am having lots of difficulties this week. I programmed many multithreaded applications but in this program I am really stucked. There is a main View class (say CMyView). This class have a CTaskManager class, which controls all the threads. The threads are called CTaskThread. Each CTaskThread need to call methods of CTaskManager. Here is the problematic part of the code:
class CTaskManager
{
public:
CTaskManager ();
virtual ~CTaskManager ();
protected:
CString m_sSharedVars [MAX_VARS];...
};
void CTaskManager::ReplaceVariables (CString &str)
{
for (int i = 0 ; i < MAX_VARS ; i++)
{
if (m_sSharedVarNames [i] != m_sSharedVars [i])
{
while (str.Replace (m_sSharedVarNames [i], m_sSharedVars [i]) != 0);
}
}
}When I run the program, it crashes at line
m_sSharedVarNames [i] != m_sSharedVars [i]
. In fact, it crashes whenever it tries to acces the elements of CTaskManager. However, when I remove the definitionCString m_sSharedVars [MAX_VARS];
from the class and write it to the beginning of the CPP file (i.e. make it global), the program works fine. But the problem is that, I have to access other variables of CTaskManager, and I cannot make all of them global. The error message is the following:The intruction at "0x5f47714c" referenced memeory at "0xcdcdcde1". The memory could not be "read". Click on OK to terminate the program Click on Cancel to debug the program
When I click on Cancel and debug the program, it jumps to the following line in AFX.INL_AFX_INLINE CString::operator LPCTSTR() const { return m_pchData; }
The error isUnhandled exception in MyProgram.exe (MFC42D.DLL):0xC0000005:Access Violation
Please HELP. I am stucked and I dont know what to do. I tried to use CCriticalSection to protect the data to be read/written by more than one threads at the same time but it didnt worked. In fact, when I run only one thread, the program crashes again! Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrixWhat and where is m_sSharedVarNames[i] defined? It looks like something isn't being initialized. Try adding some debug statements or logging to see what's really happening and in what order.
Todd Smith
-
What and where is m_sSharedVarNames[i] defined? It looks like something isn't being initialized. Try adding some debug statements or logging to see what's really happening and in what order.
Todd Smith
Sorry I forgot to show the definition of m_sSharedVarNames. m_sSharedVarNames is defined in the CPP file as a global variable.
CString m_sSharedVarNames [MAX_VARS] = {"", "", "", "", "",
"", "", "", "", "",
"","","","","",
"","","","","",
"","","","",""};The problem occurs when I try to access the member variables of CTaskManager. Accessing sSharedVarNames (which is not a member of the class) is OK, but accessing sSharedVars (which is a public member of the class) is not. I dont know why this occurs. Also, sSharedVars are initialized in the constructor. Thanks for your helps. I am looking forward to hearing your suggestions. :confused: :confused: :confused: Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix
-
Hi, I am programming a multithreaded application however I am having lots of difficulties this week. I programmed many multithreaded applications but in this program I am really stucked. There is a main View class (say CMyView). This class have a CTaskManager class, which controls all the threads. The threads are called CTaskThread. Each CTaskThread need to call methods of CTaskManager. Here is the problematic part of the code:
class CTaskManager
{
public:
CTaskManager ();
virtual ~CTaskManager ();
protected:
CString m_sSharedVars [MAX_VARS];...
};
void CTaskManager::ReplaceVariables (CString &str)
{
for (int i = 0 ; i < MAX_VARS ; i++)
{
if (m_sSharedVarNames [i] != m_sSharedVars [i])
{
while (str.Replace (m_sSharedVarNames [i], m_sSharedVars [i]) != 0);
}
}
}When I run the program, it crashes at line
m_sSharedVarNames [i] != m_sSharedVars [i]
. In fact, it crashes whenever it tries to acces the elements of CTaskManager. However, when I remove the definitionCString m_sSharedVars [MAX_VARS];
from the class and write it to the beginning of the CPP file (i.e. make it global), the program works fine. But the problem is that, I have to access other variables of CTaskManager, and I cannot make all of them global. The error message is the following:The intruction at "0x5f47714c" referenced memeory at "0xcdcdcde1". The memory could not be "read". Click on OK to terminate the program Click on Cancel to debug the program
When I click on Cancel and debug the program, it jumps to the following line in AFX.INL_AFX_INLINE CString::operator LPCTSTR() const { return m_pchData; }
The error isUnhandled exception in MyProgram.exe (MFC42D.DLL):0xC0000005:Access Violation
Please HELP. I am stucked and I dont know what to do. I tried to use CCriticalSection to protect the data to be read/written by more than one threads at the same time but it didnt worked. In fact, when I run only one thread, the program crashes again! Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix0xCDCDCDCD is a dead giveaway of an uninitialized pointer - memory is set to that value right after it's allocated so you can catch just this sort of bug. --Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
-
0xCDCDCDCD is a dead giveaway of an uninitialized pointer - memory is set to that value right after it's allocated so you can catch just this sort of bug. --Mike-- My really out-of-date homepage Buffy's on. Gotta go, bye! Sonork - 100.10414 AcidHelm Big fan of Alyson Hannigan.
Michael Dunn wrote: 0xCDCDCDCD is a dead giveaway of an uninitialized pointer - memory is set to that value right after it's allocated so you can catch just this sort of bug. Hi Mike, That helped a lot. I found that the error is not related to the threads but an uninitizlized variable. In fact, the variable is initialized, but after sometime it somehow becomes invalid. The source code is very long and now I am trying to find the code fragment that changes that pointer. Anyway, THANKS A LOT FOR YOUR HELPS. Happy new year all CP fellows. GOD BLESS CP ;) Kind regards Mustafa Demirhan http://www.macroangel.com Sonork ID 100.9935:zoltrix