Use std::unique_ptr (C++11) where possible #11043

pull practicalswift wants to merge 13 commits into bitcoin:master from practicalswift:unique-pointers changing 20 files +104 −131
  1. practicalswift commented at 2:42 pm on August 14, 2017: contributor

    Use std::unique_ptr (C++11) where possible.

    Rationale:

    1. Avoid resource leaks (specifically: forgetting to delete an object created using new)
    2. Avoid undefined behaviour (specifically: double delete:s)

    Note to reviewers: Please let me know if I’ve missed any obvious std::unique_ptr candidates. Hopefully this PR should cover all the trivial cases.

  2. practicalswift force-pushed on Aug 14, 2017
  3. practicalswift force-pushed on Aug 14, 2017
  4. practicalswift force-pushed on Aug 14, 2017
  5. practicalswift force-pushed on Aug 14, 2017
  6. practicalswift force-pushed on Aug 14, 2017
  7. in src/policy/fees.cpp:997 in 8bdd63bf05 outdated
     998-            delete shortStats;
     999-            delete longStats;
    1000-            feeStats = fileFeeStats.release();
    1001-            shortStats = fileShortStats.release();
    1002-            longStats = fileLongStats.release();
    1003+            feeStats.reset(fileFeeStats.release());
    


    sipa commented at 6:37 pm on August 14, 2017:

    How about

    0feeStats = std::move(fileFeeStats);
    1shortStats = std::move(fileShortStats);
    2longStats = std::move(fileLongStats);
    
  8. practicalswift force-pushed on Aug 14, 2017
  9. practicalswift commented at 8:22 pm on August 14, 2017: contributor
    @sipa Nice catch! Fixed! :-)
  10. ryanofsky commented at 8:53 pm on August 14, 2017: member

    It would be nice to replace all the new

    std::unique_ptr<ClassName>(new ClassName(...))

    occurrences this is adding with simpler

    make_unique<ClassName>(...)

    calls.

    std::make_unique isn’t available until c++14, so we can’t use it yet, but it is easy enough to write a substitute. For example (from #10973):

    0//! Substitute for C++14 std::make_unique.
    1template <typename T, typename... Args>
    2std::unique_ptr<T> MakeUnique(Args&&... args)
    3{
    4    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
    5}
    
  11. in src/wallet/db.cpp:490 in 8798bd4afe outdated
    486@@ -487,7 +487,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
    487                 std::string strFileRes = strFile + ".rewrite";
    488                 { // surround usage of db with extra {}
    489                     CDB db(dbw, "r");
    490-                    Db* pdbCopy = new Db(env->dbenv.get(), 0);
    491+                    std::unique_ptr<Db> pdbCopy = std::unique_ptr<Db>(new Db(env->dbenv.get(), 0));
    


    ryanofsky commented at 9:33 pm on August 14, 2017:

    In commit “Use unique_ptr for pdbCopy (Db)”

    Is this fixing a potential resource / memory leak? It seems like the pointer was only being freed conditionally below. Maybe update commit description to say how this is changing behavior, if it is changing behavior.

  12. in src/test/test_bitcoin.h:53 in 5a92b9d895 outdated
    49@@ -50,7 +50,7 @@ struct BasicTestingSetup {
    50  */
    51 class CConnman;
    52 struct TestingSetup: public BasicTestingSetup {
    53-    CCoinsViewDB *pcoinsdbview;
    54+    std::unique_ptr<CCoinsViewDB> pcoinsdbview;
    


    ryanofsky commented at 9:43 pm on August 14, 2017:

    In commit “Use unique_ptr for pcoinscatcher/pcoinsdbview/pcoinsTip/pblocktree”

    Can this variable just be deleted like the one in rpcnestedtests.h, since it is shadowing a global with the same name? If it can’t be deleted, it would be good to have a comment noting that it does shadow the global (and maybe explaining why).

  13. ryanofsky commented at 9:44 pm on August 14, 2017: member
    utACK 5a92b9d89509fa17ea341da3794b34ac330669a9
  14. practicalswift force-pushed on Aug 15, 2017
  15. fanquake added the label Refactoring on Aug 15, 2017
  16. laanwj commented at 7:47 am on August 15, 2017: member

    Avoid resource leaks (specifically: forgetting to delete an object created using new)

    Just want to add that this is not a panacea. In a few places I’ve explicitly used manual pointer allocation/deallocation instead of unique_ptr (for globals) so to explicitly control the lifespan, because the order of deallocation matters. So in some of these cases, forgetting explicit .reset() is as bad as leaking and the ‘parachute’ that automatic deallocation provides isn’t worth anything (could even result in spurious crashes at shutdown).

  17. bytting commented at 7:56 am on August 15, 2017: contributor

    I like the concept, and encourage anyone who wants more meat on the bone to read Sutter’s Mill articles about smart pointers

    https://herbsutter.com/gotw/

  18. practicalswift commented at 8:02 am on August 15, 2017: contributor

    @laanwj Absolutely true! My goal with this PR is to address only the trivial/safe cases :-)

    I know about the dependency on deconstruction order related to LogPrintStr(…) (FILE* fileout, boost::mutex* mutexDebugLog and std::list<std::string>* vMsgsBeforeOpenLog) - so these were intentionally excluded. Were you thinking about to those when mentioning spurious crashes at shutdown? :-)

  19. practicalswift force-pushed on Aug 15, 2017
  20. laanwj commented at 8:04 am on August 15, 2017: member

    Were you thinking about to those when mentioning spurious crashes at shutdown? :-)

    No, I’ll comment inline.

  21. in src/httprpc.cpp:256 in bb8dc2e616 outdated
    252@@ -253,8 +253,6 @@ void StopHTTPRPC()
    253     LogPrint(BCLog::RPC, "Stopping HTTP RPC server\n");
    254     UnregisterHTTPHandler("/", true);
    255     if (httpRPCTimerInterface) {
    256-        RPCUnsetTimerInterface(httpRPCTimerInterface);
    257-        delete httpRPCTimerInterface;
    258-        httpRPCTimerInterface = 0;
    259+        RPCUnsetTimerInterface(httpRPCTimerInterface.get());
    


    laanwj commented at 8:05 am on August 15, 2017:
    add httpRPCTimerInterface.reset() after this

    practicalswift commented at 8:08 am on August 15, 2017:
    @laanwj Thanks for reviewing! Fixed!
  22. practicalswift force-pushed on Aug 15, 2017
  23. in src/wallet/test/wallet_test_fixture.cpp:29 in bb8dc2e616 outdated
    29 WalletTestingSetup::~WalletTestingSetup()
    30 {
    31-    UnregisterValidationInterface(pwalletMain);
    32-    delete pwalletMain;
    33-    pwalletMain = nullptr;
    34+    UnregisterValidationInterface(pwalletMain.get());
    


    laanwj commented at 8:10 am on August 15, 2017:
    add pwalletMain.reset() (this is not part of the WalletTestingSetup but a global, so needs to be cleaned up here and now)

    practicalswift commented at 8:15 am on August 15, 2017:
    Thanks! Fixed!
  24. laanwj commented at 8:14 am on August 15, 2017: member

    I found two in a cursory look-over. There might be more instances where explicit .reset is important (where there was an explicit delete before), please be careful in reviewing this.

    LogPrintStr(…) (FILE* fileout, boost::mutex* mutexDebugLog and std::liststd::string* vMsgsBeforeOpenLog) so these were intentionally excluded

    Agree with leaving these changes out for that “magic”, just too easy to inadvertently break something there.

  25. practicalswift force-pushed on Aug 15, 2017
  26. practicalswift commented at 8:30 am on August 15, 2017: contributor
    @ryanofsky Thanks for reviewing! Great feedback - all items addressed.
  27. practicalswift force-pushed on Aug 16, 2017
  28. ryanofsky commented at 6:06 am on August 16, 2017: member
    utACK 6b7863325b74a6b45aa77a1fc9f3586520783f5b. Changes since previous were just all the suggested changes (restoring some explicit deletes instead of relying on destructors, removing pcoinsdbview shadowing, adding MakeUnique) plus rebasing.
  29. practicalswift force-pushed on Aug 16, 2017
  30. practicalswift commented at 2:21 pm on August 16, 2017: contributor
    Rebased!
  31. laanwj commented at 7:43 am on August 22, 2017: member
    Needs rebase again as #11007 was a subset of this.
  32. practicalswift force-pushed on Aug 22, 2017
  33. practicalswift commented at 8:04 am on August 22, 2017: contributor
    @laanwj Done! :-)
  34. practicalswift force-pushed on Oct 2, 2017
  35. practicalswift commented at 2:28 pm on October 2, 2017: contributor
    Rebased! :-)
  36. practicalswift commented at 7:30 pm on October 9, 2017: contributor
    Anyone willing to review? I think this one should be ready for merge :-)
  37. ryanofsky commented at 7:23 pm on October 12, 2017: member
    utACK 3d7109971002358fbf62477068dc06f67234d2b4. Only change since previous review is rebase and changing a few 0s to nullptrs.
  38. sipa commented at 11:04 pm on October 12, 2017: member
    utACK 3d7109971002358fbf62477068dc06f67234d2b4
  39. practicalswift force-pushed on Oct 17, 2017
  40. practicalswift commented at 5:00 pm on October 17, 2017: contributor
    Rebased!
  41. practicalswift force-pushed on Oct 18, 2017
  42. practicalswift commented at 3:13 pm on October 18, 2017: contributor
    Rebased!
  43. ryanofsky commented at 7:04 pm on October 31, 2017: member
    0a38fb91025cb3a190aae5d8c44093a5d599f8c1. Only change is rebase and new MakeUnique in wallet/db.cpp
  44. practicalswift commented at 7:16 pm on October 31, 2017: contributor

    Thanks for the re-review @ryanofsky!

    Are we getting close to merge? :-)

  45. MarcoFalke commented at 7:26 pm on October 31, 2017: member

    Are we getting close to merge? :-)

    We might want to wait until the 0.15.1 stuff is merged, to ease backports. Also, noting that some commits should be squashed before merge.

  46. promag commented at 7:27 pm on October 31, 2017: member
    utACK 0a38fb9.
  47. practicalswift commented at 7:30 pm on October 31, 2017: contributor
    @MarcoFalke Sounds good! Should I squash into one commit now or should I wait for further instructions?
  48. Use unique_ptr for pwalletMain (CWallet) 860e912583
  49. Use unique_ptr for httpRPCTimerInterface (HTTPRPCTimerInterface) 5a6f768896
  50. Use unique_ptr:s for {fee,short,long}Stats (TxConfirmStats) fa6d1228e9
  51. Use unique_ptr for dbw (CDBWrapper) 0024531625
  52. Use unique_ptr for upnp_thread (boost::thread) 73db0635a3
  53. Use unique_ptr for sem{Addnode,Outbound} (CSemaphore) 8ccf1bb0c3
  54. Use unique_ptr for pfilter (CBloomFilter) f72cbf9ba9
  55. Use unique_ptr for dbenv (DbEnv) 29ab96dbd2
  56. Use unique_ptr for pdbCopy (Db) and fix potential memory leak b45c597caa
  57. Use unique_ptr for pcoinscatcher/pcoinsdbview/pcoinsTip/pblocktree
    * pcoinscatcher (CCoinsViewErrorCatcher)
    * pcoinsdbview (CCoinsViewDB)
    * pcoinsTip (CCoinsViewCache)
    * pblocktree (CBlockTreeDB)
    * Remove variables shadowing pcoinsdbview
    d223bc940a
  58. Add MakeUnique (substitute for C++14 std::make_unique)
    From @ryanofsky:s #10973. Thanks!
    86179897e2
  59. Use MakeUnique<T>(...) instead of std::unique_ptr<T>(new T(...)) 3e09b390b4
  60. Use MakeUnique<Db>(...) a357293c87
  61. practicalswift force-pushed on Nov 9, 2017
  62. practicalswift commented at 3:54 pm on November 9, 2017: contributor
    Rebased! :-)
  63. laanwj commented at 8:28 pm on November 9, 2017: member
    Looks good to me now, utACK a357293
  64. laanwj merged this on Nov 9, 2017
  65. laanwj closed this on Nov 9, 2017

  66. laanwj referenced this in commit 5e9be169e4 on Nov 9, 2017
  67. laanwj referenced this in commit 79313d2e20 on Feb 12, 2018
  68. PastaPastaPasta referenced this in commit 7f4cfcae74 on Feb 13, 2020
  69. PastaPastaPasta referenced this in commit b515ff2ddf on Feb 13, 2020
  70. PastaPastaPasta referenced this in commit 7f4825166b on Feb 13, 2020
  71. PastaPastaPasta referenced this in commit c1897a6f14 on Feb 29, 2020
  72. PastaPastaPasta referenced this in commit 7a795d3f01 on Mar 14, 2020
  73. PastaPastaPasta referenced this in commit 8b13a956bc on Mar 14, 2020
  74. PastaPastaPasta referenced this in commit 5e19f32b6b on Mar 15, 2020
  75. ckti referenced this in commit 7d5d24de9b on Mar 28, 2021
  76. ckti referenced this in commit c32dc3e689 on Mar 28, 2021
  77. practicalswift deleted the branch on Apr 10, 2021
  78. gades referenced this in commit cfb30839fc on Jun 25, 2021
  79. furszy referenced this in commit 60d36292bc on Jul 26, 2021
  80. gades referenced this in commit e55710c063 on Feb 5, 2022
  81. gades referenced this in commit 7669d647b5 on Apr 1, 2022
  82. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-17 18:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me