Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with Multithreads

Problem with Multithreads

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++comdebuggingperformance
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mustafa Demirhan
    wrote on last edited by
    #1

    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 definition CString 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 is Unhandled 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

    M L T M 4 Replies Last reply
    0
    • M Mustafa Demirhan

      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 definition CString 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 is Unhandled 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

      M Offline
      M Offline
      Mustafa Demirhan
      wrote on last edited by
      #2

      I 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

      1 Reply Last reply
      0
      • M Mustafa Demirhan

        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 definition CString 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 is Unhandled 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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • L Lost User

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Your Code is not clear for uncertain fragments.Perhaps U know the meaning but I don't.Show more Definitions and Implementations for analysis.

          1 Reply Last reply
          0
          • M Mustafa Demirhan

            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 definition CString 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 is Unhandled 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

            T Offline
            T Offline
            Todd Smith
            wrote on last edited by
            #5

            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

            M 1 Reply Last reply
            0
            • T 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

              M Offline
              M Offline
              Mustafa Demirhan
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Mustafa Demirhan

                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 definition CString 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 is Unhandled 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

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #7

                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.

                M 1 Reply Last reply
                0
                • M Michael Dunn

                  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.

                  M Offline
                  M Offline
                  Mustafa Demirhan
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups