Could there be an overload of SBCommandReturnObject constructor that takes an fd instead of a FILE*, and pass through to the private method which does its own duping?  Then this typemap could be changed to convert to int instead of to FILE*<br><br>Also I'm OOO until tomorrow now, so I can't look at the code again until then<br><div class="gmail_quote"><div dir="ltr">On Thu, Mar 24, 2016 at 3:56 PM Francis Ricci <<a href="mailto:fjricci@fb.com">fjricci@fb.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">fjricci added inline comments.<br>
<br>
================<br>
Comment at: scripts/Python/python-typemaps.swig:532<br>
@@ -531,3 +524,1 @@<br>
-         file.Clear();<br>
-    }<br>
 }<br>
----------------<br>
zturner wrote:<br>
> zturner wrote:<br>
> > fjricci wrote:<br>
> > > The problem is that here, we save the `FILE*` (`$1`) and let the `File` (`file`) go out of scope. So the `File` gets destructed (but it's after calling `file.Clear()`, so the close doesn't happen). But we still hold onto the `FILE*`, and we pass it, as `$1`, to the API call (not seen here, but it comes right after this block in the generated swig cpp code).<br>
> > I think SWIG explicitly has a mechanism to handle this.  Let me find it.<br>
> It's the `freearg` [[ <a href="http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_nn33" rel="noreferrer" target="_blank">http://www.swig.org/Doc1.3/Typemaps.html#Typemaps_nn33</a> | typemap ]].<br>
><br>
> Basically you can put a rule that just calls `fclose()` on the argument, and it will generate this code after it's called the API.<br>
><br>
> Would this work?<br>
So the issue is that we don't want to `fclose()` after `SetImmediateOutputFile()`, we want it to `fclose()` when we're done writing to the `FILE*`, which happens when the `CommandReturnObject` is destructed.<br>
<br>
<br>
<a href="http://reviews.llvm.org/D18459" rel="noreferrer" target="_blank">http://reviews.llvm.org/D18459</a><br>
<br>
<br>
<br>
</blockquote></div>