CPropertySheet的问题

wzyzb 2009-08-12 08:06:12
void CPropertySheetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CMyProperty * m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
m_MyProperty->DoModal();
}
按下按钮1弹出属性页,我把属性页关了 再按按钮1的时候出错了.这是为什么?哪里出错了?
...全文
122 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
huziwu 2009-08-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wzyzb 的回复:]
void CPropertySheetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CMyProperty * m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
m_MyProperty->DoModal();
        delete m_MyProperty;  //添上这句就好了 照我的理解m_MyProperty不是局部变量嘛?虽然上次分配的资源未释放,应该不会影响下次的使用啊!
}


[/Quote]
你的CMyProperty类中是否存在全局勤静态变量,有的话如果你不释放会出错,而且申请指针,不管是不是局部的都应该释放。
wzyzb 2009-08-13
  • 打赏
  • 举报
回复

void CPropertySheetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CMyProperty * m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
m_MyProperty->DoModal();
        delete m_MyProperty;  //添上这句就好了 照我的理解m_MyProperty不是局部变量嘛?虽然上次分配的资源未释放,应该不会影响下次的使用啊!
}


雪影 2009-08-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wzyzb 的回复:]
引用 5 楼 tttyd 的回复:
在类的头文件中添加 CMyProperty * m_MyProperty;
并将CMyProperty的头文件#include,或在类定义之前添加class CMyProperty;
C/C++ codeCPropertySheetDlg::CPropertySheetDlg()
{
    m_MyProperty= NULL;//在类的构造函数中初始化m_MyProperty指针}void CPropertySheetDlg::OnButton1()
{// TODO: Add your control notification handler code hereif(m_MyProperty)
{
  m_MyProperty=new CMyProperty(_T("选项"),this,0);
  CPage1 Page1;
  CPage2 Page2;
  m_MyProperty->AddPage(&Page1);
  m_MyProperty->AddPage(&Page2);
}
m_MyProperty->DoModal();
}

使用上面的代码试一试
刚开始我就是这么做的 一样的问题
[/Quote]
我这样使用过,没什么问题的。
你仔细检查检查
wzyzb 2009-08-13
  • 打赏
  • 举报
回复

void CPropertySheetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CMyProperty * m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
m_MyProperty->DoModal();
delete m_MyProperty; //添上这句就好了 照我的理解m_MyProperty不是局部变量嘛?虽然上次分配的资源未释放,应该不会影响下次的使用啊!
}

wzyzb 2009-08-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tttyd 的回复:]
在类的头文件中添加 CMyProperty * m_MyProperty;
并将CMyProperty的头文件#include,或在类定义之前添加class CMyProperty;
C/C++ codeCPropertySheetDlg::CPropertySheetDlg()
{
m_MyProperty= NULL;//在类的构造函数中初始化m_MyProperty指针}void CPropertySheetDlg::OnButton1()
{// TODO: Add your control notification handler code hereif(m_MyProperty)
{
m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
}
m_MyProperty->DoModal();
}

使用上面的代码试一试
[/Quote]刚开始我就是这么做的 一样的问题
wzyzb 2009-08-13
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 huziwu 的回复:]
引用 11 楼 wzyzb 的回复:
void CPropertySheetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CMyProperty * m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
m_MyProperty->DoModal();
        delete m_MyProperty;  //添上这句就好了 照我的理解m_MyProperty不是局部变量嘛?虽然上次分配的资源未释放,应该不会影响下次的使用啊!
}



你的CMyProperty类中是否存在全局勤静态变量,有的话如果你不释放会出错,而且申请指针,不管是不是局部的都应该释放。
[/Quote]
CMyProperty类中没有全局的静态变量,虽然指针都应该释放但是这里我不释放也不应该会引起出错吧
Tue 2009-08-13
  • 打赏
  • 举报
回复
局部变量的地址不要随便传给非局部对象啦!

你把两个局部属性页对象交给窗口,OnButton1返回后,你的两个属性页都不在了,肯定崩溃。
fishion 2009-08-12
  • 打赏
  • 举报
回复
不怎么会用就不用指针
晒月光的青蛙 2009-08-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 tttyd 的回复:]
在类的头文件中添加 CMyProperty * m_MyProperty;
并将CMyProperty的头文件#include,或在类定义之前添加class CMyProperty;
C/C++ codeCPropertySheetDlg::CPropertySheetDlg()
{
m_MyProperty= NULL;//在类的构造函数中初始化m_MyProperty指针}void CPropertySheetDlg::OnButton1()
{// TODO: Add your control notification handler code hereif(m_MyProperty)
{
m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
}
m_MyProperty->DoModal();
}

使用上面的代码试一试
[/Quote]


if(m_MyProperty)
应该换成if( NULL != m_MyProperty )

yzhouen 2009-08-12
  • 打赏
  • 举报
回复
楼上代码可行
雪影 2009-08-12
  • 打赏
  • 举报
回复
在类的头文件中添加 CMyProperty * m_MyProperty;
并将CMyProperty的头文件#include,或在类定义之前添加class CMyProperty;
CPropertySheetDlg::CPropertySheetDlg()
{
m_MyProperty = NULL; //在类的构造函数中初始化m_MyProperty指针
}
void CPropertySheetDlg::OnButton1()
{
// TODO: Add your control notification handler code here
if(m_MyProperty)
{
m_MyProperty=new CMyProperty(_T("选项"),this,0);
CPage1 Page1;
CPage2 Page2;
m_MyProperty->AddPage(&Page1);
m_MyProperty->AddPage(&Page2);
}
m_MyProperty->DoModal();
}


使用上面的代码试一试
oyljerry 2009-08-12
  • 打赏
  • 举报
回复
既然出错了,那么按下按钮的时候自己设置断点,一步步debug调试就可以找到出错的地方了
bohut 2009-08-12
  • 打赏
  • 举报
回复
m_MyProperty定义成CPropertySheetDlg的成员变量
dong364 2009-08-12
  • 打赏
  • 举报
回复
有没有delete啊?
DarkChampion 2009-08-12
  • 打赏
  • 举报
回复
报什么错?

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧