title: Do I have to do StringIO.close()?
body: <p>Some code:</p>
  
  <pre><code>import cStringIO
  
  def f():
      buffer = cStringIO.StringIO()
      buffer.write('something')
      return buffer.getvalue()
  </code></pre>
  
  <p>The <a href="http://docs.python.org/library/stringio.html#StringIO.StringIO.close" rel="noreferrer">documentation</a> says:</p>
  
  <blockquote>
    <p><code>StringIO.close()</code>: Free the memory buffer. Attempting to do further
    operations with a closed StringIO object will raise a ValueError.</p>
  </blockquote>
  
  <p>Do I have to do <code>buffer.close()</code>, or it will happen automatically when buffer goes out of scope and is garbage collected?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I did a test:</p>
  
  <pre><code>import StringIO, weakref
  
  def handler(ref):
      print 'Buffer died!'
  
  def f():
      buffer = StringIO.StringIO()
      ref = weakref.ref(buffer, handler)
      buffer.write('something')
      return buffer.getvalue()
  
  print 'before f()'
  f()
  print 'after f()'
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>vic@wic:~/projects$ python test.py 
  before f()
  Buffer died!
  after f()
  vic@wic:~/projects$
  </code></pre>