Recent

Author Topic: Bug in TPlaysound ?  (Read 2475 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Bug in TPlaysound ?
« on: May 02, 2018, 07:43:11 pm »
There is something I cannot understand, which I think maybe a bug.

I wrote a test component using:
Lazarus main menu > Package > New Component

My test component is a child class inherits from TComponent. I added FData and the constructor Create to set a value for FData. I then created a demo project and dropped the test component on the mainform. My test shown GetData always returns 123, which means the constructor Create is called without I manually do MyComponent1 := TMyComponent.Create.

Code: Pascal  [Select][+][-]
  1. unit MyComponent;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils;
  9.  
  10. type
  11.  
  12.   { TMyComponent }
  13.  
  14.   TMyComponent = class(TComponent)
  15.   private
  16.     FData: Integer;
  17.   public
  18.     constructor Create(AOwner: TComponent); override;
  19.     function GetData: Integer;
  20.   end;
  21.  
  22. procedure Register;
  23.  
  24. implementation
  25.  
  26. procedure Register;
  27. begin
  28.   RegisterComponents('Standard',[TMyComponent]);
  29. end;
  30.  
  31. { TMyComponent }
  32.  
  33. constructor TMyComponent.Create(AOwner: TComponent);
  34. begin
  35.   inherited Create(AOwner);
  36.   FData := 123;
  37. end;
  38.  
  39. function TMyComponent.GetData: Integer;
  40. begin
  41.   Result := FData;
  42. end;
  43.  
  44. end.

Problem:

I installed TPlaySound (using OPM), created a demo and dropped a TPlaySound on the form. On my test, TPlaySound.Create was never called, unless I manually call the constructor. I tested on Lazarus 1.8.0 Linux and Lazarus 1.6.0 Windows.

Is it a bug? Or I did something wrong? Can you reproduce the issue?

You can get TPlaySound using Online Package Manager or from here:
https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/playsoundpackage/

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Bug in TPlaysound ?
« Reply #1 on: May 02, 2018, 08:43:32 pm »
My test shown GetData always returns 123, which means the constructor Create is called without I manually do MyComponent1 := TMyComponent.Create.

Since GetData is a public method (not a class method) of TMyComponent, how can you call GetData without instantiating the class via a constructor?

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: Bug in TPlaysound ?
« Reply #2 on: May 02, 2018, 08:55:56 pm »
I created a test project, dropped a TMyComponent on the form and tested using this code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ShowMessage(MyComponent1.GetData.ToString);
  4. end;

It shows the '123', which is the value set by the constructor. And if I put a breakpoint inside TMyComponent.Create, I can see it's really go inside the constructor.

But if I create a test, put a TPlaySound on the form and some breakpoints inside TPlaySound.Create, it will never reach those breakpoints.

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Bug in TPlaysound ?
« Reply #3 on: May 02, 2018, 09:30:40 pm »
It's been a while since I wrote TPlaySound.  Did you notice its class declaration?
Code: Pascal  [Select][+][-]
  1. Tplaysound = class(TAboutPlaySound)

TAboutPlaySound is a class declared in unit aboutplaysound.

Code: Pascal  [Select][+][-]
  1. TAboutPlaySound = class(TComponent)
The purpose of this is to be able to display an About dialog in the Object Inspector at designtime.

You can see this at runtime if you want:
1. Drop the component on a form (playsound1)
2. Drop a button1 on the form
3. On Button1Click call
Code: Pascal  [Select][+][-]
  1. playsound1.About.ShowDialog;
Compile and run.

If a dialog shows when you click the button, it was created in the TAboutPlaySound.Create constructor code.

There's no need for the About property at runtime of course, and splitting the component into designtime and runtime versions is something I never got round to doing.

TPlaySound is a component sorely in need of refurbishment.  I only wrote it as a kludge to play wave files in the Linux version of a game.  I noticed a functional error when reviewing the code just now which I should get round to fixing.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Handoko

  • Hero Member
  • *****
  • Posts: 5129
  • My goal: build my own game engine using Lazarus
Re: Bug in TPlaysound ?
« Reply #4 on: May 03, 2018, 06:37:09 am »
@minesadorada

Thank you for your attention.

After several testings, now I understand the issue. Please read my post here:
https://forum.lazarus.freepascal.org/index.php/topic,41124.msg284866.html#msg284866
« Last Edit: May 03, 2018, 07:56:57 am by Handoko »

 

TinyPortal © 2005-2018