Comments (9)
That is correct. There are no virtual functions in this class, so there is no need for a virtual destructor.
from libpqxx.
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.
from libpqxx.
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.
from libpqxx.
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().
from libpqxx.
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.
from libpqxx.
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.
from libpqxx.
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!
from libpqxx.
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.
from libpqxx.
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!
from libpqxx.
Related Issues (20)
- Can't initialize zview from string_view HOT 2
- Missing cast opportunity HOT 17
- cmake will take the given lib name and it changes to "-l{libname}" and linker will search using "-l{libname}" HOT 7
- `RETURNING` from a `stream_to` HOT 3
- Failure on streaming a list of empty strings HOT 6
- Conversion of TEXT[] to std::vector<std::string>. HOT 5
- error C2039: 'converts_from_string': is not a member of 'pqxx::string_traits<pgvector::Vector>' HOT 8
- Undefined references while compiling project HOT 17
- Static linking is not working, C++20, libpq REL_16_2, HOT 11
- Can I use stream_to to insert multiple records while one field is Postgis Geometry type? HOT 5
- std::optional<std::chrono::year_month_day> to DATE error if optional is empty HOT 4
- Encoding problems with bytea HOT 19
- Undefined refences when compiling example from the documentation HOT 10
- After compilation, there are only pqxx files but no pqxx.hxx files. Is this normal? HOT 31
- Rtti usage on demand HOT 6
- linking issue when using torch+pqxx in cmake. HOT 11
- Can't use libpqxx 7.9.0 from vcpkg in a Visual Studio 2022 C++ 17 project HOT 6
- Allow parsing arrays without connection HOT 20
- Any example to deal with time or date? HOT 2
- ERROR: Unsupported server version; 9.0 is the minimum. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libpqxx.