OK. It all boils down to style of programming. You have the following
options:
1. Make the stream a property of the component, so you create it in the
constructor, and free it in the destructor
2. Make it so that the caller always supplies a TStream.
> The function is part of a component, and I was concerned that
> by not freeing
> the stream that is created within that component, that it
> hangs around.
That is true, if you create it, there is no guarantee that the caller would
free it.
> But
> if you free it, it cant pass the result back to the calling
> routine,
That goes without saying.
Perhaps you want your method to look like:
DoSomething (instream, outstream: TStream);
Leave it up to the caller to do the memory allocation, etc. And BTW, you
should use TStream instead of TMemoryStream -> this way, for example, the
caller might create a TFileStream to read the jpeg file in, and another
TFileStream to write it out to. Your code will be able to handle both.
Personally, I would not create the stream, or a free a supplied stream in
the method - I consider this poor style, as it is relatively prone to bugs
and memory leaks.
Hope this helps,
Dennis.
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of Alistair George
> Sent: Friday, 11 February 2000 15:39
> To: Multiple recipients of list delphi
> Subject: [DUG]: Streams, and GetTempPath
>
>
> Tks Dennis, but the Q was:
> > > QUESTION
> > > BTW, you cant free a memorystream at the end of a function,
>
> The function is part of a component, and I was concerned that
> by not freeing
> the stream that is created within that component, that it
> hangs around. But
> if you free it, it cant pass the result back to the calling
> routine, in
> which of course, I eventually free the owner memory stream.
> EG
> Mystream:=gethestream(newstream);
>
> //within the gethestream function a newstream is created and
> passed back as
> a result, but newstream is not freed.
>
> Maybe I am missing the point; I am creating _another_ memory
> stream within
> the function, which is the utility stream, which is passed
> back as a result.
> That is the stream I am concerned about.
>
>
>
> Re other Q about the GetTempPath() function and the string
> allocation. I
> think it was due to windows wanting a Pchar on that call,
> without extraneous
> bytes after the desired lot.
>
> --------------------------------------------------------------
> -------------
> New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
> Website: http://www.delphi.org.nz
>
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz