Another thing you should note is that especially when not creating
and then freeing an object in the same func/Proc you should always use nil.
When freeing it use FreeandNil(Object); because just free'ing it does not
necessarily set it to nil as well!  This way you can check it's state easily
before trying to use or create it again somewhere else without causing AV's.
I've also come to believe that it's almost always a good idea to create your
object instance outside the main block of code in which you begin to make
use of the object in anyway...even as little as setting simple properties or
setting methods.  This way you set that entire block within a try...except
and free and nil the object automatically should something go awry.   

from: Robert Meek at: [EMAIL PROTECTED] 
dba "Tangentals Design" home of "PoBoy"
freeware Windows apps and utilities
located at: www.TangentalsDesign.com
Proud to be a moderotor for the
"Delphi Programming Lists" at: elists.org 


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Scott Kellish (@Work)
Sent: Friday, September 30, 2005 9:59 AM
To: Borland's Delphi Discussion List
Subject: Re: Referencing a component

Stephen,
The instance of the object still exists despite not explicitly being
assigned to a variable (local or global). From the limited code provided, it
seems like all of the work is being done within the context of the with
statement which is perfectly legitimate. We don't know what the OnEndPlay
property looks like, but it would typically be something like:

TNotifyEvent = procedure(Sender: TObject) of object;

So that the called event handler gets an pointer to the object which could
then be used to free it.

I would also urge some caution in freeing objects inside event handlers like
David is doing. You have to absolutely sure that the code that calls your
event handler, where you then free the object, does not do further
processing after you return, because the object is effectively dead and
memory released at that point.

Back to the original problem though, if David is looking to somehow
manipulate the instance outside of the PlaySoundCue method then I certainly
agree that a hard reference to it needs to be held somewhere. From the code,
it looks like the dsMixer object is the parent of the new TcbDSMixerChannel
instance. Perhaps its easy enough just to iterate over the dsMixer
Components collection to find the object. There's not enough information
provided to speculate further.

Thanks,
Scott

I am always willing to learn. I do not, however, always enjoy being
taught. - Sir Winston Leonard Spencer Churchill

__________________________________________
SoftSystem Solutions, LLC
18 Ridge Road
Clark, New Jersey 07066 USA
Scott Kellish, Owner
Tel.      +01 732 382 1873
Fax:     +01 732 382 1873
Mobile: +01 732 261-5856
e-mail:  [EMAIL PROTECTED]
__________________________________________


----- Original Message ----- 
From: "Stephen Posey" <[EMAIL PROTECTED]>
To: "Borland's Delphi Discussion List" <[email protected]>
Sent: Thursday, September 29, 2005 10:02 AM
Subject: Re: Referencing a component


> David Duffy wrote:
> > I'm only into Delphi in a small way. (D7 professional)
> > Normally I plonk components on forms and when I need
> > to reference them I just use the component name. But...
> >
> > I'm making a small audio player (freeware) and I am
> > creating the audio channels as required as there are
> > multiple sounds to be played at once sometimes.
> >
> > I'm creating the instance ok (I think) and starting it
> > playing, but can't get how to adjust it while it's in
> > use. (playing) I'm freeing the instance in the OnEndPlay
> > handler and that seem to work out ok. Help please? :-)
> >
> >
> > function TfmMain.PlaySoundCue(CueRow: integer):boolean;
> > var
> >   SoundFilename: TFilename;
> > begin
> >   Result := False;
> >   SoundFilename := GrCueList.Cells[6,CueRow];
> >   if FileExists(SoundFilename) then begin
> >    with TcbDSMixerChannel.Create(dsMixer) do begin
> >     FileName := SoundFilename;
> >     Volume := -(3000-(StrToInt(GrCueList.Cells[4,CueRow])*30));
> >     Pan := StrToInt(GrCueList.Cells[5,CueRow])*30;
> >     Tag := CueRow;
> >     OnEndPlay := OnEndEventPlay;
> >     Name := 'Channel' + IntToStr(CueRow);
> >     Play;
> >     Result := True;
> >    end;
> >   end;
> > end;
>
> Using the WITH statement like this, you've created an "anonymous"
> instance of the component, so  there's no way to refer to it
> outside the WITH statement (which makes me curious how you're
> referring to the instance to "[free] the instance in the
> OnEndPlay handler").
>
> In order to have access to an instance like this in other places,
> you need to provide a global reference, which is most easily done
> by adding an instance variable of the appropriate type as a field
> of the form, something like:
>
>    TfrmMain = class(TForm)
>      ...
>    public
>      MixerChannel: TcbDSMixerChannel;
>      PlaySoundCue(CueRow: integer):boolean;
>    end;
>
> Then in your method:
>
> function TfmMain.PlaySoundCue(CueRow: integer):boolean;
> var
>    SoundFilename: TFilename;
> begin
>    Result := False;
>    SoundFilename := GrCueList.Cells[6,CueRow];
>    if FileExists(SoundFilename) then begin
>     MixerChannel := TcbDSMixerChannel.Create(dsMixer);
>     with MixerChannel do begin
>     ...
>
> Now you can refer to MixerChannel anywhere in your form code.
>
> When you're ready to release the instance, just do:
>
>    MixerChannel.Free;
>
> Does that make sense?
>
> HTH
>
> Stephen Posey
> [EMAIL PROTECTED]
>
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://www.elists.org/mailman/listinfo/delphi
>

CONFIDENTIALITY NOTICE: The information in this message is confidential and
may be legally privileged.  It is intended solely for the addressee.  Access
to this message by anyone else is unauthorized.  If you are not the intended
recipient, any disclosure, copying, or distribution of this message, or any
action or omission taken by you in reliance on it, is prohibited and may be
unlawful.  Please immediately contact the sender if you have received this
message in error and permanently delete this message and any attachments.
Thank you.

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to