3 ms·
Actually it does have automatic destructors, they're just not the default option. The first way to auto destroy objects is to expose an Interface and free your
by sysrpl 8y ago
Actually it does have automatic destructors, they're just not the default option.
The first way to auto destroy objects is to expose an Interface and free your object in the Release method. You can inherit from several class types that already do that if you don't want to rewrite a new class.
The second way is a newer feature that allows you to define Initiazlize and Finalize methods on any complex type. When you define these methods, the compiler generates code to reference count your object and call Initazlize on create, and Finalize when your object is no longer referenced. This works the same on stack based types as well as heap allocated types.
See also http://wiki.freepascal.org/management_operators http://wiki.freepascal.org/management_operators
- theamk 8y agoSweet! The docs say "since version 3.1.1", and the latest release seems to be 3.0.4, so I guess this has not been released yet? Do you think that the changes help with simpler algorithms -- like in StringHashList [0], where destructor specifically calls Dispose on each array element, and then deletes the array storage itself? [0] https://github.com/graemeg/lazarus/blob/95d183bacd5f895d3e6c7cc5b11ea7723debda31/components/lazutils/stringhashlist.pas https://github.com/graemeg/lazarus/blob/95d183bacd5f895d3e6c...
- sysrpl 8y agoThe odd minor versions are never released as official builds, but they are official versions. You'll need to build it yourself which isn't that hard. Keep in mind fpc, the compiler, is separate from Lazarus, but if you want to use Lazarus with fpc 3.1.x you need to build Lazarus using fpc 3.1.x. Again it's not that hard (make all). The change we're talking about automatically invokes Initialize and Finalize on all types for dynamic allocations / destruction, unless they intentionally circumvent it. Intentional circumvention might include allocating raw memory in a class and treating the class like an array with an index property. In other words, if you declare a raw pointer, allocate untyped memory (e.g. bytes), and handle something back from that memory using a typecast, then you are bypassing automatic allocation and destruction. All other ways to make space for complex types (records and classes) as well as types which may hold complex types (arrays and nested fields) will safely and reliably use Initialize and Finalize when needed if they are defined.
- edwinyzh 8y ago>You'll need to build it yourself which isn't that hard. Or use fpcupdeluxe with several clicks, screenshot here: https://screenpresso.com/=iyqoc https://screenpresso.com/=iyqoc