tags:

views:

99

answers:

3

I'm working with a 3rd party JPEG/EXIF manipulation library (Mediautil) that is causing me some headaches. I want to change an image's EXIF data. To do this, I need to write the updated version to a temporary file, delete the original and then rename the temp file to the original name.

My problem is that the File.delete() call fails and returns false, presumably because the library still has it opened in some way - but I have done everything I can find in the API to make it close all streams. Even worse: the problem seems to be timing-dependant, and the Unit tests where it occurs sometimes fail and sometimes not - but the code is not multithreaded.

Bizarrely, there is one library call that removes the problem - but it also removes the EXIF thumbnail, which I don't actually want. And looking at the code, I absolutely can't see where it closes any streams that might otherwise stay open.

Any ideas how to attack this problem?

Edit: This is on Windows XP, Java 6. And another thing: I have found out that if I call System.gc() before calling File.delete(), it works - presumably because that triggers some finalizer. So it definitely seems to be an unclosed stream.

+2  A: 

I would go for some help with the debugger here. A quick dig through the java.io stuff shows that the only likely finalize() candidate is in FileOutputStream. So slap a breakpoint in there, run your program, and try and get System.gc() to trigger FileOutputStream.finalize() to release your stream. That should give you an answer as to whether or not that's your problem.

Once you can reproduce that, then you need to start matching instantiation of FileOutputStream instances with their finalization. A good debugger will give you internal JVM object identifiers for each object, so if you can track the OIDs as they get created, and track them as they get finalized, then hopefully you'll be able to associate the key call to finalize with a specific call new new FileOutputStream.

Might be a long slog, though, depending how complex your application is.

skaffman
Trying this now. The annoying thing is that I'm pretty sure the culprit is a FileInputStream, and those get used by the JVM for loading classes...
Michael Borgwardt
OK, I now found the leak in the library using this method.
Michael Borgwardt
A: 

Why don't you rename the file before you get the library to open it? Then maybe use Java's File.deleteOnExit() to delete the renamed file. For example:

 File jpeg = new File("image.jpg");
 File temp = new File(jpeg + ".temp.jpg");
 jpg.renameTo(temp);
 SomeObj result = exifLibrary(temp); // or exifLibrary(new FileInputStream(temp);
 OutputStream jpegStream = new FileOutputStream(jpeg);
 output.write(result.bytes();
 output.close();
 temp.deleteOnExit();
 temp.delete();
Adrian Pronk
The problem is that not all files that are read need to have the EXIF data updated.
Michael Borgwardt
A: 

And looking at the code, I absolutely can't see where it closes any streams that might otherwise stay open.

I think you could have identified the real problem; i.e. that the API you are using leaks open file handles by design.

Since this is an open source library you are using you have source code access. Therefore, you should be able to confirm this, and if necessary fix it for yourself. And to be a good citizen, contribute your fix back to the project by submitting a patch.

Stephen C
I was already talking about looking through the library's source code, not the API. And the puzzling thing is that one library call seems to fix the problem (as a side effect), but I can't see where it's closing any streams. Then again, it might only be trigering a GC that runs the finalizers...
Michael Borgwardt
"Then again, it might only be triggering a GC that runs the finalizers..." - that was my theory too. But there's something to try to see if that is the case: run your application with a HUGE heap so that the GC is unlikely to be triggered by heap exhaustion.
Stephen C
Since you've found the leak, my last comment is kind of redundant. But if you hadn't found the leak, it might have helped ... so I'll leave it for posterity.
Stephen C