[fpc-pascal] LoadLibrary, FreeLibrary & Threads (win32)

2023-06-22 Thread Thomas Kurz via fpc-pascal
Dear all,

I have spent 2 days now tracking an access violation in my app which I couldn't 
debug well because it occured somewhere where no debug info was available, but 
obviously during a call to FreeLibrary.

I don't know whether I am allowed to do this or not, but as far as I have read 
from the Microsoft docs about using libraries [1,2] I didn't find a note that 
my approach is forbidden. (Of course, I may have overread it.)

[1] 
https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices
[2] 
https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibrarya

What I did is to call LoadLibrary in a different thread than the main thread. 
The corresponding call to FreeLibrary on the other hand was done in the main 
thread.

Question 1: Is this legal on Windows? If not, we're finished here and you can 
skip the rest ;)

However, if it *is* legal, I'd need some help with FPC. Here's a minimal 
example:

***

library dummylib;

uses
  Classes;

begin
end.

***

program project1;

uses SysUtils, Types, Classes;

type TLoadLibThread = class(TThread)
  procedure Execute; override;
  var FHandle: THandle;
end;

procedure TLoadLibThread.Execute;
begin
  FHandle := LoadLibrary('dummylib.dll');
end;

var h: THandle = NilHandle;

begin
  with TLoadLibThread.Create(FALSE) do begin
WaitFor;
h := FHandle;
Free;
  end;
  Assert (h <> NilHandle);
  FreeLibrary (h); 
end.

***

The call to FreeLibrary causes an "access violation writing to address $14".

I also tried to follow the advice in the "multithreaded application tutorial" 
in the wiki ("initialize the FPC's threading system by creating a dummy 
thread"), but this doesn't make any difference.

I assume that I have some obviouy error and am just too blind to see, so I'd 
appreciate someone pointing me to it.

Thanks,
Thomas

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Subscript pointers

2023-06-22 Thread Michael Van Canneyt via fpc-pascal




On Thu, 22 Jun 2023, Hairy Pixels via fpc-pascal wrote:





On Jun 22, 2023, at 8:41 PM, Michael Van Canneyt via fpc-pascal 
 wrote:

It's for easier pointer math, a construct which has been used in FPC since day 
1 (AFAIK).
Use with care, since no boundary checks are done.


You mean instead of doing p + 1 or you can do p[1]?


Yes.


It may have been used for C compatibility too since an array in C is just a 
pointer.


Yes, the concepts are similar.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Subscript pointers

2023-06-22 Thread Hairy Pixels via fpc-pascal



> On Jun 22, 2023, at 8:41 PM, Michael Van Canneyt via fpc-pascal 
>  wrote:
> 
> It's for easier pointer math, a construct which has been used in FPC since 
> day 1 (AFAIK).
> Use with care, since no boundary checks are done.

You mean instead of doing p + 1 or you can do p[1]? It may have been used for C 
compatibility too since an array in C is just a pointer. 

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Subscript pointers

2023-06-22 Thread Michael Van Canneyt via fpc-pascal




On Thu, 22 Jun 2023, Hairy Pixels via fpc-pascal wrote:


I've forgotten entirely, what does subscripting a pointer do in Pascal? p[0] 
returns 100 but after that garbage. Seems like a c-style array which doesn't 
feel right in the language.

var
 i: Integer;
 p: PInteger;
begin
p := @i;
p^ := 100;
writeln(p[0]);
writeln(p[1]);
writeln(p[2]);


It's for easier pointer math, a construct which has been used in FPC since day 
1 (AFAIK).
Use with care, since no boundary checks are done.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] Subscript pointers

2023-06-22 Thread Hairy Pixels via fpc-pascal
I've forgotten entirely, what does subscripting a pointer do in Pascal? p[0] 
returns 100 but after that garbage. Seems like a c-style array which doesn't 
feel right in the language.

var
  i: Integer;
  p: PInteger;
begin
p := @i;
p^ := 100;
writeln(p[0]);
writeln(p[1]);
writeln(p[2]);

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Legitimate use of for and break

2023-06-22 Thread wkitty42--- via fpc-pascal

On 6/21/23 10:54 AM, Steve Litt via fpc-pascal wrote:

wkitty42--- via fpc-pascal said on Wed, 21 Jun 2023 08:07:59 -0400


On 6/20/23 10:54 PM, Steve Litt via fpc-pascal wrote:

It was a long time ago, but if I remember correctly the
Whitesmith Pascal and Turbo Pascal 2 and 3 had either break or
continue. If I remember correctly, I first learned about those when
learning C.


i'm confused about your statement... first you say break/continue
existed in one of two pascal dialects but then you say you first
learned about the two keywords in C... u, wut?


I meant to say "neither" instead of "either". Sorry for the confusion.


that does make a difference :) thanks...


--
 NOTE: No off-list assistance is given without prior approval.
   *Please keep mailing list traffic on the list where it belongs!*
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal