UF.MAIL

UF (Use Freed) issues are reported when there is an attempt to use resources after they were released. The UF.MAIL warning indicates an attempt to use Java Mail API object after it was closed.

Example 1

Copy
 public Collection<Message> getMessages(final Session session,
                                    final String... folderNames) throws MessagingException {
 final Collection<Message> result = new ArrayList<Message>();
 Store store = session.getStore("pop3");
 store.connect();
 
 for (String folderName : folderNames) {
     Folder folder = getFolder(store, folderName);
     if (folder != null) {
         Message messages[] = folder.getMessages();
         for (int i = 0, n = messages.length; i < n; i++) {
             result.add(messages[i]);
         }
     }
 }
 
 return result;
 }
 
 private Folder getFolder(Store store, final String folderName) throws MessagingException {
 if (folderName == null) {
     return null;
 }
 
 try {
     Folder folder = store.getFolder(folderName);
     folder.open(Folder.READ_ONLY);
     return folder;
 } catch (MessagingException e) {
     store.close();
 }
 return null;
 }

UF.MAIL is reported for the snippet on line 20: Java Mail API object 'store' is closed by method 'getFolder()' in case if MessagingException is thrown on line 38. This is a problem because on the next iteration of the cycle the method 'getFolder()' would be called once more and it will attempt to access the closed store.

Example 2

Copy
 public Collection<Message> getMessages(final Session session,
                                        final String... folderNames) throws MessagingException {
     final Collection<Message> result = new ArrayList<Message>();
     Store store = session.getStore("pop3");
     store.connect();
 
     for (String folderName : folderNames) {
         Folder folder = getFolder(store, folderName);
         if (folder != null) {
             Message messages[] = folder.getMessages();
             for (int i = 0, n = messages.length; i < n; i++) {
                 result.add(messages[i]);
             }
         }
     }
 
     return result;
 }
 
 private Folder getFolder(Store store, final String folderName) throws MessagingException {
     if (folderName == null) {
         return null;
     }
 
     try {
         Folder folder = store.getFolder(folderName);
         folder.open(Folder.READ_ONLY);
         return folder;
     } catch (MessagingException e) {
         store.close();
         throw e;
     }
 }

The snippet from the previous section is fixed. Method 'getFolder()' re-throws the MessagingExcpetion now.