RLK.SOCKRLK (Resource Leak) issues are reported when resources are allocated but not properly disposed after use. Failing to properly dispose a resource can lead to such problems as:
An RLK.SOCK warning indicates that a Socket is not closed on exit. Vulnerability and riskResources such as streams, connections and graphic objects must be explicitly closed. The close operation can unblock transactions or flush file changes in the file system. While a resource will eventually be closed by the garbage collector, resource exhaustion can occur before garbage collection starts. Depending on the nature of the resource, various exceptions will be thrown on a failed attempt to allocate another resource, for example: java.io.FileNotFoundException: Too many open files or too many database connections. Mitigation and preventionExplicitly close all resources that have the close method, even those that you think are not doing anything significant. Future code changes will then be safe from such errors. Example 123 public void accept() throws IOException { 24 ServerSocket serverSocket = null; 25 try { 26 serverSocket = new ServerSocket(1034); 27 Socket socket = serverSocket.accept(); // socket created 28 final OutputStream outputStream = socket.getOutputStream(); 29 sendStatus(outputStream); 30 outputStream.close(); 31 } finally { 32 serverSocket.close(); 33 } 34 } RLK.SOCK is reported for the snippet on line 27: 'socket' created by 'serverSocket.accept()' call is not closed after creation. Example 223 public void accept() throws IOException { 24 ServerSocket serverSocket = null; 25 try { 26 serverSocket = new ServerSocket(1034); 27 Socket socket = serverSocket.accept(); // socket created 28 try { 29 final OutputStream outputStream = socket.getOutputStream(); 30 sendStatus(outputStream); 31 outputStream.close(); 32 } finally { 33 socket.close(); // socket closed 34 } 35 } finally { 36 serverSocket.close(); 37 } 38 } The snippet from the previous section is fixed; RLK.SOCK is not reported here. ExtensionThis checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information. |