How do i return an object from a function in Delphi without causing Access Violation?

后端 未结 10 1237
被撕碎了的回忆
被撕碎了的回忆 2021-02-13 14:54

I have a delphi function that returns a TStringList, but when I return a value and try to use it I get a Access Violation Error i.e

myStringList := FuncStringLis         


        
10条回答
  •  南方客
    南方客 (楼主)
    2021-02-13 15:17

    You simply cannot free something and then expect to reference it later. That is the wrong way. You have two basic options:

    • Do not call free, and make the caller responsible for disposing of the object
    • Have the caller pass in an object so that it's responsible for both Create and Free

    The first option seems simpler, keeps the interface to the function smaller, etc. The second option makes usage less error prone because it's intuitive to the caller that it's responsible for managing the object.

提交回复
热议问题