Quantcast
Channel: Delphi – The Wiert Corner – irregular stream of stuff
Viewing all 1440 articles
Browse latest View live

Note that the Delphi superobject library has changed to “not maintained” in december 2018, has problems with large address aware

$
0
0

A while ago I found out the [WayBack] not maintained status · hgourvest/superobject@f1c42db · GitHub.

This means you should not use the [WayBack] superobject JSON library in Delphi any more: there won’t be any fixes.

Many people use it, especially because it used to be much more stable than the built-in JSON support of Delphi.

One breaking issue in superobject is the lack of large address space support: due to the pointer calculations in various places, it does not support pointers above the 2 gibibyte boundary as filed in the 2016 [WayBack] Issues with {$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE} · Issue #22 · hgourvest/superobject · GitHub

This gives problems in at least this case:

  • enabling {$SetPEFlags IMAGE_FILE_LARGE_ADDRESS_AWARE} (in older Delphi 7 through 2006 also versions this was {$SetPEFlags $20})
  • using top-down memory allocation, for instance by:
    • a user setting HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Memory Management value AllocationPreference to hex value 00100000
    • using FastMM4 with the (default) {$define AlwaysAllocateTopDown} setting

Example registry file and batch file to enable top-down memory (reboot afterwards):

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management]
"AllocationPreference"=dword:00100000

Command to view:

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" | findstr "AllocationPreference"

Command to enable:

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v AllocationPreference /t REG_DWORD /d 00100000 /f

Command to disable:

reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v AllocationPreference /f

Large Address Aware is a nightmare

Be very very very careful with this, and by enabling Large Address Aware to your executables, as many times they can load 3rd party libraries that often are beyond your control.

Even if there is a slight chance that your code is being used with Large Address Aware enabled, then follow guidelines line in [WayBack] windows – Unit Testing for x86 LargeAddressAware compatibility – Stack Overflow

Summary of [WayBack] memory – Drawbacks of using /LARGEADDRESSAWARE for 32 bit Windows executables? – Stack Overflow:

blindly applying the LargeAddressAware flag to your 32bit executable deploys a ticking time bomb!

by setting this flag you are testifying to the OS:

yes, my application (and all DLLs being loaded during runtime) can cope with memory addresses up to 4 GB.
so don’t restrict the VAS for the process to 2 GB but unlock the full range (of 4 GB)”.

but can you really guarantee?
do you take responsibility for all the system DLLs, microsoft redistributables and 3rd-party modules your process may use?

–jeroen


GExperts: searching for case-insensitive “T*List.Create” but not “TStringList.Create”

$
0
0

Just learned that partial exclusion can be done with the case-insensitive GExperts Grep Search like this:

T[^s][^t][^r][^i][^n][^g].*List.*\.Create

This will skip TStringList.Create, but matches TMyList.Create.

I’d rather have done something like this, but the Delphi RegEx does not support negative lookbehind:

^ *[a-zA-Z0-9_]* *: *T(<!string)[a-zA-Z0-9_]*ListO? *;$

So the alternative is to search for this:

^ *[a-zA-Z0-9_]* *: *T[a-zA-Z0-9_]*ListO? *;$

then exclude all the case insensitive TStringList entries from it, however GExperts did not support that at the time of writing.

This is an intermediate that works for some of the times:

^ *[a-zA-Z0-9_]* *: *T[^s][^t][^r][^i][^n][^g][a-zA-Z0-9_]*ListO? *;$

–jeroen

^ *[a-zA-Z0-9_]* *: *T[^s][^t][^r][^i][^n][^g][a-zA-Z0-9_]*ListO? *$;
^ *[a-zA-Z0-9_]*: .T[a-zA-Z0-9_]*ListO? *;$;
^ *[a-zA-Z0-9_]* *: *T(?!string)[a-zA-Z0-9_]*ListO? *;$

Registering/unregseting Windows file associations with Delphi

$
0
0

More Delphi debug visualizers

$
0
0

Need to try this: overloaded default properties

$
0
0

[Archive.is] Need to try this: … multiple default index properties having the same name …getters can be overloads … resolve …by type signature … – Thomas Mueller (dummzeuch) – Google+, thanks to marck for this brilliantly simple example:

private
  function GetColumnValue(const ColumnName: string): string; overload;
  function GetColumnValue(Index: Integer): string; overload;
  procedure SetColumnValue(Index: Integer; const Value: string);
public
  property Values[const ColumnName: string]: string read GetColumnValue; default;
  property Values[ColumnIndex: Integer]: string read GetColumnValue write SetColumnValue; default;
end;

This means:

  • you can have multiple default indexor properties
  • the multiple indexor properties can have the same name e.g., Values
  • the properties getters can be overloads (i.e. have the same name) e.g., GetColumnValue
  • Delphi will resolve the overloads by type signature

–jeroen

How to Design Early Returns in C++ (Based on Procedural Programming) – Fluent C++

$
0
0

One more thing to take away from Procedural Programming: It’s Back? It Never Went Away – Kevlin Henney [ACCU 2018] – YouTube was explained in [WayBack] How to Design Early Returns in C++ (Based on Procedural Programming) – Fluent C++.

Though in C++, it applies to all programming languages that stem from a procedural background (Pascal, C#, Java, golang, to name just a few).

The article is about keeping an if/else-if/else tree, even when they can be removed becomes some of their bodies perform an early return, as

In C++, as well as in other languages, the return keyword has two responsibilities:

  • interrupting control flow,
  • yielding a value.

It basically comes down to this argument:

Essentially, the argument for Code #1 is that you need to know less to understand the structure of the code.

Indeed, if we fold away the contents of the if statements, Code #1 becomes this:

The structure of the code is very clear. There are 4 different paths based on the year, they’re independent from each other, and each path will determine the boolean result of the function (if it doesn’t throw an exception).

Now let’s see how Code #2 looks like when we fold away the if statements:

And now we know much less. Do the if statements contain a return? Maybe.

Do they depend on each other? Potentially.

Do some of them rely on the last return false of the function? Can’t tell.

With Code #2, you need to look inside of the if statement to understand the structure of the function. For that reason, Code #1 requires a reader to know less to understand the structure. It gives away information more easily than Code #2.

–jeroen

via [WayBack] Kevlin Henney – Google+: How to Design Early Returns in C++ (Based on Procedural Programming) – Fluent C++

The Initialization-Block of a unit that is part of a package. Is it run as part of DLLMain?

$
0
0

The interesting question a while back [WayBack] The Initialization-Block of a unit that is part of a package. Is it run as part of DLLMain? – Alexander Benikowski – Google+ has a simple TL;DR answer: “it depends” on the actual usage of those units.

Way more elaborate, as I dislike language stuff that you need to track down by trial and error what is actually used:

Well, seems so(in our case). Rootcall which triggers the initialization is:
ntdll.ldrLoadDLL
I did not mention, that the BPL is used by a DLL(DLL links against package) which means the package is loaded/initialized by the Os when the DLLMain runs. Odd combination but that seems to be the culprit here.

David Heffernan
Yes, it is triggered from DllMain. And yes, this has massive consequences for what can and cannot be done in initialization sections.

Alexander Benikowski
+David Heffernan in my case. When a packages unit is already initialized by being used from an Exe(which links to the Package), it is not from within a DLLMain.
In my case, both the Application and the dll it loads, both link to the same package. But the unit in question is unused until the DLL is loaded.

Uwe Raabe
The docs for InitializePackage say it calls the initialization parts of all contained units in the package – not only the used ones.

David Heffernan
+Alexander Benikowski​ In that scenario we have load time linking and I guess the package framework handles it differently.

David Millington
Related: don’t forget that class constructors and destructors effectively run in the initialization and finalization sections too. Restrictions or side effects apply there too. docwiki.embarcadero.com – Methods (Delphi) – RAD Studio

Stefan Glienke
+Uwe Raabe Which is not being done when you use it as runtime package. Only initialization sections from unit being used are being run. This caused us several problems in the past where 2 modules (A being the host application exe and B being some DLL that gets loaded at a later point via LoadLibrary) were using a runtime package but only B used a particular unit from the package which caused the initialization code for that unit being executed when B was loaded and hence being executed in the context of the dllmain of B.

The usual solution is then to put those units into some dummy unit forcing the initialization of that unit to be run in A.

Another solution according to your statement could be to call InitializePackage on all the used runtime packages – and there the question is: couldn’t the RTL do that somehow?

+Stefan Glienke I am not sure if that is desirable when done unconditionally. Even when compiled with packages I wouldn’t expect units to be initialized which aren’t actually used. That would perhaps change the behavior of the application depending on whether it is compile with runtime packages or without.
The case is different with dynamic loaded packages. The units in there are obviously not directly used in the first place. As no one can know which units of such a package are used or not, initializing all of them seems like a viable decision.
Of course there will be situations where your proposed behavior might come in handy, but I doubt that this will be a fit for all.

Source: The Initialization-Block of a unit that is part of a package. Is it run as pa…

Related:

–jeroen

Delphi XE8 introduced the dependency directive, but it’s not for Windows

$
0
0

Interesting thread that shows Delphi XE8 introduced a dependency directive: [WayBack] Just curious, in which version was the dependency directive introduced? As in procedure X; external ‘somelib’ dependecy ‘otherlib’; S… – Johan Bontes – Google+

It’s documented as of XE8: [WayBack] Procedures and Functions (Delphi) – RAD Studio: Specifying Dependencies of the Library

The earliest use I could find is in XE4 source C:\Program Files (x86)\Embarcadero\RAD Studio\11.0\source\IBX\IBIntf.pas

–jeroen


web services – Accessing the original TWebRequest object in a Delphi SOAP Server – Stack Overflow

Useful settings for FastMM4 to tracking bottlenecks in multi-threaded memory allocation

$
0
0

Interesting thread that reminded me of the relatively new FastMM feature to track down lock contention and cope with it using release stacks:

[WayBack] I try use FastMM4 to tracking bottleneck in allocating memory. I was inspired by this Primož movie: https://www.youtube.com/watch?v=p-5mJyXvmrc When I… – Jacek Laskowski – Google+

The changes by Primož have added these two new conditional defines to the FastMM4 codebase:

These options are mutually exclusive.

This was the original post it got introduced in [WayBack] The Delphi Geek: Finding Memory Allocation Bottlenecks with FastMM4 and merged into the main repository.

FastMM introduction: [WayBack] The New Memory Manager In BDS 2006 – by Pierre le Riche.

It also taught me about [WayBack] RAMDisk – Software that Accelerates, Protects, Optimizes – Server Memory Products & Services – Dataram.

Primož stores his DCU files there: much faster than SSD, and far less wear on your SSD; see https://youtu.be/p-5mJyXvmrc?t=2675

–jeroen

Video:

Starting points for JSON unmarshaller, that applies a JSON string to an existing object…

IDE Fix Pack 5.92 keyboard binding for finding references

$
0
0

Since I keep forgetting this piece of IDE Fix Pack 5.92 released – DelphiFeeds.com

The new version 5.92 now binds

  • Ctrl+Alt+Enter to “Find References” and introduces
  • Shift+Ctrl+Alt+Enter for “Find Local References”.

No shortcut toggling anymore.

–jeroen

TInterlocked.Exchange for interfaces?

$
0
0

Via [WayBack] TInterlocked.Exchange for interfaces? Since there is no System.SyncObjs.TInterlocked.Exchange overload for interfaces (and the Exchange versio… – Stefan Glienke – Google+

It has made it to this piece in [Archive.issglienke / Spring4D / source / Source / Reactive / Spring.Reactive.pas — Bitbucket:

class function TInterlockedHelper.Exchange<T>(var Target: T;
  const Value: T): T;
begin
  Result := Default(T);
  PPointer(@Result)^ := Exchange(PPointer(@Target)^, PPointer(@Value)^);
  if Assigned(Value) then
    Value._AddRef;
end;

It is similar to the TInterlocked.Exchange methods.

–jeroen

 

GExperts Help: Copy component names

delphi – Sorting TDictionary by a key of Integer in ascending order – Stack Overflow

$
0
0

Great answer on [WayBackdelphi – Sorting TDictionary by a key of Integer in ascending order – Stack Overflow by J… comes down to this:

var
  LDictionary : TDictionary<Integer, string>;
  LArray : TArray<Integer>;
...
  LArray := LDictionary.Keys.ToArray();
  TArray.Sort<Integer>(LArray);

I was trying the wrong direction (functional approach like LArray := LDictionary.Keys.ToArray.Sort(); ), but the above procedural solution works.

Members used:

–jeroen


rvelthuis/BigNumbers: BigInteger, BigDecimal and BigRational for Delphi

GrijjyCloudLogger, remote logging for Windows, iOS, Android, macOS and Linux – grijjy blog

$
0
0

windows – How to simulate drop-down form in Delphi? – Stack Overflow

Working with big data databases in Delphi – Cassandra, Couchbase and MongoDB (Part 3 of 3) – grijjy blog

Re-read “I am looking for a good replacement for INI files for storing large/complex configuration”

$
0
0

Reminder to re-read [WayBackI am looking for a good replacement for INI files for storing large / complex configuration. So far I have used JvApplicatoinIniStorage + a custom INI f… – Thomas Mueller (dummzeuch) – Google+

INI files are textual and allow for comments. They are not good at large bits of information, and are hard to compare because the order is unimportant.

Alternatives like JSON or DFM have similar limitations.

XML is too chatty, and hard to get right by humans.

Related: [WayBack] JSON as configuration files: please don’t

–jeroen

Viewing all 1440 articles
Browse latest View live