Thread: CWinThread messing itself up

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    34

    CWinThread messing itself up

    Hello,

    I've been have some trouble lately with a CWinThread. Here's my original declaration:

    Code:
    PortThread[3]= AfxBeginThread(StartCryptoPortThread, &Port[3],THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
    where StartCryptoPortThread is:

    Code:
    UINT C_Control::StartCryptoPortThread( LPVOID pParam )
    {
    	char*	buffer;
    	int		size = 0;
        C_RS_422_Port* pObject = ( C_RS_422_Port*)pParam;
    
        if (pObject == NULL )
    		return 1;   // if pObject is not valid
    	else
    	{
    		//C_Control::GetOutboundMessage(buffer,size);
    		
    		pObject->Send_Crypto_Key(1);    // do something with 'pObject'
    
    		return 0;   // thread completed successfully
    	}
    }
    and Port[3] is an instance of a class. Now, the first time that I use PortThread[3] like so:

    Code:
    SuspendCount=PortThread[PortNumber-1]->ResumeThread();
    Everything works fine and the program and the GUI continue to run. However, upon using it a second time like so:

    Code:
    SuspendCount=PortThread[PortNumber-1]->SuspendThread();
    I get a ridiculous return of 4 million or something. And then, when pointing to ResumeThread() as previously used (when it worked), it returns the same value of 4 million+. I've been able to discern that sometime after the initial point to ResumeThread(), PortThread goes bad and I get values of 0x00000000 for m_hThread where there used to be an address like 0x0000fd4c and I get 0xfeababab for things like m_pMainWnd/m_pActiveWnd where they used to be 0x00000000. Please help me! I've never actually had to use threads before and I don't know how they work!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Run it in the debugger. Set a data break point on m_hThread to occur when it changes to 0. Inspect the call stack when it's triggered to see who or what is corrupting memory.

    gg

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    LowlyIntern, have you followed up on Codeplug's suggestion?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    I've been trying to, but I don't know how to set data break points to trigger when something (a HANDLE in this case) changes. In it's stead I've been trying other things like having the function name where the problem occurs to output but that's been proving difficult. Can someone enlighten me on how to follow through on Codeplug's advice?

  5. #5

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    "The following breakpoint was hit:

    When '0x00BF64A0' changes (4 bytes) in process 'RS_SIMULATOR.exe'"

    How can the application be changing it?! And only that one, too! The first 3 PortThreads are fine!

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The break point hits, example the call stack of each of your threads to see what each of them are doing. One of them is responsible for triggering the break point (by stomping on memory that doesn't belong to it).

    gg

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    34
    Got it, I was breaking a thread that I was supposed to keep going potentially forever. Thanks for your help, though! More clear answer here:

    http://cboard.cprogramming.com/showt...=111094&page=3
    Last edited by LowlyIntern; 01-16-2009 at 08:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. messing around with classes: how to use arrays?
    By eastmus in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2009, 08:26 PM
  2. Retrieving the exit code of CWinThread objects
    By mynickmynick in forum Windows Programming
    Replies: 4
    Last Post: 10-07-2008, 08:10 AM
  3. Windows MFC leaks CWinThread??
    By VirtualAce in forum Windows Programming
    Replies: 2
    Last Post: 12-24-2005, 06:16 AM
  4. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  5. The taskbar hates me messing around!
    By Queatrix in forum C++ Programming
    Replies: 7
    Last Post: 09-12-2005, 07:58 PM