Should I put a try-finally block after every Object.Create?

后端 未结 8 1381
青春惊慌失措
青春惊慌失措 2021-02-04 17:11

I have a general question about best practice in OO Delphi. Currently, I put try-finally blocks anywhere I create an object to free that object after usage (to avoid memory leak

8条回答
  •  星月不相逢
    2021-02-04 18:06

    If you're creating the object in a class's constructor and the object will be owned by the enclosing class, you'll want to free it in the owning class's destructor.

    I tend to use FreeAndNil() instead of calling Free.

    EDIT: But as others have said, you definitely always want to free what you create.

提交回复
热议问题