难道Marco Cantu写错了吗

piaozi 2001-10-24 03:40:24
我在学习《Delphi5从入门到精通》,作者说“在处理对象某个时间的同时,不能
消除该对象,否则会引起一个异常”,可我在窗体中建立一个按纽,处理OnClick
事件时:
procedure TForm1.ButtonKeyPress(Sender:TObject,var Key:Char)
begin
if Key=#8 then
Sender.Free;
end;
未出现任何问题,难道Marco Cantu写错了吗?请大侠指点
...全文
140 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
EP外星人 2001-10-25
  • 打赏
  • 举报
回复
刚才在帮助中看到一段,请留意那个Warning.

Destroys an object and frees its associated memory, if necessary.

procedure Free;

Description

Use Free to destroy an object. Free automatically calls the destructor if the object reference is not nil
. Any object instantiated at runtime that does not have an Owner should be destroyed by a call to Free, so that can be properly destroyed and the memory released. Unlike Destroy, Free is successful even if the object is nil, so if the object was never initialized, Free won抰 result in an error.

When you call Free for a component, it calls Free for all components that it owns, that is, all components in its component list. A form owns all the controls and non-visual components that are created on it in design mode. When it is freed, all of these components are automatically freed as well. Since, by default, all forms are owned by the Application object, when the application terminates, it frees the Application object, which frees all forms. For all objects that are not components, or for components created with a nil owner, be sure to call Free after you are finished with the object; otherwise the allocated memory will not be usable until after the application terminates.

Warning: Never explicitly free a component within one of its own event
handlers or free a component from the event handler of a
component it owns or contains. For example, don抰 free a button
in its OnClick event handler or free the form that owns the
button from the button's OnClick event.

To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.


它只是说永远不要,当然,有点常识的程序员谁也都不会这么干,可这么干偏偏可以。

EP外星人 2001-10-24
  • 打赏
  • 举报
回复
谢谢各位。
EP外星人 2001-10-24
  • 打赏
  • 举报
回复
我要给Marco Cantu写份信。
liang_z 2001-10-24
  • 打赏
  • 举报
回复
FREE后你再去调用那不出错才怪了!
liang_z 2001-10-24
  • 打赏
  • 举报
回复
我想只要你在FREE后没有再去用它,应该不会有问题。
tikkypeng 2001-10-24
  • 打赏
  • 举报
回复
同意楼上各位所言!该对象虽然已经被释放调~可能你还没有再次操作涉及到那一部分内存或者是那个对象实例~~所以还没有引发异常~~如果你再次调用试试~~我想应该会报错的~~
我在网吧~~每办法调试~~有机会我在试试~~
liang_z 2001-10-24
  • 打赏
  • 举报
回复
我想你可以用一个方法来测试内存有没有释放,
那就是用一个线程不停地产生对象,另一个线程
不停地发消息,一段时间后比较前后的系统可用
资源有没有少下去!
EP外星人 2001-10-24
  • 打赏
  • 举报
回复
并不是windows消息的问题,他的确释放了该对象的内存,
没有出问题是因为你还没有使用那片内存,如果一旦被分配
就会产生另系统崩溃的冲突错误!~~~

上面的这段话我的理解是即使对象被释放,它所占用过的内存也不能重新分配。


我想Marco Cantu的话不一定在每个事件中都成立

比如我们肯定不能在拖放的一系列事件的开始接触所拖放的对象。

现在我的问题是无法验证在我保证代码没有问题的情况下(比如执行keyPress后,我知道已经解除了Button,我就不会对它进行有关的操作),也会导致异常。
EP外星人 2001-10-24
  • 打赏
  • 举报
回复
我执行了不光以上的代码,还在鼠标事件中作乐同样的尝试。

问题是Delphi释放了内存,无论对于程序还是Windows,被释放的内存总是可用的,如何会如icehill所说不可用呢。

如果你不对已经释放了的对象再进行新的操作如何会引发异常?
BlueTrees 2001-10-24
  • 打赏
  • 举报
回复
嘿!你的Key=#8,这个按键按了吗?
如果真的执行了Sender.Free那么那个对象的的确确被释放了,那么有两种可能,一种是Delphi建立的内存管理机制并没有向Windows返还这个内存,另一种是Windows没有真正的回收这片内存,要知道Windows的内存分配是基于页的,如果你的这个对象所占用的内存太小,或者还有其他对象或者变量和他占据着同一块页面,那么Windows是不会回收的,你仍然可以使用。
EP外星人 2001-10-24
  • 打赏
  • 举报
回复
我感觉不大对头
piaozi 2001-10-24
  • 打赏
  • 举报
回复
谢谢你,希望以后还能向你请教。
icehill 2001-10-24
  • 打赏
  • 举报
回复
你想,在对象的事件中释放对象,我想肯定是释放不干净的
这个不干净可以用下面的话来解释,就是他口头上宣布释放了,但是
事实上内存仍旧被占用,有新对象建立的时候,windows则认为此片
内存是可以使用的,就冲突了~~
EP外星人 2001-10-24
  • 打赏
  • 举报
回复
求教icehill(闲云幽步踏古来) 

被释放了的内存如何会不可用呢

icehill 2001-10-24
  • 打赏
  • 举报
回复
在该对象的事件中释放该对象,后果是不可预料的
并不是windows消息的问题,他的确释放了该对象的内存,
没有出问题是因为你还没有使用那片内存,如果一旦被分配
就会产生另系统崩溃的冲突错误!~~~
gaoql 2001-10-24
  • 打赏
  • 举报
回复
同意楼上的,你的理解有问题
EP外星人 2001-10-24
  • 打赏
  • 举报
回复
我想你并没有处理该对象,而是在处理windows的消息。
不知下面的高手认为这样说对不对。
piaozi 2001-10-24
  • 打赏
  • 举报
回复
这个问题都每人愿意回答吗?是不是太没水平?:<
bubble 2001-10-24
  • 打赏
  • 举报
回复
@_@

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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