-
Notifications
You must be signed in to change notification settings - Fork 98
chore: Improve closing on FairLmdSource #1465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
ebf8600
to
9f7501b
Compare
`Close` is a virtual method. It's getting called internally in a scenario where only FairLmdSource specific resources should be closed. Also the destructor did not close.
9f7501b
to
46b14b6
Compare
Hi, thanks for the PR. There are few problems:
|
That does not work. Inside the base class destructor the object is of type base-class. So any calls to any virtual functions in the dtor go to the virtual methods of that base class (or its parents). That's basically, why calling virtual functions in the dtor / ctor (there it's the same story) is a bad idea, because it does not do what you would think it should do. There's even a clang-tidy warning for this: clang-analyzer-optin.cplusplus.VirtualCall. And yes, we have enabled this one in our CI. And yes, we should fix them…
And IMHO it is okay for a FairSource to have Open and Close functions. The Close one can be virtual (yes, it would be nicer, if the virtual functions would be private/protected and the public interface would be another member functions, but we don't have that yet), so that FairRun can close the source when it's done, but doesn't have to destruct it yet. So the idea is:
|
You are right! I always thought calling a virtual function in dtor is same as in any other member functions. Glad I learned this new thing.
Even though we can't call the Close in FairSource dtor, we could still call the Fair::Close in the dtor of its owner, as is also a better place to call it. As is a golden rule for the ownership, whoever owns it is responsible for how it's disposed (delete, close, log ext.). So in my opinion, what's the correct way is to have a public non-virtual close member function in the base class (in our case, A very simple illustration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following my comment above.
The benefit of this way is that the close action is automatically handled by the base class. The developer who implements a new derived class just needs to override the Close_imp()
without concerning where it should be called. And we have only one line of Close for all derived classes.
@@ -54,6 +54,7 @@ FairLmdSource::FairLmdSource(const FairLmdSource& source) | |||
|
|||
FairLmdSource::~FairLmdSource() | |||
{ | |||
CloseLmd(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This step could be executed by its owner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, no.
I don't think, the "owner" should be resposible for calling Close. It could be considered "being a good citizen", but it should not be strictly needed.
FairLmdSource mysource;
mysource.Open(…);
if (true)
throw std::runtime_exception("Something else went wrong");
mysource.Close();
This should not leak resources! (See "RAII printciple").
So yes, This is some overhead for the developer of a FairSource/Sink. But that's how C++ is like.
void FairLmdSource::Close() | ||
{ | ||
CloseLmd(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be implemented in the base class FairSource
:
void FairSource::Close() {Close_imp();}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we postpone this for another PR?
@@ -65,6 +65,9 @@ class FairLmdSource : public FairMbsSource | |||
|
|||
FairLmdSource& operator=(const FairLmdSource&); | |||
|
|||
private: | |||
void CloseLmd(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be an overridden private virtual function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's please postpone this for another PR as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much cleaner closing now. Looks good.
Should we backport this to 18.8 (or even 18.6)? |
Close
is a virtual method. It's getting called internally in a scenario where only FairLmdSource specific resources should be closed.Also the desctructor did not close.
This was triggered by a comment here: #1464 (review)
Checklist: