UF.JNDI

UF (Use Freed) issues are reported when there is an attempt to use resources after they were released. The UF.JNDI warning indicates an attempt to use a JNDI context after it was closed.

Example 1

Copy
     public void scan(Name name, String... filters) {
         try {
             DirContext ctx = initContext();
             try {
                 for (final String filter : filters) {
                     search(name, filter, ctx);
                 }
             } finally {
                 ctx.close();
             }
         } catch (NamingException e) {
             e.printStackTrace();
         }
     }
 
     private DirContext initContext() throws NamingException {
         Hashtable env = new Hashtable(11);
         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
         env.put(Context.PROVIDER_URL, "ldap:///o=JndiSample,dc=example,dc=com");
 
         DirContext ctx = new InitialDirContext(env);
         return ctx;
     }
 
     private void search(Name name, String filter, DirContext ctx) throws NamingException {
         try {
             final SearchControls searchControls = new SearchControls();
             searchControls.setReturningAttributes(necessaryItemAttributes);
             searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
             final NamingEnumeration<SearchResult> resultNamingEnumeration = ctx.search(name, filter, searchControls);
         } catch (NamingException e) {
             ctx.close();
         }
     }

UF.JNDI is reported for the snippet on line 30: JNDI context 'ctx' is accessed in the for-cycle in method 'scan' (see line 54). This is a problem because 'ctx' could be previously closed on one of the iterations as the result of an exception happening in method 'search' on line 56.

Example 2

Copy
     public void scan(Name name, String... filters) {
         try {
             DirContext ctx = initContext();
             try {
                 for (final String filter : filters) {
                     search(name, filter, ctx);
                 }
             } finally {
                 ctx.close();
             }
         } catch (NamingException e) {
             e.printStackTrace();
         }
     }
 
     private DirContext initContext() throws NamingException {
         Hashtable env = new Hashtable(11);
         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
         env.put(Context.PROVIDER_URL, "ldap:///o=JndiSample,dc=example,dc=com");
 
         DirContext ctx = new InitialDirContext(env);
         return ctx;
     }
 
     private void search(Name name, String filter, DirContext ctx) throws NamingException {
         final SearchControls searchControls = new SearchControls();
         searchControls.setReturningAttributes(necessaryItemAttributes);
         searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
         final NamingEnumeration<SearchResult> resultNamingEnumeration = ctx.search(name, filter, searchControls);
     }

The snippet from the previous section is fixed. Method 'search' no longer closes the stream. Now it is closed by the 'scan' method, which is the method that opened the stream. In general, it is good coding practice to release resources with the same methods in which they were originally allocated.