3 ms·
The key point is that finalize() is not reliable, so you shouldn't depend on it. The correct idiom is more or less myResource = foo(); try { ... } f
by salmonellaeater 13y ago
The key point is that finalize() is not reliable, so you shouldn't depend on it. The correct idiom is more or less
myResource = foo();
try {
...
} finally {
myResource.release();
}
Some programmers (although I've never met one) try to avoid this boilerplate by writing a finalize() method on myResource that releases it. It's an anti-pattern. C# and Java have syntactic sugar now that makes the boilerplate shorter, which should help.