| |

VerySource

 Forgot password?
 Register
Search
View: 1820|Reply: 14

I use AfxBeginThread to start a thread. The view class pointer is required in the thread function, but there will be mem

[Copy link]

1

Threads

9

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-9-25 19:00:01
| Show all posts |Read mode
I am an MDI, and a thread starts in the dialog box. In the main thread of the dialog box class to obtain the view class pointer as follows, there will be a memory leak. In a separate thread, press the following to get the view pointer, there will be an assertion error.
CMDIFrameWnd *pMain=(CMDIFrameWnd *)AfxGetApp()->m_pMainWnd;//
CMDIChildWnd *pChild=(CMDIChildWnd *)pMain->GetActiveFrame
CMyView *pView=(CMyView *)pChild->GetActiveView();
Ask heroes for help!
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 Unknown

Post time: 2020-9-26 16:15:01
| Show all posts
Interrelated interfaces should be created, accessed, and destroyed in the same thread.
Reply

Use magic Report

1

Threads

9

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-26 20:30:01
| Show all posts
Can you tell me more in detail upstairs?
My code is to open a dialog box in the view class, and then create a thread in the dialog box. In the thread function, we need to get the pointer of the currently active view class to add content to the member variables of the view class and draw pictures on the interface. Is this wrong?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-26 20:45:01
| Show all posts
Generally speaking, in MFC, the CWnd object pointer cannot be passed across threads. Because each thread has its own internal map, the map records the HWND and the corresponding CWnd object. When a CWnd object is created, its HWND and CWnd object pointers will be recorded in the map of the thread, but the maps of other threads are not recorded.
When passing CWnd pointers across threads and calling certain functions of CWnd, these functions will perform a validity check, that is, check the map, because there is no corresponding record in the map, an error will be reported. Usually this error is AsserValid(this)
Therefore, to pass parameters across threads in MFC, it is better to pass the handle HWND instead of the CWnd object pointer.
Reply

Use magic Report

1

Threads

9

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-26 21:00:01
| Show all posts
My code is specifically like this:
MDI
MyView is based on CFormView
A button function in the Dialog class
void CUsbBegin::OnButtonStart()
{
...
//Create drawing display thread
m_draw=AfxBeginThread(DrawGraph,(void *)this,THREAD_PRIORITY_BELOW_NORMAL,\
0,CREATE_SUSPENDED);
m_draw->m_bAutoDelete=false;
m_draw->ResumeThread();
...
...
CDialog::OnOK();
}

The thread function is a global function:
UINT DrawGraph(LPVOID lpV)
{
CUsbBegin *pDraw=(CUsbBegin *)lpV;
pDraw->ShowDataItems();
...
To
}

UINT CUsbBegin::ShowDataItems()
{
To
CMDIFrameWnd *pMain=(CMDIFrameWnd *)AfxGetApp()->m_pMainWnd;//Get the active MDI child window.
CMDIChildWnd *pChild=(CMDIChildWnd *)pMain->GetActiveFrame();//Get the active view attached to the active MDI child window.
CMyView *pView=(CMyView *)pChild->GetActiveView();
pView->func();
...
...
}
Run or debug will appear
The first is the assertion error, click OK and then the memory cannot be read error.

If I want to get the view class pointer in the drawing thread to draw on the interface, how should I achieve it?
Thank you ^_^
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-26 21:15:01
| Show all posts
It can be handled like this:
Customize a message, such as WM_USER_DRAW,
When you need to perform a drawing operation, just like the view sending the message, you can use PostMessage or SendMessage. If you need to pass data, use global variables or SendMessage.
The approximate pseudo code is as follows:

hWnd; // This is the handle of the CMyView passed as a parameter when the thread is created
...
// Define and initialize the data required for drawing operations, theData
::SendMessage( hWnd, WM_USER_DRAW, (WPARAM)&theData, (LPARAM)NULL);
//////////////////////////////////////////////
Add a message corresponding function in CMyView, and perform actual drawing operations in this function
LRESULT CMyView::OnUserDraw( WPARAM wParam, LPARAMlParam)
{

...
return 0L;
}
Reply

Use magic Report

1

Threads

9

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-27 01:00:01
| Show all posts
I have a large amount of data and require high real-time performance. Is it efficient to send and receive messages so frequently?
If I directly define a global variable data, declare extern data in the view class; is it possible to draw pictures in the view class?
But this is not a new thread function to draw a picture, is it equivalent to drawing in the main thread?
marrco: Is it SendMessage in the drawing thread?
Reply

Use magic Report

1

Threads

9

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-27 10:00:01
| Show all posts
hWnd; // This is the handle of the CMyView passed as a parameter when the thread is created

Please tell me how to achieve this?
Reply

Use magic Report

1

Threads

9

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-9-27 10:15:01
| Show all posts
The reason for the assertion failure is that the problem you mentioned in the debug mode
There is no mfc library in the release version mode. It is not recommended to do this, because data may be lost when the cpu scene is switched.
If you want to do, don’t execute CFrameWnd::AssertValid() where the assertion error occurs.
Reply

Use magic Report

1

Threads

9

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-9-27 10:30:01
| Show all posts
Why not execute CFrameWnd::AssertValid()?
This is the stuff in the mfc library function
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list