释放Frame内存引起的奇怪异常,来拿分! ( 积分: 100 )

  • 主题发起人 主题发起人 man8888
  • 开始时间 开始时间
M

man8888

Unregistered / Unconfirmed
GUEST, unregistred user!
我在点一个Frame里面一个按钮后释放Frame内存,然后显示另外一个Frame时出奇怪的错误。<br>Access&nbsp;violation&nbsp;at&nbsp;address&nbsp;0040366E&nbsp;in&nbsp;module&nbsp;'Project1.exe'.&nbsp;Read&nbsp;of&nbsp;address&nbsp;67696518.<br>如果把这个按钮放在主窗体(form1)上那就一点错误都没有,那么怎么在Frame之间进行切换呢?估计释放Frame内存后按钮也被释放了?
 
我觉得问题说的不够详细,最好能贴出代码.
 
这样做肯定会出错的
 
不建议在Frame里面释放自己,然后再加载其他窗体到主窗体。<br>非要这样做,可以把这部分代码提出来,放到外部单元,如公共单元,或者主窗体。<br>//主窗体<br>unit&nbsp;Unit3;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs;<br><br>type<br>&nbsp;&nbsp;TFrameClass&nbsp;=&nbsp;class&nbsp;of&nbsp;TFrame;<br><br>&nbsp;&nbsp;TForm3&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;FormCreate(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>procedure&nbsp;ShowFrame(AParent:&nbsp;TWinControl;&nbsp;AFrame:&nbsp;TFrameClass);<br><br>var<br>&nbsp;&nbsp;Form3:&nbsp;TForm3;<br>&nbsp;&nbsp;FCurrFrame:&nbsp;TFrame;//当前加载的FRAME<br><br>implementation<br><br>uses<br>&nbsp;&nbsp;Unit1,&nbsp;Unit2;<br><br>{$R&nbsp;*.dfm}<br><br>{&nbsp;TForm3&nbsp;}<br><br>procedure&nbsp;TForm3.FormCreate(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ShowFrame(self,&nbsp;TFrame1);<br>end;<br><br>procedure&nbsp;ShowFrame(AParent:&nbsp;TWinControl;&nbsp;AFrame:&nbsp;TFrameClass);<br>begin<br>&nbsp;&nbsp;if&nbsp;Assigned(FCurrFrame)&nbsp;then&nbsp;FreeAndNil(FCurrFrame);<br><br>&nbsp;&nbsp;FCurrFrame&nbsp;:=&nbsp;AFrame.Create(nil);<br>&nbsp;&nbsp;FCurrFrame.Parent&nbsp;:=&nbsp;AParent;<br>&nbsp;&nbsp;FCurrFrame.Show;<br>end;<br><br>end.<br>//Frame1<br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,&nbsp;<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls;<br><br>type<br>&nbsp;&nbsp;TFrame1&nbsp;=&nbsp;class(TFrame)<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Label1:&nbsp;TLabel;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>implementation<br><br>uses<br>&nbsp;&nbsp;Unit3,&nbsp;Unit2;<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TFrame1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ShowFrame(Form3,&nbsp;TFrame2);<br>end;<br><br>end.<br><br>//Frame2<br>unit&nbsp;Unit2;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,&nbsp;<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls;<br><br>type<br>&nbsp;&nbsp;TFrame2&nbsp;=&nbsp;class(TFrame)<br>&nbsp;&nbsp;&nbsp;&nbsp;Button2:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Label1:&nbsp;TLabel;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button2Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>implementation<br><br>uses<br>&nbsp;&nbsp;Unit3,&nbsp;Unit1;<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TFrame2.Button2Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ShowFrame(Form3,&nbsp;TFrame1);<br>end;<br><br>end.
 
答案是:通过点按钮时候PostMessage一个自定义消息,在Frame内部处理这个消息,消息处理函数写Frame2.Free就没问题了,感谢各位!<br>这个题本来是送分题,是给thinknet的,他不来拿分咱就平分秋色吧!恭喜大家!揭帖!:))
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部