Skip to content
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

pqxx::connection_base does not have a virtual DTOR #57

Closed
kalman5 opened this issue Jan 18, 2018 · 9 comments
Closed

pqxx::connection_base does not have a virtual DTOR #57

kalman5 opened this issue Jan 18, 2018 · 9 comments
Milestone

Comments

@kalman5
Copy link
Contributor

kalman5 commented Jan 18, 2018

It looks like the pqxx::connection_base doesn't not have the virtual DTOR

@jtv
Copy link
Owner

jtv commented Jan 21, 2018

That is correct. There are no virtual functions in this class, so there is no need for a virtual destructor.

@jtv jtv closed this as completed Jan 21, 2018
@kalman5
Copy link
Contributor Author

kalman5 commented Jan 21, 2018

I was able to do:
std::unique_ptr<pqxx::connection_base> myConnection = foo();
and you can imagine that when myConnection went out of scope the connection to the database was not discarded indeed the close() is in ~basic_connection();
If you don't wanna make that DTOR virtual then better move the close() from ~basic_connection to ~connection_base.

@egorpugin
Copy link
Contributor

egorpugin commented Jan 22, 2018

True.
Until pqxx::connection_base isn't virtual (having virtual members/destructor), information about the true type under pointer is lost.
Consider these examples:

struct s1 { ~s1(){} };
struct s2 : s1 { ~s2() { /* not executed */ } };
int main()
{
    std::unique_ptr<s1> p = std::make_unique<s2>();
}
struct s1 { virtual ~s1(){} };
struct s2 : s1 { ~s2() { /* executed */ } };
int main()
{
    std::unique_ptr<s1> p = std::make_unique<s2>();
}

So, the rule of thumb: if you intend pointers usage like @kalman5 mentioned, or you have any virtual methods in child classes, you must always make destructor virtual in the very base class of hierarchy.

@kalman5
Copy link
Contributor Author

kalman5 commented Jan 22, 2018

Fact is that most of methods take a pqxx:connection_base for reference so it seems valid to have pointers to pqxx::connection_base around, but deleting a connection trough a pqxx::connection_base pointer leaks the connection. I'd say fine that pqxx::connection_base doesn't have a virtual table but then in his destructor we need to call the close().

@jtv
Copy link
Owner

jtv commented Jan 26, 2018

I suppose I always assumed that whoever created the connection would also ensure that it was closed, with the full type information at hand. If nothing else, the advent of move semantics may have upset that calculation.

Something else that puzzles me now, decades later, is that I don't see why connectionpolicy should have a vtable.

@jtv jtv reopened this Jan 26, 2018
@kalman5
Copy link
Contributor Author

kalman5 commented Jan 26, 2018

You can remove the vtable entirely from connectionpolicy, indeed you are using a specialization of it inside basic_connection trough a pointer to the base class neither trough a reference.

@jtv
Copy link
Owner

jtv commented Jan 29, 2018

Ah, no — connection_base::m_policy is a reference to connectionpolicy. So that needs the vtable.

I think some design considerations are starting to come back to me... The close() is done in the basic_connection destructor so that it would have the complete static type information, and a complete basic_connection object. Without that, it becomes hard to guarantee that you'll still have a working connectionpolicy when the connection_base destructor gets run.

It's all pretty brittle, but ISTM changing this without breaking things could require some invasive surgery, and not necessarily take us to a better design. Perhaps the best thing I can do right now is document it properly!

@jtv
Copy link
Owner

jtv commented Sep 4, 2018

Looks like we can solve this along with #58 and #113. I'll mark this one a duplicate of #58, and when we make this change, we'll call 7.0.

@jtv jtv added the duplicate label Sep 4, 2018
@jtv jtv added this to the 7.0 milestone Dec 17, 2018
@jtv
Copy link
Owner

jtv commented May 7, 2019

Not released yet, but in master now: we're in the run-up to 7.0, where the connection hierarchy is now just a single class. No vtable required!

If you don't mind I'll close this ticket now, so it's off the to-do list. Please try the latest development versions!

@jtv jtv closed this as completed May 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants