2012-12-29 63 views
0

現在我有以下的MFC SDI應用程序代碼,這個代碼來自我的視圖類:在MFC SDI應用改變了這個指針值

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; 
} 

正如你所看到的,我叫UpdateFpgaAttrib一個CDialog派生類,我在OnItemUpdate函數中實例化了這個函數,這個函數在發佈菜單命令時調用,然後彈出對話框窗口,在對話框中有一個按鈕,點擊後會調用屬於視圖的 SetItemFpgaAttrib函數類,

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

這裏的問題是,當這種 SetItemFpgaAttrib使用該指針引用一些數據,它總是有一些訪問衝突錯誤,當我調用其他的視圖類功能這一功能,這是確定,

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

觸發時通過彈出對話框按鈕,它會引起問題,我在SetItemFpgaAttrib上設置了斷點,我發現這個指針的值是正常的0x0041237f的東西,但是當被按鈕觸發時,它總是0x00000001,GetDocument調用總是會引起問題。爲什麼這個指針值發生了變化,是由上下文還是其他引起的?我正在使用Vs2008 SP1

回答

0

問題解決了,我只想把答案放在別人誰也有這個問題的一天。問題是

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

使用getParent()在CWnd的實現,並返回一個CWnd *,這就是問題所在,在SetItemFpgaAttrib(0,資訊)是我的CDialog派生類CNew_demo_appView的功能,它的不是CWnd的成員,所以返回的CWnd *指針無法獲得該函數的代碼,如果你這樣做,你會訪問一些錯誤的地方,並會得到訪問違反錯誤等。我需要一個函數,返回原來CNew_demo_appView *指針值,一個在m_pParentWnd是需要的值(我想出解決辦法,當我踏進的CWnd ::的getParent功能),whilethe默認的getParent這樣做:

return (CWnd*)ptr; 

來解決這個問題,我只需要添加其他功能我CDialog派生類:

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

然後調用而不是默認的getParent這樣的:

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

然後一切正常。

因此得出結論:在GetParent中投射的CWnd *改變了指針的值。