xml4r/libxml-ruby — pull request #79 by brettg (2013-11-22 03:06:09+00:00)
Title: Fix IOErrors from using XML::Writer.io

Apparently when `xmlFreeTextWriter()` is called with a `xmlTextWriter` that was created with an `xmlOutputBuffer` the write callback of the `xmlOutputBuffer` can get called as part of the `xmlTextWriter` being freed. In an `XML::Writer` created with `.io` this means that the writer will try to write to the `IO` while being __GCed__, often (in my case at least) with highly confounding results. For example, an `IOError` raised at seemingly random places in the code when the `XML::Writer` being GCed tries to write to a closed file.

This patch adds a `closed` flag to the `rxml_writer_object` struct which gets set before `xmlFreeTextWriter()` is called and then wraps the output buffer's callback to check that flag before actually writing to the `IO`.

I wasn't readily able to create a test case (my guess due to timing issues in the test runner), but here's example code that reliably throws the error before the fix:
```
>> require 'libxml'
=> true
>> File.open('test.xml', 'w') do |file|
>> writer = LibXML::XML::Writer.io(file)
>> writer.start_document
>> writer.write_element('something')
>> writer.end_document
>> end
=> true
>> GC.start
IOError: closed stream
from (irb):9:in `start'
from (irb):9
from /Users/brett/.rbenv/versions/2.0.0-p247/bin/irb:12:in `

'
```

I'm open to help out with other approaches to fix the issue.

Thanks!