CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Location
    india/chennai
    Posts
    29

    need help using thread and socket

    hi
    i have developed a server socket program which keeps on sending message. i have created a seperate thread using afxbeginthread(SendMsg,this) (this points to the CSocket derived class). im using carchive. the problem is when the thread tries to send messaage i get a debug assertion error.
    i dont know what is the cause of the problem.

    UINT SendMS(LPVOID lp)
    {
    CServerSock* pSock=(CServerSock*)lp;
    do
    {
    pSock->SendStatus();// here i get error

    Sleep(1000);
    }while(pSock->m_pDlg->m_sta);
    return 1;
    }


    void CServerSock::SendStatus()
    {
    m_pDlg->Serialize(*m_pArOut);
    m_pArOut->Flush();

    }

    when i press retry i goes to base class

    void CAsyncSocket::AssertValid() const
    {
    CObject::AssertValid();
    ASSERT(m_hSocket == INVALID_SOCKET || CAsyncSocket::FromHandle(m_hSocket) != NULL);
    }


    outside the thread im able to send the message. i would be happy if u can help me with this problem
    regards
    aswin

  2. #2
    Join Date
    Feb 2002
    Posts
    3,788
    Code:
    CServerSock* pSock=(CServerSock*)lp;
    do NOT pass pointers to MFC objects across threads! NEVER!
    pass the m_hWnd's of the objects instead.
    see this FAQ and this thread.

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Post the code the calls the API to send messages. One solution is AfxGetMainWnd().

    Kuphryn

  4. #4
    Join Date
    Aug 2000
    Location
    Scotland
    Posts
    147
    The code in the thread will be running in a different 'scope' from the MFC object you created, and therefore the pointer is invalid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured