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: when calling TThread.Synchronize, ensure the synchronised method handles exceptions

Posted by jpluimers on 2019/09/17

Since about a decade, TThread has a few overloaded [WayBack] Synchronize methods which all allow some specified synchronised method to run in the context of the main thread:

Any exceptions raised in that methods are caught using [WayBackSystem.AcquireExceptionObject and re-raised in the calling thread.

If that happens, you loose a piece of the stack information. I knew that, but found out the hard way that it does because I had to hunt for bugs through inherited code written by people that did not know.

This was part of the stack trace that code would show during an exception:

Exception EAccessViolation at $004D732F: Access violation at address $00409174 in module ''.....exe''.
Read of address 80808080
StackTrace:
(000D632F){.....exe} [004D732F] System.Classes.TThread.Synchronize$qqrp41System.Classes.TThread.TSynchronizeRecordo (Line 14975, "System.Classes.pas" + 40) + $0
(000D6430){.....exe} [004D7430] System.Classes.TThread.Synchronize$qqrxp22System.Classes.TThreadynpqqrv$v (Line 15007, "System.Classes.pas" + 9) + $A
(005D6E61){.....exe} [009D7E61] IdSync.DoThreadSync$qqrp18Idthread.TIdThreadynpqqrv$v (Line 281, "IdSync.pas" + 21) + $6
(005D6E87){.....exe} [009D7E87] IdSync.TIdSync.SynchronizeMethod$qqrynpqqrv$v (Line 326, "IdSync.pas" + 2) + $8

Exception EAccessViolation at $00409174: Access violation at address $00409174 in module ''.....exe''. Read of address $80808080 with StackTrace
(00008174){.....exe} [00409174] System.@IsClass$qqrxp14System.TObjectp17System.TMetaClass + $C

The first exception has a different address than the one in the exception message.

Which means that you miss the whole stack path to the _IsClass call (the underlying method implementing the as keyword) that the actual exception was initiated at.

And yes: the $80808080 is the FastMM4 marker for freed memory, so this was a use-after-free scenario.

A simple wrapper like this using a central logging facility gave much more insight in the actual cause:

procedure RunLoggedMethod(AMethod: TMethod);
begin
  try
    AMethod();
  except
    on E: Exception do
    begin
      Logger.LogExceptionDuringMethod(E, AMethod);
      raise; // mandatory to stay compatible with the old un-logged code
    end;
  end;
end;

Then call it like this inside a thread descendant:

Synchronize(RunLoggedMethod(MethodToRunInMainThread));

The old code was like this:

Synchronize(MethodToRunInMainThread);

This was quite easy to change, as I already had boiler code around exported DLL functions that had a similar construct (without the raise; as exceptions cannot pass DLL boundaries unless very specific circumstances hold).

Similar methods are needed to encapsulate procedure TIdSync.Synchronize(), procedure TIdSync.SynchronizeMethodprocedure TIdThread.Synchronize(Method: TThreadMethod) and [WayBack] Queue overloads:

–jeroen

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

Delphi / Stefan Glienke: I looked into IntroSort … and the Microsoft implementation … Here is my current implementation

Posted by jpluimers on 2019/09/12

For my link archive: [WayBack] Delphi / Stefan Glienke: I looked into IntroSort … and the Microsoft implementation … Here is my current implementation

[WayBackDelphi IntroSort by Stefan Glienke

–jeroen

https://bitbucket.org/snippets/sglienke/64LG6b/introsort

 

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

C# (and presumably Delphi): why parameterless record constructors are absent

Posted by jpluimers on 2019/09/12

For my link archive.

Full text at: [WayBack] … why the Delphi language does not allow parameterless constructors… – David Heffernan – Google+

Abstract:

+Stefan Glienke deleted his post about parameterless record constructors, presumably due to all the off topic comments.

.net at CLR level does allow parameterless constructors on structs. But the C# language bans them: https://msdn.microsoft.com/en-us/library/saxz13w4.aspx

Jon Skeet posted an answer on SO way back in 2008 on this topic: http://stackoverflow.com/a/333840/ From that answer:

—-
The CLR allows value types to have parameterless constructors, but C# doesn’t. I believe this is because it would introduce an expectation that the constructor would be called when it wouldn’t. For instance, consider this:

MyStruct[] foo = new MyStruct[1000];


—-

My guess is that Embarcadero decided to ban parameterless constructors on Delphi records for the same reason. Or perhaps they just copied the rules from C# without realising that the CLR supported parameterless struct constructors.

References:

--jeroen

Posted in .NET, C#, Delphi, Development, Jon Skeet, Software Development | Tagged: | Leave a Comment »

delphi: you can only access protected identifiers from parent classes in your own “Self” scope, or when you are “friends” with your parent (so you are in the same unit)

Posted by jpluimers on 2019/09/11

An interesting question from a while back: [WayBack] delphi – Should a descendant class’ method’s variable that is identical to Self, have access to its ancestor’s protected methods? – Stack Overflow

In unit A:

TParent = class
protected
  function DoSomething: TParent;
end;

In unit B:

TChild = class(TParent)
public
  procedure DoAnotherThing;
end;

implementation

procedure TChild.DoAnotherThing;
begin
  DoSomething.DoSomething
end;

This won’t compile, throwing a

cannot access protected symbol TParent.DoSomething

The kicker here is that the error message makes you think you are operating in Self context, but you are not as you are calling DoSomething.DoSomething where only the first DoSomething is in your Self context, but the second .DoSomethingis in the context of any TParent instance trying to access a public identifier.

Stefan Glienke posted [WayBack] a more elaborate answer explaining some workarounds.

–jeroen

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

Is there a way to use the JVCL’s TJvXxxAppStorage to store float values as s…

Posted by jpluimers on 2019/09/10

For my link archive: [WayBack] Is there a way to use the JVCL’s TJvXxxAppStorage to store float values as strings (e.g. “10.152”) rather than hex dumps and also control the decimal s… – Thomas Mueller (dummzeuch) – Google+

Relevant sources:

–jeroen

 

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

…short presentation at the Norway Delphi Club[1] meetup, covering some fun and interesting uses of the “new” language features introduce since Delphi7… – Asbjørn Heid – Google+

Posted by jpluimers on 2019/08/27

From [WayBack…short presentation at the Norway Delphi Club[1] meetup, covering some fun and interesting uses of the “new” language features introduce since Delphi7… – Asbjørn Heid – Google+

Yesterday I did a short presentation at the Norway Delphi Club[1] meetup, covering some fun and interesting uses of the “new” language features introduced after Delphi 7.

The main purpose of the talk was to try to be inspirational, and give a sense of what kind of stuff you can do with the language these days. So it’s not very in-depth, but rather tries to showcase several interesting features and tricks.

Anyway, in case it might be useful to others I thought I’d publish the slides and code here as well.

The examples are a nice introduction for when you want to dig deeper in the language itself, for instance when you are digging deeper into Spring4d.

Slides 35 and 36 – though hard for me to grasp initially – show a very nice concept called [WayBack] Partial Application (thanks Stefan Glienke for pointing me at this) by binding a parameter value plus parameter position to an existing function returning the bound function so it becomes easier to tall.

–jeroen

Read the rest of this entry »

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

Delphi Ensuring Left/alLeft or Top/alTop controls are positioned in the right order…

Posted by jpluimers on 2019/08/22

The post [WayBack] Why is my buttons not created in logical order? If I run this, my buttons area created ACB rather than ABC as I expected… – Johan Swart – Google+ reminded me the trouble of Delphi VCL and FMX have with alignment.

Basically you have to position your control away from your intended alignment position in order for it to work. So this fails:

procedure TForm1.FormCreate(Sender: TObject);
var
  myToolBar: TToolBar;
  myCornerButton: TCornerButton;
  i: Integer;
begin
  myToolBar := TToolbar.Create(Self);
  myToolBar.Parent := Self;

  for i := 0 to 2 do
  begin
    myCornerButton := TCornerButton.Create(tbarGrid);
    myCornerButton.Parent := myToolBar;
    myCornerButton.Align := TAlignLayout.Left;
    myCornerButton.Text := Chr(65 + I);
  end;
end;

Basically you have to set myCornerButton.Left to 1 before setting the Align property.

Similar for the Top property and TAlignLayout.Top value.

The same holds for VCL Align values alLeft with setting the Left property and alTop with setting the Top property before setting Align.

See these for the actual properties and types:

See also this question: [WayBack] Delphi: How to programmatically adjust visual ordering of components with align = alTop

–jeroen

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

Delphi built-in data types and their memory sizes

Posted by jpluimers on 2019/08/21

Though 64-bit support was released back in 2011 with Delphi XE2, sometimes I forget which data type are native size and which keep their size no matter the compiler bitness (wiktionary/wikipedia).

This post was motivated by via [WayBack] Having started with Delphi before the Cardinal type was available (Or has it always? I can’t remember.) I routinely declare 32 bit unsigned variables as… – Thomas Mueller (dummzeuch) – Google+

The most simple distinction is between Win32 and Win64, but there are more non-32 bit platforms, so these do not suffice any more:

The easiest for me are the below tables that only got introduced with Delphi 10.2 Tokyo: [WayBack] Delphi Data Types for API Integration – RAD Studio.

I have bolded the ones that change size.

Read the rest of this entry »

Posted in Delphi, Development, Software Development | 5 Comments »

Delphi generic nested classes

Posted by jpluimers on 2019/08/21

Reminder to Self: do not nest T generic types as you’re in for a surprise.

Source: [WayBackDelphi generic nested classes

Via: [WayBack] An interesting question over on SO relating to nested generic classes… – David Heffernan – Google+

The surprise:


type
TClassC<T> = class
private
type
TClassD<T> = class
private
x: T;
end;
end;
var
obj: TClassC<Integer>.TClassD<string>;

what type would you expect obj.x to be? Integer or string?

The compiler hint:

[dcc32 Hint]: H2509 Identifier 'T' conflicts with type parameters of container type

–jeroen

Posted in Delphi, Development, Software Development | 2 Comments »

TestInsight provides a local JSON web-server from the IDE for the test-runner to communicate from

Posted by jpluimers on 2019/08/15

Stefan Glienke shared the TestInsight default JSON web-server location with me through chat; I like it!

Some endpoints:

The mechanism for accessing this JSON server are implemented in the TestInsight.Client.pas

You can find the endpoint base URL in TestInsightSettings.ini which by default looks like this:

[Config]
BaseUrl=http://WIN10-DELPHI:8102

–jeroen

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