Ben, On 03 Nov 2012, at 03:23, Ben Coman <[email protected]> wrote:
> I am refereshing my familiarity with streams by looking at PharoByExample1 > which says... > "You have to remember that each time you open a stream on a file, you have to > close it too." > > Does that mean if in Workspace I iteratively develop some code to process a > stream, so that I am continually running... > myStream := FileStream readOnlyFileNamed: 'C:\test.txt. > ...so that 'myStream' is overwritten each time such that I lose reference to > the previous stream, and I am no longer able to send #close to it, > am I creating a memory leak in my image ? > > My prior assumption has been that once 'myStream' drops the reference drops > to the previous stream, garbage collection would take care of 'everything'. > > cheers -ben Yes, external resources need to be closed, even though finialization (the garbage collector telling an object that it is about to become garbage) will try to clean up as well but it might take some time. The proper idiom to use is FileStream readOnlyFileNamed: 'C:\test.txt' do: [ :stream | … ]. or in Pharo 2.0 'C:\test.txt' asFileReference readStreamDo: [ :stream | … ]. 'C:\test.txt' asFileReference writeStreamDo: [ :stream | … ]. HTH, Sven -- Sven Van Caekenberghe http://stfx.eu Smalltalk is the Red Pill
