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:
- interfaces with the same GUID are treated the same with
ascasts even if they are different. - 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: [WayBack] List 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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