<div dir="ltr">Thanks Pavel.  <div><br></div><div>> <span style="font-size:12.8000001907349px">the root cause is unclear ownership semantics between Threads, Processes and TSC</span></div><div><br></div><div>Who is going to work on this problem?  How do we solve this problem?  Document our best guess and refactor to fit?  (And it that doesn't work, try again)</div><div><br></div><div>Vince</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 18, 2015 at 3:46 AM, Pavel Labath <span dir="ltr"><<a href="mailto:labath@google.com" target="_blank">labath@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Greetings,<br>
<br>
I have been following this discussion, and would like to add my 2 cents. I'll start with my thoughts on virtual functions.<br>
<span class=""><br>
> On 17 February 2015 at 18:44, Oleksiy Vyalov <<a href="mailto:ovyalov@google.com">ovyalov@google.com</a>> wrote:<br>
<br>
><br>
<br>
> > On Tue, Feb 17, 2015 at 10:01 AM, Tamas Berghammer <<a href="mailto:tberghammer@google.com">tberghammer@google.com</a>> wrote:<br>
<br>
>  > As far as I know NativeProcessLinux is still fully functional during the execution of it's destructor. The only difference is that the members in<br>
<br>
>  > GDBRemoteCommunicationServerLLGS >defined after the NativeProcessLinux smart pointer are already destructed. If this ordering is the problem then it can be solved with<br>
<br>
>  > changing the order of the member declarations >inside GDBRemoteCommunicationServerLLGS.<br>
<br>
><br>
<br>
> Yes, an instance is functional when its destructor is being called but there some limitations:<br>
<br>
> You may hit undefined behavior if virtual function are called when destruction is in progress - <a href="http://www.artima.com/cppsource/nevercall.html" target="_blank">http://www.artima.com/cppsource/nevercall.html</a>, i.e. if some virtual function of >NativeProcessLinux are called by TSC when ~NativeProcessLinux is in progress - it might be a problem.<br>
<br>
<br>
</span>And a more quote from the standard.<br>
<br>
> Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a<br>
<br>
>  constructor or from a destructor, including during the construction or destruction of the class’s non-static data members, and the object to which the call applies is the object (call it<br>
<br>
>  x) under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class.<br>
<br>
<br>
Judging from this, there is nothing undefined about the situation you mentioned. While ~NativeProcessLinux is in progress any call to its virtual functions will resolve "as expected". This is especially true if you are still executing the body of the destructor (as would be the case if you placed the Join() call in ~NativeProcessLinux), as all the member variables are still fully constructed, so you cannot get undefined behavior if the virtual functions do member access.<br>
<br>
A different case would be if you were calling virtual functions of the (now non-existing) NativeProcessLinux object after its destructor has completed, and the destruction has moved on to its Base classes. In some cases the behavior would be undefined, in others "defined, but surprising". And if this were to happen (which, as far as i can see, is not the case), then I would argue that the bug is in the fact that ThreadStateCoordinator was still alive after the destruction of NativeProcessLinux  -- the coordinator is owned by nativeprocess, so it should never outlive it<br>
<br>
Therefore I think it is a bad idea to create a "destructor" function just to avoid doing something in a destructor. The presence of Terminate() defeats the purpose of having a shared pointer to the process: a shared pointer should delete an object, once the last pointer to it goes out of scope, but right now you cannot properly delete the process object in any other way except by calling ~GDBRemoteCommunicationServerLLGS(). If someone happened to be holding a NativeProcessProtocolSP expecting it to be a functional process, it will be surprised that it is non functional after GDBRemoteCommunicationServerLLGS has been gone.<br>
<br>
Since it seems that the root of the problem was something else (a null ptr dereference in NativeThreadLinux), and this has already been addressed, I would recommend moving the Join() back to the destructor. Unless there are other issues which would require its presence... (?)<br>
<br>
PS:<br>
After my discussion with Tamas, I have come to think that the root cause is unclear ownership semantics between Threads, Processes and TSC: a process holds shared_ptrs to threads, which seems to imply that it is sharing the ownership of them with someone else. However, from the code it would seem that the threads should never outlive the process (or the TSC for that matter). Therefore, it would seem that a Process (or maybe the TSC) is the perfect candidate for the "owner" of it's threads, which would have the sole responsibility for destroying them (which could be represented by a unique_ptr). And then, when the lifetime of threads gets tied to the lifetime of their process, they no longer need to hold a shared (or weak) pointer, they can be happy with a regular pointer. However, this is definitely an issue far out of scope of this CL.<br>
<br>
This completes my braindump. Sorry about the length, I did not expect it to be this long when I started. I realize some of the claims might be too strong for someone who just started and is getting to know the codebase, but I figure I might as well send it, since I spent so much time thinking about it.<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<a href="http://reviews.llvm.org/D7692" target="_blank">http://reviews.llvm.org/D7692</a><br>
<br>
EMAIL PREFERENCES<br>
  <a href="http://reviews.llvm.org/settings/panel/emailpreferences/" target="_blank">http://reviews.llvm.org/settings/panel/emailpreferences/</a><br>
<br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><br><table cellspacing="0" cellpadding="0" style="font-family:'Times New Roman'"><tbody><tr style="color:rgb(85,85,85);font-family:sans-serif;font-size:small"><td nowrap style="border-top-style:solid;border-top-color:rgb(213,15,37);border-top-width:2px">Vince Harron |</td><td nowrap style="border-top-style:solid;border-top-color:rgb(51,105,232);border-top-width:2px"> Technical Lead Manager |</td><td nowrap style="border-top-style:solid;border-top-color:rgb(0,153,57);border-top-width:2px"> <a href="mailto:vharron@google.com" target="_blank">vharron@google.com</a> |</td><td nowrap style="border-top-style:solid;border-top-color:rgb(238,178,17);border-top-width:2px"> 858-442-0868</td></tr></tbody></table><br></div></div>
</div>