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,854 other subscribers

Delphi: do NOT use duplicate GUIDs on interfaces

Posted by jpluimers on 2019/04/04

One of the things when fixing bugs in an old codebase is wading through technical debt.

A quick win is to get rid of duplicate GUIDs when interface portions have been copy-paste re-used:

  1. interfaces with the same GUID are treated the same with as casts even if they are different.
  2. the compiler does not warn about duplicate GUID values**

These searches help big time: it shows the duplicate GUIDs if they have been indented all the same way. Good enough for most situations.

grep -Sh "\[\'{" *.pas | sort

grep -rh "\[\'{" *.pas | sort

It depends which flavour of grep you use (gnu or regular) to specify recursion.

Note that findstr doesn’t cut it as it always shows the filename: [WayBackList files recursively showing only full path and file size from Windows Command Prompt – Super User

** this compiles without warning:

program NoWarningOnDuplicateInterfaceGUID;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  IInterface1 = interface
    ['{ECF26C39-CBFF-488E-A3AB-2629726F1005}']
  end;

  IInterface2 = interface
    ['{ECF26C39-CBFF-488E-A3AB-2629726F1005}']
  end;

var
  Subject: IInterface;
begin
  try
    (Subject as IInterface1)._Release;
    (Subject as IInterface2)._release;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

–jeroen


program NoWarningOnDuplicateInterfaceGUID;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
type
IInterface1 = interface
['{ECF26C39-CBFF-488E-A3AB-2629726F1005}']
end;
IInterface2 = interface
['{ECF26C39-CBFF-488E-A3AB-2629726F1005}']
end;
var
Subject: IInterface;
begin
try
(Subject as IInterface1)._Release;
(Subject as IInterface2)._release;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.