Calling C++ destructor directly in resourceHash.hpp

Ioi Lam ioi.lam at oracle.com
Mon Jun 12 16:15:36 UTC 2017



On 6/11/17 10:20 PM, John Rose wrote:
> On Jun 11, 2017, at 9:19 PM, Ioi Lam <ioi.lam at oracle.com 
> <mailto:ioi.lam at oracle.com>> wrote:
>>
>> I am looking at these two functions in "utilities/resourceHash.hpp":
>
> You are worried about the V destructor running,
> where the V struct is a member of Node (Node::_value).
> In the normal case, running the destructor of Node
> transparently runs the destructors of the K and V
> members of Node.
>
> The place where dropped destructors can happen
> in this sort of pattern is when you overwrite a variable,
> which is point (a) in your example.  Your V::operator=
> is responsible for retiring any resources used by the
> previous value of Node::_value which are not going to
> be used by the new value.
>
> Eventually, when "delete node" happens, whatever
> resources were in use in Node::_value will be freed.
>
I checked and you're right: when "delete node" is called, _value's 
destructor is called.

Our problem here is that if the ResourceHashtable was 
resource-allocated, node is not deleted.

       if (ALLOC_TYPE == C_HEAP) {
         delete node;
       }

I think the reason is here:

void ResourceObj::operator delete(void* p) {
   assert(((ResourceObj *)p)->allocated_on_C_heap(),
          "delete only allowed for C_HEAP objects");
   DEBUG_ONLY(((ResourceObj *)p)->_allocation_t[0] = 
(uintptr_t)badHeapOopVal;)
   FreeHeap(p);
}

The assert seems wrong -- if the object is allocated_on_res_area(), we 
should just fall-through and do nothing. That would allow the destructor 
of Node to be called.

> So I don't think you have to do anything with point (b).
> Your problem, if you have one, is operator=.  Those are
> hard to get right.
>
Ah! I just found out "C++ copy constructor is called when a new object 
is created from an existing object; assignment operator is called when 
an already initialized object is assigned a new value from another 
existing object."

Thanks
- Ioi

> — John
>
>



More information about the hotspot-dev mailing list