Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to delete temporary DLL file in Windows due to AccessDeniedException #10

Closed
mscarlett opened this issue Jul 6, 2015 · 2 comments
Assignees

Comments

@mscarlett
Copy link

Hi Michael, I made a fork of this project at https://github.com/mscarlett/opencv/tree/windows-patch (which I can submit as a pull request) and was able to use the library successfully on Windows. However if the OpenCV.loadLocally() method is called, the TemporaryDirectory.delete() method throws an AccessDeniedError when the JVM is terminating due to not having permission to delete the DLL file in the temporary directory. As a result the binaries still reside in the temporary directory after the program terminates. This issue does not occur in Linux so it appears Windows specific.

The error is likely due to the JVM locking this file once the DLL file is loaded, but I was unable to find documentation regarding why it happens.

Here is the full stack trace in case it helps:

Exception in thread "Thread-1" java.lang.RuntimeException: java.nio.file.AccessDeniedException: C:\Users\Michael\AppData\Local\Temp\5989570558901519688\nu\pattern\opencv\windows\x86_64\libopencv_java249.dll
at nu.pattern.OpenCV$TemporaryDirectory.delete(OpenCV.java:146)
at nu.pattern.OpenCV$TemporaryDirectory$1.run(OpenCV.java:117)
Caused by: java.nio.file.AccessDeniedException: C:\Users\Michael\AppData\Local\Temp\5989570558901519688\nu\pattern\opencv\windows\x86_64\libopencv_java249.dll
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.implDelete(Unknown Source)
at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(Unknown Source)
at java.nio.file.Files.deleteIfExists(Unknown Source)
at nu.pattern.OpenCV$TemporaryDirectory$2.visitFile(OpenCV.java:141)
at nu.pattern.OpenCV$TemporaryDirectory$2.visitFile(OpenCV.java:1)
at java.nio.file.Files.walkFileTree(Unknown Source)
at java.nio.file.Files.walkFileTree(Unknown Source)
at nu.pattern.OpenCV$TemporaryDirectory.delete(OpenCV.java:130)

@mscarlett mscarlett changed the title Unable to delete binaries in Windows due to AccessDeniedException Unable to delete temporary DLL file in Windows due to AccessDeniedException Jul 14, 2015
@mscarlett
Copy link
Author

According to http://stackoverflow.com/questions/453359/how-to-unload-dll-from-java-jvm it is necessary to load the DLL with a custom class loader so that it can be garbage collected to unload the DLL file on Windows. Otherwise this might require spawning a process that deletes the DLL file after the JVM terminates. Apparently Windows does not make it possible to delete DLLs that are still being used.

@michaelahlers
Copy link
Contributor

@vonnieda's fork of this project may have addressed this issue. I strongly recommend migrating to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants