What's wrong with my code?
-
I had written a dialog based application. It contained a button named "Try" When I clicked the "Try" button,opened an other dialog named "Test" which contained a button named "Open". When I first clicked "Open" button and clicked the "OK" button in Open File Dialog,everything is ok,and then close the "Test" Dialog. But When I click "Try" button again,it showed me wrong. Who can please tell me what's wrong with my code?? ****************************************************************** void CFirstDlg::OnButtonTry() { // TODO: Add your control notification handler code here CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); } ****************************************************************** BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString sCon; _ConnectionPtr m_pCon; _RecordsetPtr m_pRs; sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=main.mdb"; CoInitialize (NULL); HRESULT hr=S_OK; try { hr=m_pCon.CreateInstance(__uuidof(Connection)); if(FAILED(m_pCon->Open(_bstr_t(sCon),"","",adModeUnknown))) AfxMessageBox("Can not open the database!"); } catch(_com_error e) { CString errormessage; errormessage.Format("Failed!\r\nError Message:%s",e.ErrorMessage()); AfxMessageBox(errormessage); } m_pRs.CreateInstance("ADODB.Recordset"); if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText))) AfxMessageBox("Can not open the record set!"); if((m_pRs->State & adStateOpen) == adStateOpen) m_pRs->Close(); if ( (m_pCon->State & adStateOpen) == adStateOpen) m_pCon->Close(); CoUninitialize(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } *********************************************************************** void CTestDialog::OnButtonOpen() { // TODO: Add your control notification handler code here static char BASED_CODE szFilter[]="Bitmap Files (*.bmp)|*.bmp||"; CFileDialog *fd; fd=new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,this); if(IDOK!=fd->DoModal()) return; else AfxMessageBox(fd->GetFileName()); delete fd; }
-
I had written a dialog based application. It contained a button named "Try" When I clicked the "Try" button,opened an other dialog named "Test" which contained a button named "Open". When I first clicked "Open" button and clicked the "OK" button in Open File Dialog,everything is ok,and then close the "Test" Dialog. But When I click "Try" button again,it showed me wrong. Who can please tell me what's wrong with my code?? ****************************************************************** void CFirstDlg::OnButtonTry() { // TODO: Add your control notification handler code here CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); } ****************************************************************** BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString sCon; _ConnectionPtr m_pCon; _RecordsetPtr m_pRs; sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=main.mdb"; CoInitialize (NULL); HRESULT hr=S_OK; try { hr=m_pCon.CreateInstance(__uuidof(Connection)); if(FAILED(m_pCon->Open(_bstr_t(sCon),"","",adModeUnknown))) AfxMessageBox("Can not open the database!"); } catch(_com_error e) { CString errormessage; errormessage.Format("Failed!\r\nError Message:%s",e.ErrorMessage()); AfxMessageBox(errormessage); } m_pRs.CreateInstance("ADODB.Recordset"); if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText))) AfxMessageBox("Can not open the record set!"); if((m_pRs->State & adStateOpen) == adStateOpen) m_pRs->Close(); if ( (m_pCon->State & adStateOpen) == adStateOpen) m_pCon->Close(); CoUninitialize(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } *********************************************************************** void CTestDialog::OnButtonOpen() { // TODO: Add your control notification handler code here static char BASED_CODE szFilter[]="Bitmap Files (*.bmp)|*.bmp||"; CFileDialog *fd; fd=new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,this); if(IDOK!=fd->DoModal()) return; else AfxMessageBox(fd->GetFileName()); delete fd; }
You didn't delete the dlg object in the CFirstDialog::OnButtonTry(). That should be the problem. Why do you want to use the heap object rather than stack object? If you do it like this: void CFirstDialog::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } you won't be worried about the work of releasing object. I think this form is better.;) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.
-
I had written a dialog based application. It contained a button named "Try" When I clicked the "Try" button,opened an other dialog named "Test" which contained a button named "Open". When I first clicked "Open" button and clicked the "OK" button in Open File Dialog,everything is ok,and then close the "Test" Dialog. But When I click "Try" button again,it showed me wrong. Who can please tell me what's wrong with my code?? ****************************************************************** void CFirstDlg::OnButtonTry() { // TODO: Add your control notification handler code here CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); } ****************************************************************** BOOL CTestDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CString sCon; _ConnectionPtr m_pCon; _RecordsetPtr m_pRs; sCon="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=main.mdb"; CoInitialize (NULL); HRESULT hr=S_OK; try { hr=m_pCon.CreateInstance(__uuidof(Connection)); if(FAILED(m_pCon->Open(_bstr_t(sCon),"","",adModeUnknown))) AfxMessageBox("Can not open the database!"); } catch(_com_error e) { CString errormessage; errormessage.Format("Failed!\r\nError Message:%s",e.ErrorMessage()); AfxMessageBox(errormessage); } m_pRs.CreateInstance("ADODB.Recordset"); if(FAILED(m_pRs->Open("SELECT * FROM employeer",_variant_t((IDispatch*)m_pCon,true),adOpenStatic,adLockOptimistic,adCmdText))) AfxMessageBox("Can not open the record set!"); if((m_pRs->State & adStateOpen) == adStateOpen) m_pRs->Close(); if ( (m_pCon->State & adStateOpen) == adStateOpen) m_pCon->Close(); CoUninitialize(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } *********************************************************************** void CTestDialog::OnButtonOpen() { // TODO: Add your control notification handler code here static char BASED_CODE szFilter[]="Bitmap Files (*.bmp)|*.bmp||"; CFileDialog *fd; fd=new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY,szFilter,this); if(IDOK!=fd->DoModal()) return; else AfxMessageBox(fd->GetFileName()); delete fd; }
After seeing code, I think you are not destroying dialog (delete dlg). This should be your problem. CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); ////add line... delete dlg; :-O :-O :-O :rose: :rose: :rose: -------------------------------------------------- Say Whatever You Know. Helping other people is good for health. ========= Manish ========= ---------------------------------------------------
-
After seeing code, I think you are not destroying dialog (delete dlg). This should be your problem. CTestDialog* dlg; dlg=new CTestDialog; dlg->DoModal(); ////add line... delete dlg; :-O :-O :-O :rose: :rose: :rose: -------------------------------------------------- Say Whatever You Know. Helping other people is good for health. ========= Manish ========= ---------------------------------------------------
The wrong still happened.
-
You didn't delete the dlg object in the CFirstDialog::OnButtonTry(). That should be the problem. Why do you want to use the heap object rather than stack object? If you do it like this: void CFirstDialog::OnButtonTry() { CTestDialog dlg; dlg.DoModal(); } you won't be worried about the work of releasing object. I think this form is better.;) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.
The wrong still happened,what should I do?
-
The wrong still happened,what should I do?
So could you send your piece of codes to me? I think looking into the codes will be more clear.:) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.
-
So could you send your piece of codes to me? I think looking into the codes will be more clear.:) Law is meaningless without chaos. Chaos without Law is equal to destruction. Chaos and Law create our rich and colorful world.
Thank you,under the help of others,I had solved the problem. Thanks again. A beginner of Visual C++,and need your help.