Re: Cleaning up temporary files

Bob Friesenhahn <[email protected]> Mon, 7 Jan 2013 12:25:13 -0600 (CST)
Newsgroups gmane.comp.video.graphicsmagick.apis
Message-ID <[email protected]>
On Mon, 7 Jan 2013, Felix Saphir wrote:

> Hello,
>
> I'd like to ask a question about how to clean up temporary files created
> by GraphicsMagick:
>
> I have a bunch of programs for automated image processing. Each (main)
> program fork()'s some child processes to do the actual work. Can I just
> call DestroyMagick() before I _exit() a child to clean up temporary
> files? Or is there another way?

Are you encountering a lot of "residue" temporary files?  Ideally, 
code which uses temporary files should remove those temporary files 
immediately when it is done with them.  Sometimes things are not ideal 
such as there might be a bug in the code or some external program does 
not work as expected.  As a safeguard, GM's temporary file allocation 
code keeps a record of all of the temporary files which are still 
allocated.  If you invoke DestroyMagick() just before the program 
exits, then that will invoke DestroyTemporaryFiles() which will unlink 
any temporary files which were not previously removed.

If the child process is crashing for some reason and fails to remove 
its temporary files, then things become more difficult.  In modern GM, 
there are signal handlers (see MagickPanicSignalHandler() and 
MagickSignalHandler() in magick/magick.c) which invoke 
PurgeTemporaryFiles() to unlink any remaining temporary files.  You 
could create a similar signal handler in your program to try to remove 
temporary files if the program is about to die due to receiving a 
signal or will core dump.  You should not invoke DestroyMagick() in a 
signal handler since it may deadlock.  In a C program, it may be ok to 
invoke DestroyMagick() in an atexit() routine.

If the above is still not satisfactory, then you could use the 
approach of creating a temporary directory for each child process to 
use and setting the MAGICK_TMPDIR environment variable so that each 
child process uses its own temporary file directory.  Then you can 
remove that directory when the child process is done.

Bob
-- 
Bob Friesenhahn
[email protected], http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/

------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412