Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6618886

Anonymous objects can be destructed immediately and so should not be used

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • hs14
    • 7
    • hotspot
    • b03
    • generic
    • generic

        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;
          }
        }

              xlu Xiaobin Lu (Inactive)
              dholmes David Holmes
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: