简体   繁体   中英

False resource leak warning?

I'm getting a resource leak warnings on 's' and 'p' in this snippet. Is this warning valid?

try (StringWriter s = new StringWriter(); PrintWriter p = new PrintWriter(s)) {
    p.print(InetAddress.getLocalHost().getHostName());
    p.print('/');
    Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
    while (e.hasMoreElements()) {
        NetworkInterface i = e.nextElement();
        System.out.println(i);
        if (i.getHardwareAddress() == null || i.getHardwareAddress().length == 0)
            continue;
        for (byte b : i.getHardwareAddress())
            p.printf("%02x", b);
        return s.toString();
    }
    throw new RuntimeException("Unable to determine Host ID");
}

In this snippet - no. There are no resources to speak of, other than those managed by the JVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM