0
votes

Now I have the following MFC SDI application code,this code comes from my view class:

void CNew_demo_appView::OnItemUpdate()
{
    // TODO: Add your command handler code here
    int i=this->GetListCtrl().GetSelectionMark();//get the selected item no
    this->GetDocument()->unpacker.GetInformation(i,(BYTE*)(&(this->GetDocument()->fpga_info)));
    UpdateFpgaAttrib updatefpgadlg;
    updatefpgadlg.DisplayInfo(this->GetDocument()->fpga_info);
    updatefpgadlg.DoModal();
}

void CNew_demo_appView::SetItemFpgaAttrib(int index,FPGA_INFO info)
{
    this->GetDocument()->fpga_items[0]=info;
}

As you can see, I got a CDialog Derived class called UpdateFpgaAttrib, I instantialize it in the OnItemUpdate function which is called when a menu command is issued, then DoModal() Popup the Dialog window, on that dialog, there is a button, when clicked it will call the SetItemFpgaAttrib function which belongs to the View Class,

((CNew_demo_appView*)this->GetParent())->SetItemFpgaAttrib(0,info);

here is the problem, when this SetItemFpgaAttrib references some data using this pointer, it always got some Access Violation Error, when I invoke this function in other View class function, it is ok,

void CNew_demo_appView::test()
{
    SetItemFpgaAttrib(0,this->GetDocument()->fpga_info)
}

when triggered by the popup dialog button, it cause problem, I set break point on the SetItemFpgaAttrib , I found the this pointer value is normal 0x0041237f thing, but when triggered by the button ,it is always 0x00000001, the the GetDocument call alway cause problem. Why is the this pointer value changed,is that caused by the context or something else? I am using Vs2008 SP1

1

1 Answers

0
votes

Problem solved, I just want to put the answer here for somebody else who also got this problem someday. The Problem is the

((CNew_demo_appView*)this->GetParent())->SetItemFpgaAttrib(0,info);

the GetParent() is implemented in CWnd, and it returns a CWnd*, that's the problem, the SetItemFpgaAttrib(0,info) is a function of my CDialog-Derived class CNew_demo_appView, it's not the member of CWnd, so the returned CWnd* pointer can't get the code to that function, if you do that like me, you will access some wrong place and will got Access violated error etc. I Need a function that return the original CNew_demo_appView* pointer value, the one in the m_pParentWnd is the value needed(I figure this out when I step into the CWnd::GetParent function), whilethe default GetParent did this:

return (CWnd*)ptr;

to solve this problem, I just add another function to my CDialog-Derived Class:

CWnd* UpdateFpgaAttrib::GetParentView(void)
{
    return this->m_pParentWnd; //just return the parent wnd pointer
}

then call this instead of the default GetParent:

CNew_demo_appView* view=(CNew_demo_appView*)this->GetParentView();

Then everything is ok.

So conclusion: the CWnd* cast in the GetParent changed the value of the pointer.