{"record":{"id":"7ee9560a6a451894","repo":"java-native-access/jna","slug":"library-has-been-unloaded","errorCode":null,"errorMessage":"Library has been unloaded","messagePattern":"Library has been unloaded","errorType":"exception","errorClass":"java.lang.UnsatisfiedLinkError","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/NativeLibrary.java","lineNumber":649,"sourceCode":"     * @param symbolName\n     * @return Pointer representing the global variable address\n     * @throws UnsatisfiedLinkError if the symbol is not found\n     */\n    public Pointer getGlobalVariableAddress(String symbolName) {\n        try {\n            return new Pointer(getSymbolAddress(symbolName));\n        } catch(UnsatisfiedLinkError e) {\n            throw new UnsatisfiedLinkError(\"Error looking up '\" + symbolName + \"': \" + e.getMessage());\n        }\n    }\n\n    /**\n     * Used by the Function class to locate a symbol\n     * @throws UnsatisfiedLinkError if the symbol can't be found\n     */\n    long getSymbolAddress(String name) {\n        if (handle == 0) {\n            throw new UnsatisfiedLinkError(\"Library has been unloaded\");\n        }\n        return this.symbolProvider.getSymbolAddress(handle, name, NATIVE_SYMBOL_PROVIDER);\n    }\n\n    @Override\n    public String toString() {\n        return \"Native Library <\" + libraryPath + \"@\" + handle + \">\";\n    }\n    /** Returns the simple name of this library. */\n    public String getName() {\n        return libraryName;\n    }\n    /**\n     * Returns the file on disk corresponding to this NativeLibrary instance.\n     * If this NativeLibrary represents the current process, this function will return null.\n     */\n    public File getFile() {\n        if (libraryPath == null)","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/NativeLibrary.java#L631-L667","documentation":"NativeLibrary.getSymbolAddress throws UnsatisfiedLinkError when the library handle is 0, meaning the library has already been disposed via dispose() (or finalization) and its native handle released. No further symbol lookups are possible on that instance.","triggerScenarios":"Calling lib.getFunction(...), lib.getGlobalVariableAddress(...), or invoking a previously obtained Function after calling lib.dispose(); holding a Function object past the library's cleanup.","commonSituations":"Explicit NativeLibrary.dispose() in shutdown code followed by late callbacks or pending invocations; garbage collection finalizing the library while a Function is still referenced; reusing a cached library object after unload.","solutions":["Do not dispose the NativeLibrary while Functions derived from it are still in use","Re-acquire the library with NativeLibrary.getInstance(name) after disposal","Reorder shutdown logic so library disposal is the last step","Check for accidental dispose() calls (e.g. in finally blocks or finalizers)"],"exampleFix":"// before\nlib.dispose();\nFunction f = lib.getFunction(\"foo\"); // throws\n// after\nFunction f = lib.getFunction(\"foo\");\nf.invoke(...);\nlib.dispose(); // dispose last","handlingStrategy":"validation","validationCode":"if (lib != null && isDisposed(lib)) {\n    lib = NativeLibrary.getInstance(\"mylib\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    Function f = lib.getFunction(\"foo\");\n} catch (UnsatisfiedLinkError e) {\n    if (e.getMessage().contains(\"unloaded\")) {\n        lib = NativeLibrary.getInstance(\"mylib\"); // reacquire\n    }\n}","preventionTips":["Treat NativeLibrary instances as owning their Functions; dispose only at final shutdown","Do not cache Function objects beyond the library's lifetime","Avoid calling dispose() in finally blocks of code that still uses functions"],"tags":["jna","use-after-dispose","library-unload","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}