[lldb-dev] Too many ModuleSP references

Zachary Turner via lldb-dev lldb-dev at lists.llvm.org
Tue Feb 21 16:42:02 PST 2017


On Tue, Feb 21, 2017 at 4:24 PM Greg Clayton via lldb-dev <
lldb-dev at lists.llvm.org> wrote:

> On Feb 21, 2017, at 9:10 AM, Zachary Turner via lldb-dev <
> lldb-dev at lists.llvm.org> wrote:
>
> No comments on this specifically, but +1 to reducing shared_ptr usage in
> general. We use way too many, and often it feels like shared_ptr is used as
> a way to avoid having to think about ownership, which leads to more
> problems than it solves imo
>
>
> We do a pretty good job of using shared pointers where they are needed, so
> I don't agree with the above statement. We have strong memory models in
> LLDB and certain things need to keep certain things alive. LLVM is quite
> the wild west with regard to memory management, so I don't want to base any
> changes on it. We just need to use std::weak_ptr when needed. There should
> be no two items that contain strong references to each other.
>
> In general think about what should keep things alive. If I have a SBModule
> variable, I should expect that it won't let my module go away. If I have a
> SBTarget, I would expect it to keep the target around as long as I have a
> reference. Things that belong to any of these items, like breakpoints,
> watchpoints, compile units, functions, shouldn't keep the module or target
> around.
>

Agree with all of this, but I think most of the time you only need 1 thing
keeping an object alive (i.e. unique_ptr), and from there you can just hand
out references.  In the case where an object might die while a reference is
outstanding, then a lot of times I think the design can be adjusted so that
doesn't happen, but when it's still necessary, you can store a shared_ptr
and vend out weak_ptrs.  You don't need everything in the system keeping
everything else alive.

I'm speaking from a purely general standpoint, I haven't done a deep dive
into LLDB to see where this does / doesn't apply, but in my experience
shared_ptrs are one of the most overused things in all of C++.  So when I
see a lot of them, I just default to "I bet these can be reduced
significantly"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/lldb-dev/attachments/20170222/c4e91813/attachment.html>


More information about the lldb-dev mailing list