Recent

Author Topic: strange pointer behavior [SOLVED]  (Read 1185 times)

Phoenix

  • Full Member
  • ***
  • Posts: 109
strange pointer behavior [SOLVED]
« on: July 12, 2024, 01:26:44 pm »
Hi, here I have a very short code but I don't understand why I get a crash. The file to open for the stream is any one. Problem appearance: always. Lazarus 3.4 64bit FPC: 3.2.2 Win11

I also tested by transforming the pointer via PtrUInt but even before the crash the pointer is identical.. %)
« Last Edit: July 12, 2024, 02:46:35 pm by Phoenix »

Phoenix

  • Full Member
  • ***
  • Posts: 109
Re: strange pointer behavior
« Reply #1 on: July 12, 2024, 01:34:27 pm »
there is the zip above but I also publish the code

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.  { TForm1 }
  13.  
  14.  TForm1 = class(TForm)
  15.   Button1: TButton;
  16.   procedure Button1Click(Sender: TObject);
  17.  private
  18.  
  19.  public
  20.  
  21.  end;
  22.  
  23. var
  24.  Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. type
  33.  PStream = ^TFileStream;
  34.  
  35. function fopen(const filename: String): PStream;
  36. var
  37.  st: TFileStream;
  38. begin
  39.  st:= TFileStream.Create(filename,fmOpenRead);
  40.  if st = nil then
  41.   raise Exception.Create('');
  42.  st.Position:= 0;
  43.  Result:= @st;
  44.  
  45.  ShowMessage(IntToStr(PtrUInt(Result)));
  46. end;
  47.  
  48. function open1(ps: Pointer): Pointer;
  49. begin
  50.  ShowMessage(IntToStr(PtrUInt(ps)));
  51.  ShowMessage(IntToStr(PStream(ps)^.Position));
  52.  
  53.  Result:= ps;
  54. end;
  55.  
  56. procedure open2(ps: Pointer);
  57. begin
  58.  ShowMessage(IntToStr(PStream(ps)^.Position));
  59. end;
  60.  
  61. function open_file(const filename: String): Pointer;
  62. var
  63.  ps: PStream;
  64.  p: Pointer;
  65. begin
  66.  ps:= fopen(filename);
  67.  if ps = nil then
  68.   Exit(nil);
  69.  
  70.  open2(ps);      //<- ok
  71.  p:= open1(ps);  //<- crash
  72.  
  73.  if p <> nil then
  74.  begin
  75.   ps^.Free;
  76.   Exit(nil);
  77.  end;
  78.  
  79.  Result:= nil;
  80. end;
  81.  
  82. procedure TForm1.Button1Click(Sender: TObject);
  83. begin
  84.  open_file('d:\test2.mp3');
  85. end;
  86.  
  87. end.
  88.  

I fixed a bug in the code but it is not relevant to the error.
« Last Edit: July 12, 2024, 02:46:23 pm by Phoenix »

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: strange pointer behavior
« Reply #2 on: July 12, 2024, 01:55:08 pm »
What is that code, and why so complex? Do you have any valid reason for it? I refuse to debug it.( but I know what goes wrong)
If I smell bad code it usually is bad code and that includes my own code.

Phoenix

  • Full Member
  • ***
  • Posts: 109
Re: strange pointer behavior
« Reply #3 on: July 12, 2024, 02:05:56 pm »
thanks Thaddy, yes the code comes from a larger application (translated from c) so it is "complex". I can implement it differently but I'm curious why it doesn't work. Also I would like to keep the code very similar to the original to update it again from the original.

ASerge

  • Hero Member
  • *****
  • Posts: 2342
Re: strange pointer behavior
« Reply #4 on: July 12, 2024, 02:15:06 pm »
Code: Pascal  [Select][+][-]
  1. function fopen(const filename: String): PStream;
  2. var
  3.  st: TFileStream;
  4. begin
  5.  st:= TFileStream.Create(filename,fmOpenRead);
  6.  if st = nil then
  7.   raise Exception.Create('');
  8.  st.Position:= 0;
  9.  Result:= @st;
  10. ...
A pointer to a local variable is returned. The stack is constantly being overwritten and there will be something else.

By the way, classes are already pointers, so it's not necessary to make pointers to pointers.
« Last Edit: July 12, 2024, 02:17:41 pm by ASerge »

Phoenix

  • Full Member
  • ***
  • Posts: 109
Re: strange pointer behavior
« Reply #5 on: July 12, 2024, 02:45:28 pm »
By the way, classes are already pointers
Thank you so much, I forgot about that  :-[!

cdbc

  • Hero Member
  • *****
  • Posts: 1678
    • http://www.cdbc.dk
Re: strange pointer behavior [SOLVED]
« Reply #6 on: July 12, 2024, 04:47:42 pm »
Hi Phoenix
I took the liberty of massaging your code a little bit, now it compiles and runs:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29. function open1(ps: Pointer): Pointer;
  30. begin
  31.  ShowMessage(IntToStr(PtrUInt(ps)));
  32.  ShowMessage(IntToStr(TStream(ps).Position));
  33.  
  34.  Result:= ps;
  35. end;
  36.  
  37. procedure open2(ps: Pointer);
  38. begin
  39.  ShowMessage(IntToStr(TStream(ps).Position));
  40. end;
  41.  
  42. function fopen(const filename: String): TStream;
  43. begin
  44.  Result:= TFileStream.Create(filename,fmOpenRead);
  45.  if Result = nil then
  46.   raise Exception.Create('');
  47.  Result.Position:= 0;
  48. end;
  49.  
  50. function open_file(const filename: String): Pointer;
  51. var
  52.  ps: TStream;
  53.  p: Pointer;
  54. begin
  55.  ps:= fopen(filename);
  56.  if ps = nil then
  57.   Exit(nil);
  58.  
  59.  open2(ps);      //<- ok
  60.  p:= open1(ps);  //<- ok
  61.  
  62.  if p <> nil then
  63.  begin
  64.   ps.Free;
  65.   Exit(nil);
  66.  end;
  67.  
  68.  Result:= nil;
  69. end;
  70. { TForm1 }
  71. procedure TForm1.Button1Click(Sender: TObject);
  72. begin
  73. //  open_file('d:\test2.mp3');
  74.   open_file('/home/bc/Downloads/Zugferd2.pp');
  75. end;
  76.  
  77. end.
  78.  
Basically, you shouldn't just *stuff* C-code into 'Object Pascal', they're 2 very different beasts  %)  :P
HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16201
  • Censorship about opinions does not belong here.
Re: strange pointer behavior [SOLVED]
« Reply #7 on: July 12, 2024, 05:38:59 pm »
My, my, my, he should be lucky the two of you took the trouble to debug it. And explain it.. 8-) :P %)
If I smell bad code it usually is bad code and that includes my own code.

Phoenix

  • Full Member
  • ***
  • Posts: 109
Re: strange pointer behavior [SOLVED]
« Reply #8 on: July 12, 2024, 08:40:26 pm »
I took the liberty of massaging your code a little bit, now it compiles and runs
thanks but I have already modified it by returning the pointer in the correct way. I'm trying to translate: https://github.com/mackron/dr_libs/blob/master/dr_flac.h
Unfortunately it's not easy because the code is complicated to understand so straying too far from the implementation can lead to more problems. So I try to keep the use of pointers as much as possible.

Basically, you shouldn't just *stuff* C-code into 'Object Pascal', they're 2 very different beasts  %)  :P
+1  :P

My, my, my, he should be lucky the two of you took the trouble to debug it. And explain it.. 8-) :P %)
In fact, a trivial mistake, but sometimes, however obvious, they go unnoticed. Unfortunately I was only looking at the value of the pointer..  :D
Maybe tiredness or too much C is bad for my health  :P

cdbc

  • Hero Member
  • *****
  • Posts: 1678
    • http://www.cdbc.dk
Re: strange pointer behavior [SOLVED]
« Reply #9 on: July 12, 2024, 09:13:16 pm »
Hi
For inspiration, you could take a gander at this site: https://github.com/andrewd207/PascalAudio/blob/master/pascalaudioio/flac_classes.pas
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Phoenix

  • Full Member
  • ***
  • Posts: 109
Re: strange pointer behavior [SOLVED]
« Reply #10 on: July 12, 2024, 10:29:51 pm »
Thank you for the link  :)

 

TinyPortal © 2005-2018