但是为了可靠性和可读性,这个Free最好不要省。
而且还要用try...finally...end装起来。
这样即使你在过程中出现异常,也能保证资源能够释放。

解决方案 »

  1.   

    到了end不自动释放          
      

  2.   

    向你这样,一定要FREE,否则就会有Memory Leak。
    但是如果形如x := TX.Create(Application)这样,可以不Free,那么在Application被Free时,Application会替你free的。
      

  3.   

    同意: chechy(chechy)﹐我一般喜歡手工控制資源
      

  4.   

    如果动态创建的控件继存自Tcompoment 且Create中指定了Owner 不为NIL,无须Free,
    可参考vcl之Tcompoment.Destory 方法的源码.
      

  5.   

    自己创建的对象,系统不会帮你Free
      

  6.   

    其实在c中到}不是声明什么都会释放的因为在delphi中创建类实例必须在堆中
    所以必须释放,而在c中类实例可以在栈中声成
    所以到}可以自动释放,如用new生成的对象,也就是堆中,也必须释放,即delete
      

  7.   

    C++中}会自动释放吗?如果你将这个函数重复N遍,你可能早就死掉了。比如TList中的内容,如果把TList对象释放了,儿不把其内容释放掉,她会自己释放吗?你重复N遍试一试。
      

  8.   

    我指的是对象本身而言,vcl对象必须在堆中生成,
    如在c++builder中你可以声明一个类,

    class a
    {
      public:
        a(){ShowMessage("constructor");}
       ~a(){ShowMessage("destructor");}
    }
    在一过程里 声明 a temp;你就可以知道了,当然类似TList的类,可以在析构函数中释放其内容
      

  9.   

    楼上的高手们,请问如果我在Form的OnClose事件中加入Action:caFree;
    那么我手工建立对象会自动释放吗?
      

  10.   

    如果你的这些对象的Owner是这个Form,那么都会自动释放。
    不过我喜欢手工做。
      

  11.   

    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.
      

  12.   

    to :Brand1(孤独的人)  
    Tlist我也知道一定要释放,可我想知道的是,在Delphi中如果我只是简单的create之后做了一些事,然后就碰上了子过程的end;那还需要free吗?
     
      

  13.   

    c++ 中的对象其实有两种
    1. CObject aObject;
    2. CObject* pObject; pObject  = new CObject;第一种与普通的变量类似,是在堆栈中生成的,如果超出了它的作用域,系统会自动释放的。
    第二种是在堆中生成的,你所定义的变量只是一个指针,所以当你不再需要她的时候,delete她delphi(应该是Object Pascal)中没有第一种对象,所有以形式(var aObject : TObject)定义的变量都是指向真实对象的引用(这和java是相同的),引用其实就是一种指针,所一当你确认对象无用时free她,vcl中的标准组件好像推荐使用.Destory。另外vcl组件在释放时会先自动释放它的子组件。
      

  14.   

    这个Free是怎么个用法呀,是不是不管什么情况它都会执行这个语句呢,还是遇到异常错误才会发生这个事件呢????
      

  15.   

    一句话,凡是Create(nil)的都要自己释放,其他的就没有关系了。只要有Owner,Owner释放的时候就会自动释放了。
      

  16.   

    另外可视控件如果Create(nil),但是设置了parent也不用释放
      

  17.   

    建立后,系统不能自动FREE,在小PROGRAM中,没什么,但大PRAGRAM中同,使更好,还是FREE