@@ -349,17 +349,20 @@ bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
349
349
return true ;
350
350
}
351
351
352
- bool ModuleList::RemoveIfOrphaned (const Module *module_ptr ) {
353
- if (module_ptr ) {
352
+ bool ModuleList::RemoveIfOrphaned (const ModuleWP module_wp ) {
353
+ if (auto module_sp = module_wp. lock () ) {
354
354
std::lock_guard<std::recursive_mutex> guard (m_modules_mutex);
355
355
collection::iterator pos, end = m_modules.end ();
356
356
for (pos = m_modules.begin (); pos != end; ++pos) {
357
- if (pos->get () == module_ptr) {
358
- if (pos->use_count () == 1 ) {
357
+ if (pos->get () == module_sp.get ()) {
358
+ // Since module_sp increases the refcount by 1, the use count should be
359
+ // the regular use count + 1.
360
+ constexpr long kUseCountOrphaned = kUseCountModuleListOrphaned + 1 ;
361
+ if (pos->use_count () == kUseCountOrphaned ) {
359
362
pos = RemoveImpl (pos);
360
363
return true ;
361
- } else
362
- return false ;
364
+ }
365
+ return false ;
363
366
}
364
367
}
365
368
}
@@ -386,7 +389,7 @@ size_t ModuleList::RemoveOrphans(bool mandatory) {
386
389
made_progress = false ;
387
390
collection::iterator pos = m_modules.begin ();
388
391
while (pos != m_modules.end ()) {
389
- if (pos->use_count () == 1 ) {
392
+ if (pos->use_count () == kUseCountModuleListOrphaned ) {
390
393
pos = RemoveImpl (pos);
391
394
++remove_count;
392
395
// We did make progress.
@@ -832,7 +835,7 @@ class SharedModuleList {
832
835
if (!module_sp)
833
836
return false ;
834
837
std::lock_guard<std::recursive_mutex> guard (GetMutex ());
835
- RemoveFromMap (* module_sp. get () );
838
+ RemoveFromMap (module_sp);
836
839
return m_list.Remove (module_sp, use_notifier);
837
840
}
838
841
@@ -843,10 +846,10 @@ class SharedModuleList {
843
846
ReplaceEquivalentInMap (module_sp);
844
847
}
845
848
846
- bool RemoveIfOrphaned (const Module *module_ptr ) {
849
+ bool RemoveIfOrphaned (const ModuleWP module_wp ) {
847
850
std::lock_guard<std::recursive_mutex> guard (GetMutex ());
848
- RemoveFromMap (*module_ptr , /* if_orphaned=*/ true );
849
- return m_list.RemoveIfOrphaned (module_ptr );
851
+ RemoveFromMap (module_wp , /* if_orphaned=*/ true );
852
+ return m_list.RemoveIfOrphaned (module_wp );
850
853
}
851
854
852
855
std::recursive_mutex &GetMutex () const { return m_list.GetMutex (); }
@@ -886,16 +889,22 @@ class SharedModuleList {
886
889
m_name_to_modules[name].push_back (module_sp);
887
890
}
888
891
889
- void RemoveFromMap (const Module &module , bool if_orphaned = false ) {
890
- ConstString name = module .GetFileSpec ().GetFilename ();
891
- if (!m_name_to_modules.contains (name))
892
- return ;
893
- llvm::SmallVectorImpl<ModuleSP> &vec = m_name_to_modules[name];
894
- for (auto *it = vec.begin (); it != vec.end (); ++it) {
895
- if (it->get () == &module ) {
896
- if (!if_orphaned || it->use_count () == kUseCountOrphaned ) {
897
- vec.erase (it);
898
- break ;
892
+ void RemoveFromMap (const ModuleWP module_wp, bool if_orphaned = false ) {
893
+ if (auto module_sp = module_wp.lock ()) {
894
+ ConstString name = module_sp->GetFileSpec ().GetFilename ();
895
+ if (!m_name_to_modules.contains (name))
896
+ return ;
897
+ llvm::SmallVectorImpl<ModuleSP> &vec = m_name_to_modules[name];
898
+ for (auto *it = vec.begin (); it != vec.end (); ++it) {
899
+ if (it->get () == module_sp.get ()) {
900
+ // Since module_sp increases the refcount by 1, the use count should
901
+ // be the regular use count + 1.
902
+ constexpr long kUseCountOrphaned =
903
+ kUseCountSharedModuleListOrphaned + 1 ;
904
+ if (!if_orphaned || it->use_count () == kUseCountOrphaned ) {
905
+ vec.erase (it);
906
+ break ;
907
+ }
899
908
}
900
909
}
901
910
}
@@ -933,7 +942,7 @@ class SharedModuleList {
933
942
// remove_if moves the elements that match the condition to the end of the
934
943
// container, and returns an iterator to the first element that was moved.
935
944
auto *to_remove_start = llvm::remove_if (vec, [](const ModuleSP &module ) {
936
- return module .use_count () == kUseCountOrphaned ;
945
+ return module .use_count () == kUseCountSharedModuleListOrphaned ;
937
946
});
938
947
939
948
ModuleList to_remove;
@@ -976,7 +985,7 @@ class SharedModuleList {
976
985
llvm::DenseMap<ConstString, llvm::SmallVector<ModuleSP, 1 >> m_name_to_modules;
977
986
978
987
// / The use count of a module held only by m_list and m_name_to_modules.
979
- static constexpr long kUseCountOrphaned = 2 ;
988
+ static constexpr long kUseCountSharedModuleListOrphaned = 2 ;
980
989
};
981
990
982
991
struct SharedModuleListInfo {
@@ -1278,8 +1287,8 @@ bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
1278
1287
return GetSharedModuleList ().Remove (module_sp);
1279
1288
}
1280
1289
1281
- bool ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr ) {
1282
- return GetSharedModuleList ().RemoveIfOrphaned (module_ptr );
1290
+ bool ModuleList::RemoveSharedModuleIfOrphaned (const ModuleWP module_wp ) {
1291
+ return GetSharedModuleList ().RemoveIfOrphaned (module_wp );
1283
1292
}
1284
1293
1285
1294
bool ModuleList::LoadScriptingResourcesInTarget (Target *target,
0 commit comments