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

Improve exception safety with smart pointers #598

Closed
elfring opened this issue Jan 11, 2023 · 9 comments
Closed

Improve exception safety with smart pointers #598

elfring opened this issue Jan 11, 2023 · 9 comments
Assignees
Labels
bug Something isn't working

Comments

@elfring
Copy link

elfring commented Jan 11, 2023

💭 Would you like to wrap any pointer data members with the class template “std::unique_ptr”?

@elfring elfring added the bug Something isn't working label Jan 11, 2023
@braindigitalis
Copy link
Contributor

no thanks, this will add extra complication to the library, and the library itself tracks the ownership of the pointers (and owns all pointers).
In fact we barely use any manual allocation via new at all, except in caches and a couple of internal values.

@elfring
Copy link
Author

elfring commented Jan 11, 2023

💭 Would you become interested to apply C++ Core Guidelines for any more software components?

@elfring
Copy link
Author

elfring commented Jan 11, 2023

In fact we barely use any manual allocation via new at all, …

💭 How does such a feedback fit to improvable source code places?

Example:

zlib = new zlibcontext();

@braindigitalis
Copy link
Contributor

In fact we barely use any manual allocation via new at all, …

💭 How does such a feedback fit to improvable source code places?

Example:

zlib = new zlibcontext();

there really is no need for this to be a smart pointer, it is just added complexity. Its lifetime is managed by its owning class.

@elfring
Copy link
Author

elfring commented Jan 11, 2023

💭 Are smart pointers provided for the avoidance of undesirable software behaviour?

👀 Where do you see owning objects for the pointers “etf” and “zlib”?

@braindigitalis
Copy link
Contributor

the one that creates them, discordclient :) This is the only place they are instantiated.

@elfring
Copy link
Author

elfring commented Jan 11, 2023

This is the only place they are instantiated.

I know.

I interpret the linked source code in the way that the two pointer member variables “etf” and “zlib” are not initialised with “nullptr” so far.
Thus such a pointer combination is generally problematic in case an exception like “std::bad_alloc” would be thrown.

@elfring
Copy link
Author

elfring commented Jan 25, 2023

👀 Would you like to take another look at the advice “Item 10: Prevent resource leaks in constructors.”?

@braindigitalis
Copy link
Contributor

changing everything to smart pointers is a potentially lazy option, the correct fix is shown here without needing to add complexity to the library.

3981360

We simply need to ensure that nullptr is set for those two values, and rethrow the std::bad_alloc after any cleanup. This is a much simpler change than refactoring an entire stable library (where a bad_alloc on construction of a discordclient has never happened in production in a real world system to our knowledge) to use unique_ptr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants