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

The magic “procedure Touch(var …” construct

Posted by jpluimers on 2021/02/16

procedure Touch(var Argument);  
begin
end;

I included the above code in my blog a long time ago (2014 in fact: Delphi: always watch the compiler Warnings), but never got around to explain the why and how I got it, why it works and why it will likely work forever.

Background

Ever since the early Delphi days, there are three hints about “never used” of which the second often gets in the way during debugging:

(note that these %s only hold for non-managed types, which I also addressed in Why don’t I get the warning W1036 Variable “‘MyStrings’ might not have been initialized”… and Delphi 10.3 Rio got released; I’ll wait a while hoping to see more positive comments).

Usually the compiler is right, but sometimes it is not: [WayBack] Check your compiler warnings and hints. They may still be errors. | Shiftkey Software Blog

So once every while, you need this workaround:

Solution

The solution is to have a method with one untyped var parameter (a var parameter without any type: this way you can pass any field or variable to it) that just does nothing. Often I included only at the place I need it as this single line fragment: procedure Touch(var Argument); begin end;.

Former Delphi compiler engineer and Borland Chief Schientist Danny Thorpe handed this solution, I think it was during or around his famous BorCon99 in Philadelphi (and later BorCon2005 in San Jose) Reading Tea Leaves: The Fine Art of Debugging talk. The talk is not-online, but luckily there are notes and a StackOverflow post:

The session had seemingly simple things like this [WayBack] Shenoy At Work: Set Next Statement in Delphi? with the picture on the right.

Voiding the solution

I’ve seen teams making this method inline, but that voids it. Usually they do not see it as they already resolved the “never used” problem in another way.

Why it still works

Read the rest of this entry »

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

F2084 Internal Error: MA1263 – no relevant results

Posted by jpluimers on 2021/02/11

I tried searching for F2084 Internal Error: MA1263 – Google Search which happened on a complete up to date Delphi 10.1 Berlin installation.

It came from a large unit testing application using truckloads of generic language constructs, and large unit uses cycles.

Could not find anything useful. The error disappeared after recompiling the same application:

–jeroen

Posted in Delphi, Delphi 10.1 Berlin (BigBen), Development, Software Development | Leave a Comment »

The Delphi interface reference counting compiler bug that will never get fixed as it is marked “as designed”

Posted by jpluimers on 2021/02/10

A long time ago, I write a question [WayBack] delphi – Should the compiler hint/warn when passing object instances directly as const interface parameters? – Stack Overflow in 2010.

It was marked by (now former) compiler engineer Barry Kelly [WayBack1/WayBack2] as bug:

It’s a bug. The conversion from instance to interface reference in RunLeakCrash should be to a temporary variable, keeping it alive for the duration of RunLeakCrash.

Added to that was a comment that this has happened since at least Delphi 6, so I filed a bug WayBack QualityCentral Report #: 90482 The compiler should keep a hidden reference when passing freshly created object instances directly as const interface parameters.

Just for years later, it was marked with “As Designed” which means it is never going to be fixed, especially since in the mean time Embarcadero got rid of most the senior Delphi R&D team members and went down the path of hiring contractors.

The problem is that I run into the two manifestations every now and then, and it usually takes a long time debugging to zoom into the actual location of the spots.

First manifestation: crash

This is the bug in the first manifestation (by now the root interface is IInterface instead of IUnknown, and you usually have an interface derived from it):

Read the rest of this entry »

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

How do I pretty-print JSON in Delphi? – Stack Overflow

Posted by jpluimers on 2021/02/09

For my archive: the [WayBack] How do I pretty-print JSON in Delphi? – Stack Overflow answer by [WayBack] Bob:

If you do not want to use any external library, and you’re using a Delphi XE5 or newer, there is a very handy TJson.Format() function in the REST.Json unit.

uses json, REST.Json;

{ ... }    

function FormatJSON(json: String): String;
var
  tmpJson: TJsonObject;
begin
  tmpJson := TJSONObject.ParseJSONValue(json);
  Result := TJson.Format(tmpJson);

  FreeAndNil(tmpJson);
end;

–jeroen

Posted in Delphi, Development, JavaScript/ECMAScript, JSON, Scripting, Software Development | 2 Comments »

delphi – Faster DirectoryExists function? – Stack Overflow

Posted by jpluimers on 2021/02/04

From a while back: [WayBack] delphi – Faster DirectoryExists function? – Stack Overflow:

Q

I use

DirectoryExists (const PathName : String);

to check if a directory is reachable from a computer or not. But if the directory does not exist and the path name is a network path, i.e.

\\computer1\Data

the method takes a very long time to return.

There must be a faster way to determine that a network folder is not reachable. Or can I configure some timeout parameter that DirectoryExists uses internally (I looked at the source code but it just delegates to GetFileAttributes which is defined in kernel32)?

Any ideas?

A

There is no faster way:

any function accessing anything on a remote share will timeout when that share is not available.

If the cause of your timeouts is automatic disconnecting of shares, then these link may help you:

If the application can continue without the completion of the check, then you can put the check in a separate thread, and upon completion of the check, you can update your status in the UI.

Be aware that when you try a multi-threading way, that you have to disprove your code is free of race-conditions and memory leaks. Time-outs in combination with exceptions usually make that a hard task.

Related:

–jeroen

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

jeroenp / wiert.me / Native / Delphi / Apps / Console / DfmTools — Bitbucket

Posted by jpluimers on 2021/02/03

Reminder to self: write some more about the IsBinaryDfmFile and ConvertDfmToText tools in [WayBackjeroenp / wiert.me / Native / Delphi / Apps / Console / DfmTools — Bitbucket

–jeroen

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

Delphi: fixing “W1030 invalid compiler directive true”

Posted by jpluimers on 2021/02/02

I had a “W1030 invalid compiler directive true” in a project that got ported from Delphi XE3 to a much more modern Delphi version.

Luckily I found [WayBack] Porting to XE5 and the “W1030 Invalid compiler directive: ‘true’” warning | The curse of Dennis D. Spreen.

The cause was this on one of the PropertyGroup elements:

        <DCC_DebugInformation>true</DCC_DebugInformation>

This correspondents to the Project Options ->  Delphi Compiler -> Compiling -> Debugging setting to be true which is not supported any more.

It is similar to When the Delphi XE5 commandline compiler fails with error F1026: File not found: ‘False.dpr’

Related: [WayBack] Embarcadero Discussion Forums: XE10 compiler war: [dcc32 Warning] W1030 Invalid compiler directive:’true’

–jeroen

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

Delphi: getting the name of the current unit

Posted by jpluimers on 2021/01/28

If you have a class (like TMyClass) in the current unit, you can get the unit name as follows:

  • inside a method of the class (either instance or class method): call UnitName or UnitScope to get the unit name
  • outside a method of the class: call TMyClass.UnitName or TMyClass.UnitScope

Delphi added UnitName [WayBack] and  Delphi XE2 added UnitScope [Archive.is]. Their implementations are different, but I have not seen classes where the outcome is different.

The code below shows that when the underlying RTTI UnitName field inside a PTypeData referred by a PTypeInfo contains an @ sign, then UnitName takes the part before the @, and UnitScope the part after the @, but I have not yet seen units where the underlying field contains an @ sign.

If you have seen that, please let me know.

I needed this in order to research some unit initialisation order issues.

A post helpful with that was [WayBack] windows – Can I determine the order in which my units have been initialized? – Stack Overflow for which this comment by Ritsaert Hornstra is the most important bit:

Related: If you use a unit in the interface section you know that that unit will be initialized BEFORE the unit that uses that unit. When using units in the implementation section this is not the case. So usually when you are using a unit with a singleton in it, created in it’s initialization section, you should use that unit in the interface section to make sure that it is initialized before use.

There is also an answer [WayBack] by Remko Weijnen that shows how to hack the current initialisation order if you know the address of InitContext inside the System.InitUnits method.

Back to UnitName versus UnitScope, the code:

class function TObject.UnitName: string;
var
  LClassInfo: Pointer;
  S: _PShortStr;
begin
  LClassInfo := ClassInfo;
  if LClassInfo <> nil then
  begin
    S := @PClassData(PByte(LClassInfo) + 2 + PByte(PByte(LClassInfo) + 1)^).UnitName;
    if S^[1] <> '@' then
      Result := UTF8ToString(S^)
    else
      Result := UTF8ToString(Copy(S^, Pos(_ShortStr(':'), S^) + 1, MaxInt));
  end else
    Result := '';
end;

class function TObject.UnitScope: string;
var
  LClassInfo: Pointer;
  S: _PShortStr;
begin
  LClassInfo := ClassInfo;
  if LClassInfo <> nil then
  begin
    S := @PClassData(PByte(LClassInfo) + 2 + PByte(PByte(LClassInfo) + 1)^).UnitName;
    if S^[1] <> '@' then
      Result := UTF8ToString(S^)
    else
      Result := UTF8ToString(Copy(S^, 2, Pos(_ShortStr(':'), S^) - 2));
  end else
    Result := '';
end;

Related:

–jeroen

Posted in Delphi, Development, Software Development | 1 Comment »

Few remember the ancestry of programming languages

Posted by jpluimers on 2021/01/27

A while ago, I saw this tweet:

It mentions LISP (nowadays mostly called Lisp), likely because their derivatives like Scheme and Clojure still actively mention their ancestry.

ALGOL is not in the list, but has had so much influence on modern programming. So here the thread that followed:

–jeroen

Read the rest of this entry »

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

On my research list: Delphi “Package Cache” subtree

Posted by jpluimers on 2021/01/27

On one of the sites, when having some Delphi package registration issues, the standard measure was to delete the complete Package Cache subtree (like HKEY_CURRENT_USER\Software\Embarcadero\BDS\17.0\Package Cache).

Since there is so little information about it, it is on my list of things to eventually research.

Some links I already found, but had no time for to fully read:

Unrelated: You should not delete the folder C:\ProgramData\Package Cache\? – Super User, but matched my initial delphi registry “Package Cache” – Google Search.

Yup, I did it: I added an “Undocumented Delphi” category.

–jeroen

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