tag:blogger.com,1999:blog-53308650504335618402024-03-12T20:55:49.055-07:00Delphi Programming Tips & TricksDalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.comBlogger70125tag:blogger.com,1999:blog-5330865050433561840.post-27165358712701380722023-12-15T02:12:00.000-08:002023-12-15T12:57:53.681-08:00Catch Me If You Can - Part IIIt has been quite some time since I wrote the Catch Me If You
Can post about
the inability of LLVM-backed compilers to catch non-call hardware exceptions. In
other words, hardware exceptions raised by code written in the same block as the
try...except exception handler.
Since then, more LLVM-backed compilers have been added to Delphi, and with
those, more reasons to change common coding practicesDalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com4tag:blogger.com,1999:blog-5330865050433561840.post-1713437079768828652023-09-26T02:27:00.007-07:002023-09-26T10:45:28.801-07:00Coming in Delphi 12: Disabled Floating-Point ExceptionsDelphi 12 is in the works. One of its small features that will have a huge
impact is a change in the default handling of floating-point exceptions, which
will now be masked on all platforms.
Previously, floating-point exception handling was different across platforms,
and most notably on the Windows platform: In VCL and console applications,
floating-point exceptions were not masked and would Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com3tag:blogger.com,1999:blog-5330865050433561840.post-50061810848294084852023-08-10T03:27:00.002-07:002023-08-10T03:27:27.365-07:00Beware of loops and tasksLoops and anonymous methods don't go hand in hand. Any variable that changes for
each iteration will cause problems if it is used within an anonymous
method. I explained the mechanism behind variable capturing in the post
Mysterious Case Of Wrong
Value,
along with the problem it causes with loops and its solution.
Commonly, anonymous methods are used as callbacks or event handlers, where there
Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com6tag:blogger.com,1999:blog-5330865050433561840.post-70877454856378185922023-04-11T06:09:00.002-07:002023-04-12T02:46:40.270-07:00Assigning result to a function from asynchronous codeOne of the more common problems that comes up in multi-threading, especially when
refactoring existing code, is assigning a result obtained from asynchronous
code. In other words, how do you write a function that will return some value, and
calculate that value in a background thread?
Basically, converting the following code:
function GetData(...): string;
var
Data: string;
begin
Data := Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com4tag:blogger.com,1999:blog-5330865050433561840.post-65312000031552663982023-03-03T03:24:00.001-08:002023-03-03T03:25:19.377-08:00DelphiCon 2023: NX Horizon - The Open Source Event Bus for Delphi Replay Did you miss the DelphiCon session on "NX Horizon - The Open Source Event Bus for Delphi"? The webinar replay is now available! Code can be found on GitHub: https://github.com/dalijap/nx-horizonReplay can be found at: https://www.youtube.com/watch?v=axu53AatjHcDalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-63235271898139503532022-12-21T01:51:00.003-08:002023-01-19T06:39:54.670-08:00CodeRage 2022: Challenges of Multi-threaded ProgrammingHere you can find slides from my CodeRage 2022 presentation about Challenges of
Multi-threaded Programming, as well as additional links to code examples
related to the presentation.
Slides: https://dalija.prasnikar.info/dl/DP_CodeRage2022.pdf
Additional links:
Thread-safe Event Bus: https://github.com/dalijap/nx-horizon
Books:
Delphi Event-based and Asynchronous Programming:
Code Examples: Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-21356781415092421972022-11-21T01:04:00.002-08:002022-11-21T01:07:32.768-08:00Zeroing Weak Object ReferencesIn the two-part post series, The purpose of weak references - Part I and The purpose of weak references - Part II,
I wrote about the purpose of weak references in automatic reference counting, as
well as in manual memory management, where they are commonly referred to as
non-owning references.
In ARC, object references to reference-counted objects are unsafe, non-zeroing
weak references, and in Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-86363481117502017122022-11-07T02:23:00.001-08:002022-11-07T02:23:08.845-08:00The purpose of weak references - Part IIIn the
first
part of this post series, I covered owning and non-owning references under
manual memory management, and the purpose of non-owning (weak) references in that
memory model. Following the same use cases, we can now clearly show the purpose
of their counterparts under the automatic reference counting memory model.
Automatic reference counting
Automatic reference counting is a memory Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com4tag:blogger.com,1999:blog-5330865050433561840.post-68949881789177671562022-10-28T00:41:00.003-07:002022-10-28T00:41:37.261-07:00The purpose of weak references - Part IThe terminology "weak and strong references" is commonly used in the context of automatic
reference counting. In the context of manual memory management, we talk about
ownership, and owning and non-owning references.
While the terminology is different, those two different kinds of references represent
the same concept, regardless of the underlying memory management model. Strong
references are Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com1tag:blogger.com,1999:blog-5330865050433561840.post-6758410753700716032022-10-17T03:17:00.001-07:002022-10-17T03:17:45.872-07:00 Delphi Thread Safety Patterns book available on AmazonDelphi Thread Safety Patterns book is now available as paperback on Amazon, too. https://dalija.prasnikar.info/delphitspatt/While the thread safety of a particular piece of code depends on the surrounding context and how it is used, some data types are inherently unsafe, and for some of them, thread safety will depend on the use case and the specific code. Unfortunately, when you look at Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-73273167001484523572022-07-01T01:43:00.004-07:002022-07-01T01:45:36.469-07:00FreeAndNil Debate: Thread SafetyOne of the topics raised in yesterday's FreeAndNil debate was using FreeAndNil in
multi-threaded code for avoiding access to a dangling reference when multiple
threads are accessing and using the same object instance.
Problem is that FreeAndNil is not thread-safe and cannot be used to achieve
thread safety. You cannot call FreeAndNil on a shared object reference in one thread and
then use if Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-27072205018399690132022-06-27T00:49:00.000-07:002022-06-27T00:49:15.399-07:00Book Review: Delphi Legacy ProjectsWe are only few a years away from celebrating Delphi's thirtieth birthday. Because
it has been here for quite some time, many Delphi projects are far from being
brand-new, and almost every Delphi developer is or has previously worked on a project that
can be considered a legacy project.
Maintaining legacy projects is painful. Moving them to a newer Delphi version is even
more so.
A recently Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-42528950511805560672022-06-18T00:17:00.000-07:002022-06-18T00:17:13.322-07:00NX Horizon - Event Bus for Delphi I have published initial release of NX Horizon Event Bus for Delphi as Open Source on GitHub.Featuresimplements the publish/subscribe pattern, decoupling publishers and subscribersevents are categorized by typeany Delphi type can be used as an eventsupports four types of event delivery: synchronous, asynchronous, synchronous in the context of the main thread, and asynchronous in the contextDalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com2tag:blogger.com,1999:blog-5330865050433561840.post-78307329766406901572022-06-09T23:07:00.003-07:002022-06-14T23:18:11.615-07:00Anonymous Threads: Use or Lose?Anonymous threads are simple to use and are highly appealing when you need to
quickly put and execute some small piece of code in a background thread.
They work great when they run and complete, but if you have such a thread running
when you close the application, all hell can break loose.
The main cause of trouble with anonymous threads is self-destruction on completion.
Because of that you Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-30337479344811845542022-05-31T02:29:00.002-07:002022-06-14T23:17:50.464-07:00Just released book: Delphi Thread Safety PatternsDelphi Thread Safety Patterns Bookhttps://dalija.prasnikar.info/delphitspatt/While the thread safety of a particular piece of code depends on the surrounding context and how it is used, some data types are inherently unsafe, and for some of them, thread safety will depend on the use case and the specific code. Unfortunately, when you look at some class, type declaration, or API in isolation, Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-61322111370947877122022-01-18T23:58:00.002-08:002022-01-18T23:59:22.291-08:00Productivity tipsProductivity is a highly subjective topic. What makes one person productive might
not work for another. However, that doesn't mean that sharing your own
experience is not useful for other people. If some of the following tips don't
work for you, that is perfectly fine.š
No distractions
Nothing kills productivity as much as being distracted all the time: kids, coworkers,
bosses, cats, dogs, Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-82713412105274768442021-09-28T06:05:00.000-07:002021-09-28T06:05:13.208-07:00Are generics slow?In the TCoffeeAndCode session about "Code Profiling, Optimization, Performance, and
Memory Leaks", it has been mentioned that generics are slow. However, no specific
details were given, and this topic deserves to be explained in more detail
before people start indiscriminately removing generics from their codebases,
hoping to gain some speed.
You can watch the session replay at: https://Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com3tag:blogger.com,1999:blog-5330865050433561840.post-59493997717309021672021-07-12T01:52:00.001-07:002021-07-12T03:59:13.217-07:00Virtual methods in DelphiVirtual methods enable subtype polymorphism - in other words, they allow
implementing different behavior in descendant classes.
That means if you have a base TShape class with a virtual Paint method, and several
descendant classes like TRectangle, TCircle and TTriangle, then each of those
subclasses can implement a different Paint method to appropriately paint itself.
You can call the Paint Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com3tag:blogger.com,1999:blog-5330865050433561840.post-19252806815541314632021-06-08T02:10:00.002-07:002021-06-08T02:10:45.584-07:00Building better serializationThe "When in Rome, do as the Romans do" blog post series used serialization as
an example of what happens when applying inappropriate coding patterns for some
language. It demonstrated how field-based serialization does not fit well in
Delphi, and how published-property-based serialization is more appropriate.
But it also briefly covered some poor serialization practices, that are not
Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-8775003814556079052021-05-26T09:10:00.001-07:002021-05-26T09:10:32.797-07:00When in Rome, do as the Romans do - Part IIIIn Delphi, properties can be backed by simple fields, but they can also have
getter and setter functions that can be vital to the proper functioning of a
class. They can contain additional initialization code, protect the consistency
of the object's data, prevent invalid input...
Photo: Gary Todd from Xinzheng, China, PDM-owner, via Wikimedia Commons, https://commons.wikimedia.org/wiki/File:Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-53981570319651430352021-05-10T00:17:00.002-07:002021-05-10T00:23:35.176-07:00TCoffeeAndCode: Threading and Concurrency ReplayDid you miss the TCoffeeAndCode session on "All about threading and concurrency"? The webinar replay is now available! Hosted by Jim McKeeth and his guests: Frank Lauter, Primož GabrijelÄiÄ, Olaf Monien, Kelver Merlotti and Dalija Prasnikar. https://www.youtube.com/watch?v=KosLWa08X4QDalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-39979538621070866712021-05-06T01:23:00.000-07:002021-05-06T01:23:22.221-07:00Mastering Delphi Memory Management Webinar ReplayIn case you missed the live session, webinar replay Mastering Delphi Memory Management where I am answering your questions about memory management is available on YouTube. Book intro by Neven Prasnikar Jr. followed by a recording of live Q&A chat hosted by Jim McKeeth.Webinar replay:https://www.youtube.com/watch?v=_0_cUcygmLUBook info:https://dalija.prasnikar.info/delphimmThank you all Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-66337184419541181952021-04-27T04:05:00.000-07:002021-04-27T04:05:17.713-07:00When in Rome, do as the Romans do - Part IIIn the previous article, we learned that native Delphi serialization is based around
published properties, and that other kinds of serialization, like field-based
serialization, don't fit well with the Delphi model.
Photo: Gary Todd from Xinzheng, China, PDM-owner, via Wikimedia Commons, https://commons.wikimedia.org/wiki/File:Colosseum_(48412957912).jpg
We have also learned that Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com0tag:blogger.com,1999:blog-5330865050433561840.post-42167877662942235352021-04-20T05:37:00.000-07:002021-04-20T05:37:35.289-07:00Delphi Memory Management WebinarJoin me this Thursday, Apr 22, 2021 5:00 PM - 6:00 PM CEST for a live Q/A session about Delphi memory management. See you! Register for webinar:https://register.gotowebinar.com/register/474961949731996432Book information:https://dalija.prasnikar.info/delphimm Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com4tag:blogger.com,1999:blog-5330865050433561840.post-29702434715870789462021-04-12T03:55:00.002-07:002021-04-12T03:55:53.081-07:00When in Rome, do as the Romans do - Part IWhile different OOP languages share some common principles and many coding
patterns will be the same or at least fairly similar, there are also some significant
differences that can make some commonly used patterns from one language quite
unsuitable in anotherāand if used, such patterns can cause a lot of trouble.
Photo: Gary Todd from Xinzheng, China, PDM-owner, via Wikimedia Commons, https://Dalija Prasnikarhttp://www.blogger.com/profile/00645794274789883946noreply@blogger.com2