# HG changeset patch # User John W. Eaton # Date 1423863981 18000 # Node ID 5cfb3ccbf24a3dedc1876f1ad9966e414a0e15bb # Parent 1170c849952b479bfbdafbcb2d1d00aef46cb2b6 style fixes for resource management * __osmesa_print__.cc (close_fcn): New function. (F__osmesa_print__): Manage FILE pointer with unwind_protect. Allocate local buffer with OCTAVE_LOCAL_BUFFER. * gl2ps-renderer.cc (gl2ps_print): Check for valid FILE pointer from octave_popen. * oct-parse.in.yy (parse_fcn_file): Check for valid FILE pointer from fopen. diff -r 1170c849952b -r 5cfb3ccbf24a libinterp/corefcn/gl2ps-renderer.cc --- a/libinterp/corefcn/gl2ps-renderer.cc Fri Feb 13 14:02:06 2015 -0500 +++ b/libinterp/corefcn/gl2ps-renderer.cc Fri Feb 13 16:46:21 2015 -0500 @@ -300,15 +300,20 @@ { #ifdef HAVE_GL2PS_H - unwind_protect frame; - FILE *fp = octave_popen (cmd.c_str (), "w"); - frame.add_fcn (safe_pclose, fp); + if (fp) + { + unwind_protect frame; + + frame.add_fcn (safe_pclose, fp); - glps_renderer rend (fp, term); + glps_renderer rend (fp, term); - rend.draw (fig, cmd); + rend.draw (fig, cmd); + } + else + error ("print: failed to open pipe for gl2ps renderer"); #else diff -r 1170c849952b -r 5cfb3ccbf24a libinterp/dldfcn/__osmesa_print__.cc --- a/libinterp/dldfcn/__osmesa_print__.cc Fri Feb 13 14:02:06 2015 -0500 +++ b/libinterp/dldfcn/__osmesa_print__.cc Fri Feb 13 16:46:21 2015 -0500 @@ -27,15 +27,23 @@ #include #endif +#include "oct-locbuf.h" +#include "unwind-prot.h" + #include "defun-dld.h" #include "gl-render.h" #include "gl2ps-renderer.h" #include "graphics.h" - #include "gripes.h" #ifdef HAVE_OSMESA #include "GL/osmesa.h" + +static void +close_fcn (FILE *f) +{ + gnulib::fclose (f); +} #endif DEFUN_DLD(__osmesa_print__, args, , @@ -120,12 +128,7 @@ } // Allocate the image buffer - buffer = malloc (Width * Height * 4 * sizeof (GLubyte)); - if (! buffer) - { - error ("__osmesa_print__: Alloc image buffer failed!\n"); - return retval; - } + OCTAVE_LOCAL_BUFFER (GLubyte, 4 * Width * Height, buffer); // Bind the buffer to the context and make it current if (! OSMesaMakeCurrent (ctx, buffer, GL_UNSIGNED_BYTE, Width, Height)) @@ -168,16 +171,19 @@ else { // write gl2ps output directly to file - FILE *filep; - filep = fopen (file.c_str (), "w"); + FILE *filep = fopen (file.c_str (), "w"); + if (filep) { + unwind_protect frame; + + frame.add_fcn (close_fcn, filep); + glps_renderer rend (filep, term); rend.draw (fobj, ""); // Make sure buffered commands are finished!!! glFinish (); - fclose (filep); } else error ("__osmesa_print__: Couldn't create file \"%s\"", file.c_str ()); @@ -222,7 +228,6 @@ if (v) fp.set_visible ("on"); - free (buffer); OSMesaDestroyContext (ctx); #endif diff -r 1170c849952b -r 5cfb3ccbf24a libinterp/parse-tree/oct-parse.in.yy --- a/libinterp/parse-tree/oct-parse.in.yy Fri Feb 13 14:02:06 2015 -0500 +++ b/libinterp/parse-tree/oct-parse.in.yy Fri Feb 13 16:46:21 2015 -0500 @@ -3869,10 +3869,10 @@ if (! full_file.empty ()) ffile = gnulib::fopen (full_file.c_str (), "rb"); - frame.add_fcn (safe_fclose, ffile); - if (ffile) { + frame.add_fcn (safe_fclose, ffile); + // octave_base_parser constructor sets this for us. frame.protect_var (LEXER);