[fpc-pascal] Setting string to be UnicodeString by default

2012-05-15 Thread Olivier Sannier

Hello all,

Is there a way to force the string type to be an alias to UnicodeString 
instead of AnsiString as it is by default?
I tried the obvious "type string = UnicodeString" but of course it is 
not allowed as string is a reserved keyword.


Regards
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] EControlC instead of EZeroDivide when using SSE instructions

2012-08-20 Thread Olivier SANNIER
Hello all,

I'm using FreePascal 2.6.0 under Win32 here and I'm seeing a very weird behavior
as I'm not getting EZeroDivide when doing float divisions, but a rather
surprising EControlC.
As I was writing the code in assembly, I also tried with plain Pascal code, but
I get the same result. Here is the code:

program test;

uses
  classes,
  sysutils;

procedure DirectTestExcept;
const
  A: Double = 5;
  B: Double = 0;
asm
  movsd xmm0, A
  divsd xmm0, B
end;

procedure MyTestExcept;
var
  Tmp: Double;
begin
  Tmp := 0.0;
  Tmp := 5.0 / Tmp;
end;

begin
  try
MyTestExcept;
  except
on E: Exception do
  WriteLn(E.ClassName + ': ' + E.Message);
  end;

  try
DirectTestExcept;
  except
on E: Exception do
  WriteLn(E.ClassName + ': ' + E.Message);
  end;
end.

When I first saw the EControlC, I thought that maybe the initial exception was
eaten up, but it seems it's not the case because the above program gives this
output:

EControlC: Control-C hit
EControlC: Control-C hit

The command line used to compile is the following:

ppc386.exe -alt -gl -CF64 -Cfsse2 -Mdelphi test.dpr

Looking at the generated assembly, the pure pascal one uses the same operands
that my assembly, so it makes sense it behaves similarly.
Does any of you have any suggestion as to explain this behavior, and best of
all, how to fix it?

Regards
Olivier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FillChar/FillByte and Finalize()

2016-03-15 Thread Olivier Sannier

On 15/03/2016 22:14, Krzysztof wrote:

Hi,

Should I (and in which case) call Finalize() when 
using FillChar/FillByte? Heaptrc unit is not detecting any memory 
leak. For example:


type
  PMyRec = ^TMyRec;
  TMyRec = record
Field1: Int64;
Field2: String;
Field3: Int64;
Field4: String;
Field5: Boolean;
  end;

procedure push(ARec: PMyRec);
begin
  writeln(ARec^.Field2,',', ARec^.Field4);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FillChar(f, SizeOf(TMyRec), 0);
  f.Field1 := 1;
  f.Field2 := 'abc';
  push(@f);
  FillChar(f, SizeOf(TMyRec), 0);
  f.Field1 := 2;
  f.Field2 := 'vbnj';
  push(@f);
end;

Regards

Well, looking at your example, all strings are coming from constants in 
the source code, thus no reference counting takes place because no 
memory allocation was needed.

However, if you were to do something like this, I believe it would leak:

  f.Field1 := 1;
  f.Field2 := 'abc' + IntToStr(f.Field1);

This way, you are creating a true string instance, and overwriting the 
pointer with zeroes won't call Finalize.

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


Re: [fpc-pascal] Type-casting a class variable

2024-05-02 Thread Olivier Sannier via fpc-pascal

Hello,

You should cast to TWindowClass as TWindow is used to cast an instance, 
not a class reference.


Note that you may not have TWindowClass, in which case you need to 
declare it like this:


type
    TWindowClass = class of TWindow;

Note that CreateNewWindow would have to be virtual for derived clases 
methods to be called.



Le 01/05/2024 à 16꞉28, Adriaan van Os via fpc-pascal a écrit :

Suppose I have a

var myClass: TClass

and (for example) a

class function TWindow.CreateNewWindow( )

Now I want to call CreateNewWindow for var myClass. Of course, 
depending on the class, CreateNewWindow will behave different. 
Type-casting myClass to TWindow crashes (in my setup). And even if it 
worked, wouldn't it loose the class information ?


Of course, by adding an extra parameter of type TClass to 
CreateNewWindow, the issue can be solved. But that's far from elegant.


How to solve this ? Anything I missed ?

Regards,

Adriaan van Os

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


Re: [fpc-pascal] Custom NewInstance allocator

2024-06-05 Thread Olivier Sannier via fpc-pascal

Could it be because you do not zero out the allocated memory?


Le 04/06/2024 à 10꞉54, Hairy Pixels via fpc-pascal a écrit :

In the manual it athttps://www.freepascal.org/docs-html/ref/refse38.html  it says 
"Calling the constructor will provoke a call to the virtual class method 
NewInstance, which, in its default implementation, calls GetMem, to allocate enough space 
to hold the class instance data, and then zeroes out the memory."

I'm trying this like below but it crashes. Is this correct? The fact 
NewInstance returns TObject instead of Pointer doesn't make sense to me and 
suggests this isn't correct.

   class function TDataObject.NewInstance: TObject;
   begin
 result := TObject(GetMem(InstanceSize));
   end;

Regards,
Ryan Joseph

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