Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I've got a problem in passing a Doc pointer to my thread function.It worked like this:
C++
CMegaDoc::OnStartProcess()
{
  AfxBeginThread(ThreadProcess,(LPVOID)this);
}
 CMegaDoc::OnOpenDocument(filename)
{ 
  ReadFile(filename);// I'm quite sure there is no problem in this function.
}
ThreadProcess(LPVOID pParam)
{
  CMegaDoc *pDoc = (CMegaDoc *)pParam;
  pDoc->OnOpenDocument(filename);//it's hihgly probable that something goes wrong when calling this method.
  do someting...
  pDoc->UpDateAllViews(NULL);
}


C++
CMegaView::DrawData(CDC* pDC)
{
  CMegaDoc pDoc = GetDocument();
  int nCount = pDoc->m_CenterCoord.GetSize();
  if(nCount>=2)
 {
     for(int i=0;i<nCount;i++)
     {
        int x1 = pDoc->m_CenterCoord.GetAt(i).x;
        int y1 = pDoc->m_CenterCoord.GetAt(i).y;
        int x2 = pDoc->m_CenterCoord.GetAt(i+1).x;
        int y2 = pDoc->m_CenterCoord.GetAt(i+1).x;
        pDC->MoveTo(x1,y1);
        pDC->LineTo(x2,y2);

      }
 }
  
}

CMegaView::OnDraw(CDC*pDC)
{
   DrawData(CDC* pDC);
}


The problem here is that when running Release version,it works all right,while the Debug version encounters a Debug Assertion Failure.Debugger says there is a access violation,but I can't see why and how to solve it.
Posted
Updated 3-Mar-13 0:02am
v2

1 solution

See Multithreading: Programming Tips[^] in the MSDN.

You did not tell us about your CMegaDoc class. If it is derived from CDocument or contains window handles, and calling member functions from the thread that access window handles, there will be assertions:

Access to a window from a thread that did not create the window is not supported by MFC. The debug assertions occur probably in CWnd::AssertValid(). See the source code in wincore.cpp and the comments about multithreaded applications.

If this happens with your application, you must change the design.
 
Share this answer
 
Comments
louisejackie 3-Mar-13 4:48am    
Hi Jochen,thanks for your attention!The CMegaDoc is derived from CDocument. This is a SDI application. My intention is to open a series of bmp files and do some processing at the same time.The processing result is a sery of circle's center coordinate,which I reserve in a CArray<cpoint,cpoint>ptCenterArray, so that I could draw a chart in CMegaView's member function OnDraw().The problem is it didn't draw anything. How come?
Jochen Arndt 3-Mar-13 5:29am    
This can't be done this way because MFC classes are not thread safe.

I suggest to start without using threads to ensure that your code works. Then you can move the processing code to worker threads. With images, create a copy of them in memory and pass this to the thread for processing. When processing is finished, signal this to your main thread (e.g. by posting a user defined message to your applications main window). When using a CArray to pass the processing result, you must ensure that memory is only allocated by the thread that owns the array. So you might add an array to your worker thread class and provide a function to read or copy the content when processing has finished.
louisejackie 3-Mar-13 5:43am    
If I don't start this way ,the application will behave like dead when running. I can't even click on other menu items!
Jochen Arndt 3-Mar-13 5:59am    
While the image is processed, your applications main thread is of course blocked. My suggestion to use a non-threaded version was to test your image processing code only (use simple images to reduce the processing time). Because it is easier to find problems this way rather than inside a worker thread. The final application must use a thread if processing needs some time.

Read the link from my answer and follow the contained links. Then think about how to implement the worker thread for your processing (especially how to pass data between the threads).
louisejackie 3-Mar-13 6:11am    
You have it right. I tried it in a member function of CMegaDoc, and there is no problem.But there is still no effect when drawing the data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900