@@ -2117,9 +2117,47 @@ void Precompiler::TraceTypesFromRetainedClasses() {
21172117
21182118void Precompiler::DropMetadata () {
21192119 Library& lib = Library::Handle (Z);
2120+ const GrowableObjectArray& null_growable_list =
2121+ GrowableObjectArray::Handle (Z);
2122+ Array& dependencies = Array::Handle (Z);
2123+ Namespace& ns = Namespace::Handle (Z);
2124+ const Field& null_field = Field::Handle (Z);
2125+ GrowableObjectArray& metadata = GrowableObjectArray::Handle (Z);
2126+ Field& metadata_field = Field::Handle (Z);
2127+
21202128 for (intptr_t i = 0 ; i < libraries_.Length (); i++) {
21212129 lib ^= libraries_.At (i);
2122- lib.set_metadata (Array::null_array ());
2130+ metadata ^= lib.metadata ();
2131+ for (intptr_t j = 0 ; j < metadata.Length (); j++) {
2132+ metadata_field ^= metadata.At (j);
2133+ if (metadata_field.is_static ()) {
2134+ // Although this field will become garbage after clearing the list
2135+ // below, we also need to clear its value from the field table.
2136+ // The value may be an instance of an otherwise dead class, and if
2137+ // it remains in the field table we can get an instance on the heap
2138+ // with a deleted class.
2139+ metadata_field.SetStaticValue (Object::null_instance (),
2140+ /* save_initial_value=*/ true );
2141+ }
2142+ }
2143+
2144+ lib.set_metadata (null_growable_list);
2145+
2146+ dependencies = lib.imports ();
2147+ for (intptr_t j = 0 ; j < dependencies.Length (); j++) {
2148+ ns ^= dependencies.At (j);
2149+ if (!ns.IsNull ()) {
2150+ ns.set_metadata_field (null_field);
2151+ }
2152+ }
2153+
2154+ dependencies = lib.exports ();
2155+ for (intptr_t j = 0 ; j < dependencies.Length (); j++) {
2156+ ns ^= dependencies.At (j);
2157+ if (!ns.IsNull ()) {
2158+ ns.set_metadata_field (null_field);
2159+ }
2160+ }
21232161 }
21242162}
21252163
0 commit comments