@@ -135,7 +135,9 @@ void GDScriptParserRef::clear() {
135135
136136GDScriptParserRef::~GDScriptParserRef () {
137137 clear ();
138- GDScriptCache::remove_parser (path);
138+ if (!abandoned) {
139+ GDScriptCache::remove_parser (path);
140+ }
139141}
140142
141143GDScriptCache *GDScriptCache::singleton = nullptr ;
@@ -183,6 +185,17 @@ void GDScriptCache::remove_script(const String &p_path) {
183185 return ;
184186 }
185187
188+ if (HashMap<String, Vector<ObjectID>>::Iterator E = singleton->abandoned_parser_map .find (p_path)) {
189+ for (ObjectID parser_ref_id : E->value ) {
190+ Ref<GDScriptParserRef> parser_ref{ ObjectDB::get_instance (parser_ref_id) };
191+ if (parser_ref.is_valid ()) {
192+ parser_ref->clear ();
193+ }
194+ }
195+ }
196+
197+ singleton->abandoned_parser_map .erase (p_path);
198+
186199 if (singleton->parser_map .has (p_path)) {
187200 singleton->parser_map [p_path]->clear ();
188201 }
@@ -229,6 +242,13 @@ bool GDScriptCache::has_parser(const String &p_path) {
229242
230243void GDScriptCache::remove_parser (const String &p_path) {
231244 MutexLock lock (singleton->mutex );
245+
246+ if (singleton->parser_map .has (p_path)) {
247+ GDScriptParserRef *parser_ref = singleton->parser_map [p_path];
248+ parser_ref->abandoned = true ;
249+ singleton->abandoned_parser_map [p_path].push_back (parser_ref->get_instance_id ());
250+ }
251+
232252 // Can't clear the parser because some other parser might be currently using it in the chain of calls.
233253 singleton->parser_map .erase (p_path);
234254
@@ -432,6 +452,17 @@ void GDScriptCache::clear() {
432452
433453 singleton->parser_inverse_dependencies .clear ();
434454
455+ for (const KeyValue<String, Vector<ObjectID>> &KV : singleton->abandoned_parser_map ) {
456+ for (ObjectID parser_ref_id : KV.value ) {
457+ Ref<GDScriptParserRef> parser_ref{ ObjectDB::get_instance (parser_ref_id) };
458+ if (parser_ref.is_valid ()) {
459+ parser_ref->clear ();
460+ }
461+ }
462+ }
463+
464+ singleton->abandoned_parser_map .clear ();
465+
435466 RBSet<Ref<GDScriptParserRef>> parser_map_refs;
436467 for (KeyValue<String, GDScriptParserRef *> &E : singleton->parser_map ) {
437468 parser_map_refs.insert (E.value );
0 commit comments