The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,862 other subscribers

Archive for the ‘Delphi’ Category

Delphi debugging tip: keep an eye on a object field of an object that will go out of scope eventually

Posted by jpluimers on 2021/01/26

Every once in a while you want to have a Delphi Watch of a field inside an object (say an object of type TContext), except that the field has no value yet, but eventually will point to another object (say an object of type TUser).

However, the original object will go out of scope, so you need to employ a few tricks.

First of all, you get the address of that field:

@(Context().FCurrentUser) = $7EF6F624

Then you watch the content of that field:

TUser(PPointer($7EF6F624)^),r = nil

To get to this trick, you have to remember:

  1. The contents of address $7EF6F624 is pointer (at first nil) to a TUser.
  2. You get to the contents of the address $7EF6F624 by using PPointer($7EF6F624)^.
  3. Then you cast it to the object you want with the TUser(...) cast.

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

If you use an implementation of TNonRefCountInterfacedObject, then document in the descendants how lifetime management is arranged for

Posted by jpluimers on 2021/01/21

There are a few TNonRefCountInterfacedObject (or maybe better named TNonReferenceCountedInterfacedObject) implementations around (see list of links below).

They can be used to expose interfaces, but do not provide interface reference counting. This means you have to do your own lifetime management, which can bring quite a few headaches.

So each class you descend from it must have proper motivation on why, and how lifetime management is performed.

One thing you can do is mark the class with a hint directive like [WayBack] library.

In addition, [Archive.is] TNonRefCountInterfacedObject / [Archive.is] TNonReferenceCountedInterfacedObject implementations should at least implement [WayBack] IInterface (or [WayBack] IUnknown in really old Delphi versions); I have seen implementations that don’t but just provide almost empty [WayBackQueryInterface, [WayBack] _AddRef and [WayBack] _Release methods).

Some examples via  “TNonRefCountInterfacedObject” – Google Search:

Delphi RTL/VLC/FMX

I used this GExperts Grep Search expression to find the entries below: (_AddRef|_Release|QueryInterface)

Delphi itself has a few implementations of non-standard interface management that have good documentation on their use cases. So take a look at at least these before doing something odd with interface implementations yourself:

  • [WayBack] TAggregatedObject in System
    • This redirects all IInterface implementations to a controller
    • It does not implement IInterface itself, so a descendent must add the interface reference
    • Descendants are TContainedObject and TPropertyPageImpl (the latter used by TActiveXPropertyPage)
  • [WayBack] TContainedObject in System
    • This redirects all IInterface implementations except QueryInterface to a controller
    • Descendants are for instance TSOAPHeaders (via TSOAPHeadersBase) used by TSoapPascalInvoker, TInvokableClass and TRIO, and TConnectionPoint used by TConnectionPoints
  • [WayBack] TInterfacedPersistent in System.Classes
    • This supports the notion of (potentially) being owned by another TPersistent. Classes like TCollectionItem, TFieldOptions and TEditButton implement this ownership behaviour.
    • When owned, then redirect reference counting to the owner (if that owner implements IInterface), but not QueryInterface
    • When not owned, then it is effectively non-reference counted
  • [WayBack] TComponent in System.Classes
    • This supports the notion of (potentially) being owned by another TComponent. Classes like TComponent and TCollectionItem implement this ownership behaviour.
    • When owned, then redirects all IInterface calls to the owner (including QueryInterface).
  • [Archive.is] TXPEditReaderStream in DUnit XP_OTAEditorUtils. It is largely undocumented.
  • TXPInterfacedObject in DUnit XPInterfacedObject. It is largely undocumented too.

Not so good examples:

  • [WayBack] TCustomVariantType in System.Variants (which is basically TSingletonImplementation with a lot of extra methods)
  • [WayBack] TSingletonImplementation in System.Generics.Defaults(which is basically what most TNonRefCountInterfacedObject implementations do; this is sort of OK as it is meant to be used only in [WayBack] TCustomComparer<T> and descendants that are supposed to be singletons).
  • IUnknown in Winapi.Ole2 (this whole unit is undocumented; the class only has virtual abstract methods; the unit – together with Winapi.OleCtl – seems to be only meant to support the depcrecated Vcl.OleAuto unit.)

And of course there is the standard implementation with proper multi-threading support:

There are quite a few classes that implement reference counting while descending from things like TComponent, usually without the proper multi-threading support that TInferfacedObject has:

Read the rest of this entry »

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development | Leave a Comment »

grep for Delphi .dproj file containing copy commands for certain DLLs

Posted by jpluimers on 2021/01/20

I always forget the syntax, so this quick grep helps me finding the lines in Delphi .dproj files that have the right copy statements for getting certain DLLs in the output directory.

Those are very useful to copy for instance the FastMM or OpenSSL DLLs from a central location.

[WayBack] GNU grep (which shows filenames and supports UTF-8 and UTF-16):

grep -inS copy *.dproj | grep -i ssl | grep -i dll | grep -v amp

Good old Borland grep:

grep -ind copy *.dproj | grep -i ssl | grep -i dll | grep -v amp

The amp trick excludes any lines having amp in them, incuding the &amp; lines that are duplicated by the IDE throughout the .dproj file to keep build configurations linked correctly.

Related:

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

The Indy unit `IdObjs` got removed after Delphi 2007

Posted by jpluimers on 2021/01/19

I came across some very old code that used the IdObjs unit as it was depending on the TIdStringStream type.

Digging around, I found the unit has been removed from Indy after the Delphi 2007 era.

If you need to transition away, these links – including an old version of it – will help:

Note it is still referenced from [WayBack] indy/IdTestObjs.pas at master · graemeg/indy · GitHub.

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

GitHub – pierrejean-coudert/delphi-libraries-list: List of Delphi Libraries and Frameworks

Posted by jpluimers on 2021/01/14

For my link archive: [WayBackGitHub – pierrejean-coudert/delphi-libraries-list: List of Delphi Libraries and Frameworks

–jeroen

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development | Leave a Comment »

Delphi TVirtualMethodInterceptor like magic: some links of useful stuff made with it

Posted by jpluimers on 2021/01/13

I think TVirtualMethodInterceptor is a really nice things added to Delphi XE: it allows you to overwrite virtual methods of a class.

Since the documentation from that era cannot be saved in the WayBack machine, here is some more recent documentation:

Over the years, the underlying RTTI has been improved, so it can now do more than in the past, but is still cumbersome to use, see for instance [WayBack] tutorial script delphi Changing component class at run-time on demand – CODE Q&A Solved and [WayBack] TVirtualMethodInterceptor (Delphi) – RAD Studio Code Examples and [WayBack] Entropy Overload: Virtual method interception and [WayBack] Playing around with TVirtualMethodInterceptor | Delphi Haven with TVirtualMethodLogger.

Luckily there are more powerful alternatives based on the same ideas and/or underlying implementationIn fact, it can do so much for instance on the Spring4D and DSharp frameworks.

A demo is at [WayBack] Delphi sorcery: Pimp your unit tests using mock objects or [WayBack] Delphi sorcery: AOP and duck typing in Delphi.

The demo of the mocking framework was at code.google.com/archive/p/delphisorcery, but now is part of bitbucket.org/sglienke/dsharp at bitbucket.org/sglienke/dsharp/src/master/Source/Testing.

Some relevant directories:

Basically it is a playground for the more often updated and stable Spring4D where these directories and files are relevant:

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Debugging a Delphi – Format and Pointers/Hex Values (Memory Overwrite) – Stack Overflow

Posted by jpluimers on 2021/01/12

A long time ago (Delphi XE2 era), David Dubois and I helped out Doug find either the cause or a workaround of a memory overwrite in a 32-bit application.

The problem was that a call to Format for a pointe or hex value would give corrupt results, but with a regular integer value it would succeed.

Doug created an example [WayBack] overwriting some memory to simulate the problem.

I provided some steps [WayBack] to help trace such cases down.

In the and a rebuild – first withut madExcept, later with madExcept turned on again – solved the problem.

Likely this was the Delphi IDE becoming corrupt, something which I have seen intermittently over the last two decades.

Be sure to read both answers at [WayBack] delphi – Format and Pointers/Hex Values (Memory Overwrite) – Stack Overflow

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Named Pipes unit for Delphi | Mick’s Mix

Posted by jpluimers on 2021/01/07

[WayBack] Named Pipes unit for Delphi | Mick’s Mix  by Russell Libby, for which (Apr 7, 2013) Francoise Piette has updated this source code for Delphi XE3 and put it on his website at [WayBackOverByte – Blog Source Code as [WayBack] IpcUsingPipes.zip.

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »

Want to use SmartPointers in Delphi? Use the Spring4D one: it is unit tested and optimised

Posted by jpluimers on 2021/01/06

A while ago, there was a nice discussion on smart pointers at [WayBack] Smart Pointers – Generics vrs non-generic implementastion – RTL and Delphi Object Pascal – Delphi-PRAXiS [en].

Conclusion from that:

  • many people think that reference counted interfaces are the same as Smart Pointers
    (basically Smart Pointers are the next level and of course they are based on reference counting)
  • there are a lot of Smart Pointer implementations, but few have a test suite, nor are optimised , nor easy to use
  • The combo Shared/IShared<T>/TShared<T> from Spring4D has all of the above advantages
  • in order to optmise Smart Pointer implementations, you really have to well know the effects of modern Delphi language constructs on the compiler in various target platforms

The discussion mentioned above includes both feature and speed comparisons.

I was a bit amazed that at CodeRage 2018, Marco Cantu introduced yet another smart pointer implementation: one worse than existing implementations, and one with only basic demonstration code, leaving out a test suite.

There have many posts on my blog about smart pointers (see the list below), but Spring4D smart pointer implementation has been around for such a long time that any well respected Delphi developer by now should use them. The source is at  Shared/IShared (search for {$REGION 'Shared smart pointer'} at the current repository).

This list below on my Smart Pointer related blog posts might not be fully complete, but at least mentions that by now you should be using Spring4D.

Some comments on the CodeRage 2018 demos

Read the rest of this entry »

Posted in Conference Topics, Conferences, Delphi, Development, Event, Software Development, Spring4D | Leave a Comment »

IDEIds11…IDEIds21 – RAD Studio

Posted by jpluimers on 2021/01/05

It looks like there are pages  [WayBack] IDEIds21 – RAD Studio … [WayBack] IDEIds21 – RAD Studio.

Maybe I ever find time to find out where they are referenced from and why there is no IDEIds1 page.

–jeroen

Posted in Delphi, Development, Software Development | Leave a Comment »