<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p><br>
</p>
<div class="moz-cite-prefix">On 10/21/24 5:37 AM, Hairy Pixels via
fpc-pascal wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAGsUGtmNHeShAAJ5TFYPs5fkX3ckM85paj+NyzbfZ0s_iYvzXw@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div class="gmail_quote"><br>
<div class="gmail_quote">
<div dir="ltr">then isn’t Pascal’s requirement to predeclare
uninitialized variables the worst possible design? In C you
can at least declare a variable with its assignment in one
step but with Pascal by design you can always read
uninitialized memory with the exception of arguments.</div>
<div dir="ltr"><br>
</div>
<div dir="ltr">Many bugs I’ve caused because I didn’t set the
“result” of a function for example. I wish they could make
that an option to always set the default value. <br>
</div>
</div>
</div>
</blockquote>
Free Pascal produces warning for uninitialized values and has an
option to treat warnings as error. My advice is to compile your
programs with -Sew<br>
<blockquote type="cite"
cite="mid:CAGsUGtmNHeShAAJ5TFYPs5fkX3ckM85paj+NyzbfZ0s_iYvzXw@mail.gmail.com">
<div class="gmail_quote" dir="ltr">FPC does nothing to help you
manage memory so I would expect more leaks.</div>
</blockquote>
<p>It's not nothing. There's:</p>
<p>0) built-in leak detection with the -gh compiler option<br>
</p>
<p>1) ansistrings, widestrings and dynarrays</p>
<p>2) record types (as opposed to class), which can live on the
stack and have a pass-by-value semantics. FPC trunk supports
advanced records, so you get C++-style copy constructors.</p>
<p>3) the object type also has pass-by-value semantics</p>
<p>4) interfaces are COM-like and reference counted by default.
Every interface inherits from IUnknown and the compiler
automatically inserts calls to AddRef() and Release() when a
variable enters or leaves scope, or when assigning values, or when
passing them as parameters. If your class is only accessed through
interfaces, you can just do:</p>
<p>type</p>
<p> TMyReferenceCountedClass = class(TInterfacedObject,
IMyFirstInterface, IMySecondInterface, ...)</p>
<blockquote type="cite"
cite="mid:CAGsUGtmNHeShAAJ5TFYPs5fkX3ckM85paj+NyzbfZ0s_iYvzXw@mail.gmail.com">
<div class="gmail_quote" dir="ltr"> Being able to declare classes
on the stack and destroy automatically automates away a big
category of memory leaks right there.</div>
</blockquote>
<p>You can't with classes, but you can with records, objects and
static arrays, which are usually more appropriate in these cases.</p>
<p>When using classes, the proper way to use them is to design them
with some sort of ownership relations between them. For example
ClassA contains a ClassB, so ClassA is responsible for creating
ClassB and destroying ClassB, when ClassA is destroyed. That's how
99% of the class memory management system works, and it's so easy.
The fact that the ownerships are explicit, and easily seen (and
not hidden like in C++'s RAII style) makes thinking about this and
debugging it easy. Out of the languages that require manual memory
management, I've written C, C++, Rust and Object Pascal. Object
Pascal is the easiest of them all. C comes second, but it has some
horrible flaws, like the char* strings, which are very difficult
to use correctly and very bug-prone.<br>
</p>
<p>Nikolay</p>
</body>
</html>