United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6618886 Anonymous objects can be destructed immediately and so should not be used
JDK-6618886 : Anonymous objects can be destructed immediately and so should not be used

Details
Type:
Bug
Submit Date:
2007-10-18
Status:
Closed
Updated Date:
2012-10-08
Project Name:
JDK
Resolved Date:
2008-08-29
Component:
hotspot
OS:
generic
Sub-Component:
runtime
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:
hs14 (b03)

Related Reports
Backport:
Backport:

Sub Tasks

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

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

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

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



Hardware and Software, Engineered to Work Together