JDK-6618886 : Anonymous objects can be destructed immediately and so should not be used
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2007-10-18
  • Updated: 2012-10-08
  • Resolved: 2008-08-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7 Other
6u14Fixed 7Fixed hs14Fixed
Description
The C++ "resource acquisition is initialization" idiom is used a lot in the hotspot codebase. It has the basic form:

  {  // start block to define lifetime of object
     Foo f(...);   // construct a Foo to acquire the resource
      //... use resource in some way
  }  // end of block causes f to be destructed and releases resource

However, if the local object is anonymous, i.e. defined as:

   {
    Foo (...);   /// oops! anonymous object
    ...
   }

then it is allowed to be destructed after its "last use", which implies immediately after construction. This would obviously not have the desired affect as the "resource", such as a lock, would not be held for the duration of the block.

The Sun Studio compiler does not act in this way (it destructs the anonymous object at the end of the block), but gcc does.

There are two examples of this problem involving MutexLockerEx in osThread_solaris.cpp:

static intptr_t compare_and_exchange_current_callback (
       intptr_t callback, intptr_t *addr, intptr_t compare_value, Mutex *sync) {
  if (VM_Version::supports_compare_and_exchange()) {
     return Atomic::cmpxchg_ptr(callback, addr, compare_value);
  } else {
     MutexLockerEx(sync, Mutex::_no_safepoint_check_flag);
     if (*addr == compare_value) {
       *addr = callback;
       return compare_value;
     } else {
       return callback;
     }
  }
}

static intptr_t exchange_current_callback(intptr_t callback, intptr_t *addr, Mutex *sync) {
  if (VM_Version::supports_compare_and_exchange()) {
    return Atomic::xchg_ptr(callback, addr);
  } else {
    MutexLockerEx(sync, Mutex::_no_safepoint_check_flag);
    intptr_t cb = *addr;
    *addr = callback;
    return cb;
  }
}

Comments
EVALUATION http://hg.openjdk.java.net/jdk7/hotspot/hotspot/rev/f7e6d42d9323
11-08-2008

EVALUATION http://hg.openjdk.java.net/jdk7/hotspot-rt/hotspot/rev/f7e6d42d9323
02-08-2008

EVALUATION I believe the fix is just to avoid using anonymous C++ object.
25-10-2007