Skip to content

Commit e87ab59

Browse files
committed
Use weak_ptr instead
1 parent 10c5245 commit e87ab59

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

lldb/include/lldb/Core/ModuleList.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ class ModuleList {
489489

490490
static size_t RemoveOrphanSharedModules(bool mandatory);
491491

492-
static bool RemoveSharedModuleIfOrphaned(const Module *module_ptr);
492+
static bool RemoveSharedModuleIfOrphaned(const lldb::ModuleWP module_ptr);
493493

494494
/// Applies 'callback' to each module in this ModuleList.
495495
/// If 'callback' returns false, iteration terminates.

lldb/source/Core/ModuleList.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,16 @@ bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
12781278
return GetSharedModuleList().Remove(module_sp);
12791279
}
12801280

1281-
bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
1281+
bool ModuleList::RemoveSharedModuleIfOrphaned(const ModuleWP module_wp) {
1282+
// Get the module pointer if the shared pointer is still valid,
1283+
// but be careful to call RemoveIfOrphaned after the shared pointer
1284+
// is out of scope, otherwise the use count would be incremented by one and
1285+
// RemoveIfOrphaned would never identify the module as an orphan.
1286+
Module *module_ptr = nullptr;
1287+
if (ModuleSP module_sp = module_wp.lock())
1288+
module_ptr = module_sp.get();
1289+
else
1290+
return false;
12821291
return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
12831292
}
12841293

lldb/source/Target/Target.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,12 +2564,9 @@ ModuleSP Target::GetOrCreateModule(const ModuleSpec &orig_module_spec,
25642564
m_images.Append(module_sp, notify);
25652565

25662566
for (ModuleSP &old_module_sp : replaced_modules) {
2567-
auto use_count = old_module_sp.use_count();
2568-
Module *old_module_ptr = old_module_sp.get();
2567+
auto old_module_wp = old_module_sp->weak_from_this();
25692568
old_module_sp.reset();
2570-
// If the use count was one, this was not in the shared module list.
2571-
if (use_count > 1)
2572-
ModuleList::RemoveSharedModuleIfOrphaned(old_module_ptr);
2569+
ModuleList::RemoveSharedModuleIfOrphaned(old_module_wp);
25732570
}
25742571
} else
25752572
module_sp.reset();

0 commit comments

Comments
 (0)