@@ -77,6 +77,7 @@ impl<T: Trace> Cc<T> {
7777 let cc = ManuallyDrop :: new ( self ) ; // Never drop the Cc
7878
7979 if cc. strong_count ( ) != 1 {
80+ // cc is not unique
8081 // No need to access the state here
8182 return Err ( ManuallyDrop :: into_inner ( cc) ) ;
8283 }
@@ -94,14 +95,15 @@ impl<T: Trace> Cc<T> {
9495
9596 remove_from_list ( cc. inner . cast ( ) ) ;
9697
97- // Disable upgrading weak ptrs
98+ // SAFETY: cc is unique
99+ let t = unsafe { ptr:: read ( cc. inner ( ) . get_elem ( ) ) } ;
100+ let layout = cc. inner ( ) . layout ( ) ;
101+
102+ // Disable upgrading weak ptrs (after having read the layout)
98103 #[ cfg( feature = "weak-ptrs" ) ]
99104 cc. inner ( ) . drop_metadata ( ) ;
100- // There's no reason here to call CounterMarker::set_dropped, since the pointed value will not be dropped
105+ // There's no reason here to call CounterMarker::set_dropped, since the pointed value will not be dropped and the allocation will be freed
101106
102- // SAFETY: cc is unique and no weak pointer can be upgraded
103- let t = unsafe { ptr:: read ( cc. inner ( ) . get_elem ( ) ) } ;
104- let layout = cc. inner ( ) . layout ( ) ;
105107 // SAFETY: cc is unique, is not inside any list and no weak pointer can be upgraded
106108 unsafe {
107109 cc_dealloc ( cc. inner , layout, state) ;
@@ -304,8 +306,6 @@ impl<T: ?Sized + Trace> Drop for Cc<T> {
304306 // Set the object as dropped before dropping and deallocating it
305307 // This feature is used only in weak pointers, so do this only if they're enabled
306308 self . counter_marker ( ) . set_dropped ( true ) ;
307-
308- self . inner ( ) . drop_metadata ( ) ;
309309 }
310310
311311 // SAFETY: we're the only one to have a pointer to this allocation
@@ -318,6 +318,10 @@ impl<T: ?Sized + Trace> Drop for Cc<T> {
318318 "Trying to deallocate a CcBox with a reference counter > 0"
319319 ) ;
320320
321+ // Free metadata (the layout has already been read). Necessary only with weak pointers enabled
322+ #[ cfg( feature = "weak-ptrs" ) ]
323+ self . inner ( ) . drop_metadata ( ) ;
324+
321325 cc_dealloc ( self . inner , layout, state) ;
322326 }
323327 // _dropping_guard is dropped here, resetting state.dropping
0 commit comments