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

Delphi generic nested classes


Delphi built-in data types and their memory sizes

$
0
0

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.

Integer Data Types

Type Description Pointer
Byte 8-bit unsigned integer PByte
ShortInt 8-bit signed integer PShortInt
Word 16-bit unsigned integer PWord
SmallInt 16-bit signed integer PSmallInt
Cardinal 32-bit unsigned integer PCardinal
LongWord 32-bit unsigned integer (32-bit Windows, OSX32, 32-bit iOS, and Android platforms)
64-bit unsigned integer (64-bit iOS and 64-bit Linux platforms)
PLongWord
FixedUInt 32-bit unsigned integer PFixedUInt
Integer 32-bit signed integer PInteger
LongInt 32-bit signed integer (32-bit Windows, OSX32, 32-bit iOS, and Android platforms)
64-bit signed integer (64-bit iOS and 64-bit Linux platforms)
PLongint
FixedInt 32-bit signed integer PFixedInt
UInt64 64-bit unsigned integer PUInt64
Int64 64-bit signed integer PInt64
NativeUInt 64-bit or 32-bit platform-dependent unsigned integer PNativeUInt
NativeInt 64-bit or 32-bit platform-dependent signed integer PNativeInt

Floating-point Data Types

Type Description Pointer Record
Single Single precision floating-point value (4 bytes) PSingle TSingleRec
Double Double precision floating-point value (8 bytes) PDouble TDoubleRec
Extended Extended precision floating-point value (10 bytes on Win32, but 8 bytes on Win64)
See page about multi-device applications.
PExtended TExtended80Rec
Real Alias of Double N/A N/A

Earlier on, the

Platform-dependent integer types

Type Platform Range Format Alias
NativeInt 32-bit platforms -2147483648..2147483647
(-231..2^31-1)
Signed 32-bit Integer
64-bit platforms -9223372036854775808..9223372036854775807
(-263..263-1)
Signed 64-bit Int64
NativeUInt 32-bit platforms 0..4294967295
(0..232-1)
Unsigned 32-bit Cardinal
64-bit platforms 0..18446744073709551615
(0..264-1)
Unsigned 64-bit UInt64
LongInt 32-bit platforms and 64-bit Windows platforms -2147483648..2147483647
(-231..231-1)
Signed 32-bit Integer
64-bit POSIX platforms include iOS and Linux -9223372036854775808..9223372036854775807
(-263..263-1)
Signed 64-bit Int64
LongWord 32-bit platforms and 64-bit Windows platforms 0..4294967295
(0..232-1)
Unsigned 32-bit Cardinal
64-bit POSIX platforms include iOS and Linux 0..18446744073709551615
(0..264-1)
Unsigned 64-bit UInt64
Note: 32-bit platforms include 32-bit Windows, 32-bit macOS, 32-bit iOS, iOS Simulator and Android.

Platform-Independent Integer Types

Platform-independent integer types always have the same size, regardless of what platform you use. Platform-independent integer types include ShortIntSmallIntLongIntIntegerInt64ByteWordLongWordCardinal, and UInt64.

Platform-independent integer types

Type Range Format Alias
ShortInt -128..127 Signed 8-bit Int8
SmallInt -32768..32767 Signed 16-bit Int16
FixedInt -2147483648..2147483647 Signed 32-bit Int32
Integer -2147483648..2147483647 Signed 32-bit Int32
Int64 -9223372036854775808..9223372036854775807
(-263..263-1)
Signed 64-bit
Byte 0..255 Unsigned 8-bit UInt8
Word 0..65535 Unsigned 16-bit UInt16
FixedUInt 0..4294967295 Unsigned 32-bit UInt32
Cardinal 0..4294967295 Unsigned 32-bit UInt32
UInt64 0..18446744073709551615
(0..264-1)
Unsigned 64-bit

Thomas Mueller blogged about the Alias at [WayBack] Delphi LongWord is not always a 32 bit unsigned integer – twm’s blog (via[WayBack] Did you known that LongWord in Delphi is not (any longer) always a 32 bit unsigned integer? – Thomas Mueller (dummzeuch) – Google+) and I think he understood it backwards from what Embarcadero means:

  • NativeInt on 32-bit platforms is an alias of Integer (so it is Signed 32-bit)
  • NativeInt on 64-bit platforms is an alias of Int64 (so it is Signed 64-bit)

Some more fun links

For a lazy afternoon:

–jeroen

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

$
0
0

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

…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+

$
0
0

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

Norway Delphi Club

Oslo, NO
278 Delphi Programmers

Møt andre programmerere i Norge som bruker CodeGear Delphi, C++Builder og InterBase.

Check out this Meetup Group →

 

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

$
0
0

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)

$
0
0

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

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

$
0
0

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

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


Delphi: when calling TThread.Synchronize, ensure the synchronised method handles exceptions

$
0
0

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

GExperts: Copy Component Names

GitHub – ultraware/DelphiGrpc: DelphiGrpc is a Delphi implementation of the realtime and streaming gRPC protocol (http://grpc.io).

$
0
0

For my link list: [WayBack] GitHub – ultraware/DelphiGrpc: DelphiGrpc is a Delphi implementation of the realtime and streaming gRPC protocol (http://grpc.io).

The following libraries are used:

Via [WayBack] Interesting finding today – DelphiGrpc – a grpc.io implementation in Delphi – Edwin Yip – Google+

–jeroen

Hi, We need to implement a functionality in our VCL application where the user…

$
0
0

In recent Delphi versions one is encouraged to use the routines from System.NetEncoding. Also Soap.EncdDecd is only a wrapper to them. Even if you use the stream based implementations the whole stream is read into a TBytes. So no real gain there any more.

Source: [WayBackHi, We need to implement a functionality in our VCL application where the user…

Related:

–jeroen

Dave’s Development Blog – Using CodeSite on interfaced objects

$
0
0

Reminder to self: [WayBackDave’s Development Blog – Using CodeSite on interfaced objects:

Note to self…

If using CodeSite to log Constructor and Destructor behaviour on interfaced objects, make sure its the first unit in the project so that it is the first unit initialised AND THE LAST unit finalised!

This will save you hours and hours hunting down shutdown AVs 🙁

Or in my projects, the uses list in the Delphi project should be this order:

  1. FastMM4Bootstrap
  2. CodeSite

Via: [WayBackUsing CodeSite on interfaced objects  – David Hoyle – Google+

–jeroen

Convert an object instance into a JSON string and making use of custom attributes – Flix Engineering

$
0
0

On my research list: TensorFlow from other languages

$
0
0

On my research list as it pointed me to TensorFlow imports from both .NET and Delphi: [WayBackFixed by Code: Using TensorFlow™ with Delphi – or how to use a TStack<T> to simulate a RPN calculator.

Links from it:

I like the demo there, as I’ve done RPN calculator with some modeling tools before which makes for a good demo, and it reminds me of the HP 12C financial calculator my dad used to have.

If you like more TensorFlow, then watch the video I linked before:  “Large-Scale Deep Learning with TensorFlow,” Jeff Dean – YouTube

Via

Older conversion try: [WayBack] Converting the TensorFlow C++ headers to object pascal. It has this empty struct defined.typedef struct TF_Tensor TF_Tensor;Think it converts to obj… – Eli M – Google+

–jeroen

 


How to expose a Delphi set type via Soap – Stack Overflow

does anyone know of any Spring.Container examples (preferably non trivial) th…

$
0
0

Via [WayBack] does anyone know of any Spring.Container examples (preferably non trivial) that show how to build an app with a container just referenced from the Compo… – Russell Weetch – Google+:

Stefan Glienke:

The principle of having a composition root has nothing to do with a particular DI container. Its what you eventually get when following the principle if DI: ask for dependencies – don’t create or look for them yourself (aka service locator).

You did not mention it but I guess you mean a VCL application – now the design of the VCL is not particularly built with DI in mind and thus it can be a bit tricky to hook up the application MainForm to the container to get it injected everything. However there are several examples how to achieve that (mainly by using DelegateTo and doing the Application.CreateForm there).

An example explaining this is for instance in [WayBack] How to initialize main application form in Spring4D GlobalContainer?

Many more can be found through Spring4d DelegateTo and Spring4d DelegateTo CreateForm and

–jeroen

Soap Delphi Client end with a timeout for a 1MB call – Stack Overflow

$
0
0

This was a change between IE6 and IE7 on the default time-out decreasing from 3600 seconds to 30 seconds: [WayBack] Soap Delphi Client end with a timeout for a 1MB call – Stack Overflow.

If you want to increase the timeout, then use InternetSetOption. You can get the current value using InternetQueryOption.

In Delphi, THTTPReqResp.Send supports this by setting the various time out options right after creating the request:

    Request := HttpOpenRequest(FInetConnect, 'POST', PChar(FURLSite), nil,
                               nil, nil, Flags, 0{Integer(Self)});
    Check(not Assigned(Request));

    { Timeouts }
    if FConnectTimeout > 0 then
      Check(not InternetSetOption(Request, INTERNET_OPTION_CONNECT_TIMEOUT, Pointer(@FConnectTimeout), SizeOf(FConnectTimeout)));
    if FSendTimeout > 0 then
      Check(not InternetSetOption(Request, INTERNET_OPTION_SEND_TIMEOUT, Pointer(@FSendTimeout), SizeOf(FSendTimeout)));
    if FReceiveTimeout > 0 then
      Check(not InternetSetOption(Request, INTERNET_OPTION_RECEIVE_TIMEOUT, Pointer(@FReceiveTimeout), SizeOf(FReceiveTimeout)));

Related:

–jeroen

Terminate threads during application or not?

$
0
0

I got an interesting question a while ago: should an application terminate (anonymous) threads or not?

A problem is that a thread might not execute unless you call WaitFor before Terminate is called. The reason is that the internal function ThreadProc does not start Execute if the thread is already terminated.

The ThreadProc in the System.Classes unit is an ideal place to set breakpoints in order to see which threads might start.

Other useful places to set breakpoints:

  • TAnonymousThread.Execute
  • TExternalThread.Execute

Execute not being called by ThreadProc is a bug, but it is not documented because QC is gone (taking the below entry with it), it is not in QP and the docwiki never got updated.

Given QC has so much information, I am still baffled that Embarcadero took it down.

Sergey Kasandrov (a.k.a. serg or sergworks) wrote in [WayBack] Sleep sort and TThread corner case | The Programming Works about this bug and refers to WayBack: QualityCentral 35451 – TThread implementation doesn’t guarantee that thread’s Execute method will be called at all .

The really bad thing are the WayBack: QualityCentral Resolution Entries for Report #35451 Resolution “As Designed” implying the design is wrong.

In his post, sergworks implemented the Sleep sorting in Delphi. Related:

Note that application shutdown is a much debated topic. Best is to do as little cleanup as possible: your process is going to terminate soon anyway. No need to close handles or free memory: Windows will do that for you anyway. See for instance:

 

Related to waiting:

Related to executing:

–jeroen

Delphi; on my research list `Error creating form: Root class not found: “”.`

Viewing all 1440 articles
Browse latest View live