News for hackers. Stuff that matters.
Re:My favorite bug: segfaults in Java by DerekL on Sunday March 29, 2015 @02:24AM (#9284080)
This isn't a bug in the compiler or the JVM. The Java spec explicitly allows this optimization to happen. From The Java Language Specification (SE 7), section 12.6.1:

> Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a Java compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner.

Also, the "this" keyword is treated like a parameter. (See the end of section 2.6.1 of the JVM spec.)

Re:My favorite bug: segfaults in Java by barrkel on Sunday March 29, 2015 @08:56AM (#9284653)
Fully agree. This is a well known issue that users of native interop from managed languages commonly encounter when they first start out. One of the first bugs I found in .NET related to this; the Ping component didn't expect that Finalize could be called on the same instance before a method on that instance finished executing.

What is less convenient is that Java doesn't have a handy method to ensure that an object is reachable, that the JVM won't optimize away or eliminate. .NET has GC.KeepAlive; AFAIK, Java doesn't have an equivalent. I asked a question about this on SO[1] some time ago. An approximate equivalent can be cobbled together, with probabilistic assignments to a volatile variable.

[1] http://stackoverflow.com/questions/22457002/whats-the-java-e...

Re:My favorite bug: segfaults in Java by divs1210 on Sunday March 29, 2015 @06:25AM (#9284395)
so then why do you think the segfault is occurring?
Re:My favorite bug: segfaults in Java by ubercow13 on Sunday March 29, 2015 @07:38AM (#9284518)
Because the author of the code didn't take into account this part of the language spec
Re:My favorite bug: segfaults in Java by xxxyy on Sunday March 29, 2015 @06:30AM (#9284400)
It is not a bug in the JVM, it is an obscure bug in the JNI-ed code. As to why this happens - perhaps read the blog post?