diff options
author | Mike Buland <mbuland@penny-arcade.com> | 2024-11-07 15:34:39 -0800 |
---|---|---|
committer | Mike Buland <mbuland@penny-arcade.com> | 2024-11-07 15:34:39 -0800 |
commit | 589e691ba8503dd247aaeab76c1113f87c2ff980 (patch) | |
tree | 5d4132085ec8a277c2dd5fbef19cf38ff9e3cb76 /src | |
parent | 6c066b6bbd4a44ae7c5874abf6bd3a3e04f76b88 (diff) | |
download | libbu++-589e691ba8503dd247aaeab76c1113f87c2ff980.tar.gz libbu++-589e691ba8503dd247aaeab76c1113f87c2ff980.tar.bz2 libbu++-589e691ba8503dd247aaeab76c1113f87c2ff980.tar.xz libbu++-589e691ba8503dd247aaeab76c1113f87c2ff980.zip |
Fixed long standing double-free bug in the cache.
I guess we don't delete stuff very often.
Diffstat (limited to 'src')
-rw-r--r-- | src/tests/cachedel.cpp | 24 | ||||
-rw-r--r-- | src/unstable/cachebase.h | 10 |
2 files changed, 30 insertions, 4 deletions
diff --git a/src/tests/cachedel.cpp b/src/tests/cachedel.cpp index 3fa3e86..f4cb1b5 100644 --- a/src/tests/cachedel.cpp +++ b/src/tests/cachedel.cpp | |||
@@ -21,6 +21,12 @@ public: | |||
21 | 21 | ||
22 | virtual ~Something() | 22 | virtual ~Something() |
23 | { | 23 | { |
24 | //Bu::println("Deleting %1").arg( this->toString() ); | ||
25 | } | ||
26 | |||
27 | void sayHi() | ||
28 | { | ||
29 | Bu::println("Hello %1").arg( toString() ); | ||
24 | } | 30 | } |
25 | 31 | ||
26 | virtual Bu::Uuid getKey() const | 32 | virtual Bu::Uuid getKey() const |
@@ -39,7 +45,7 @@ public: | |||
39 | changed(); | 45 | changed(); |
40 | } | 46 | } |
41 | 47 | ||
42 | virtual Bu::String toString() const=0; | 48 | virtual Bu::String toString() const=0;// { return Bu::String("ERROR"); }; |
43 | 49 | ||
44 | private: | 50 | private: |
45 | Bu::Uuid uId; | 51 | Bu::Uuid uId; |
@@ -60,6 +66,11 @@ public: | |||
60 | iNumber( iNumber ) | 66 | iNumber( iNumber ) |
61 | { | 67 | { |
62 | } | 68 | } |
69 | |||
70 | virtual ~SubSomethingA() | ||
71 | { | ||
72 | Bu::println("Deleting-A %1").arg( this->toString() ); | ||
73 | } | ||
63 | 74 | ||
64 | virtual Bu::String toString() const | 75 | virtual Bu::String toString() const |
65 | { | 76 | { |
@@ -84,6 +95,10 @@ public: | |||
84 | sString( sString ) | 95 | sString( sString ) |
85 | { | 96 | { |
86 | } | 97 | } |
98 | |||
99 | virtual ~SubSomethingB() | ||
100 | { | ||
101 | } | ||
87 | 102 | ||
88 | virtual Bu::String toString() const | 103 | virtual Bu::String toString() const |
89 | { | 104 | { |
@@ -187,6 +202,11 @@ int main( int, char *[] ) | |||
187 | Bu::MemBuf mbStore; | 202 | Bu::MemBuf mbStore; |
188 | SomethingCache c( mbStore ); | 203 | SomethingCache c( mbStore ); |
189 | 204 | ||
205 | { | ||
206 | SubSomethingA a("Test", 1); | ||
207 | a.sayHi(); | ||
208 | } | ||
209 | |||
190 | SomethingPtr ptr; | 210 | SomethingPtr ptr; |
191 | if( time(NULL)%2 ) | 211 | if( time(NULL)%2 ) |
192 | ptr = c.insert( new SubSomethingA("Hello", 55) ).cast<Something>(); | 212 | ptr = c.insert( new SubSomethingA("Hello", 55) ).cast<Something>(); |
@@ -211,6 +231,8 @@ int main( int, char *[] ) | |||
211 | c.erase( id ); | 231 | c.erase( id ); |
212 | Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) ); | 232 | Bu::println("p2 %1: %2").arg( id ).arg( c.has( id ) ); |
213 | 233 | ||
234 | Bu::println("Program listing over, leaving main scope."); | ||
235 | |||
214 | return 0; | 236 | return 0; |
215 | } | 237 | } |
216 | 238 | ||
diff --git a/src/unstable/cachebase.h b/src/unstable/cachebase.h index ec73ede..9ac1b8c 100644 --- a/src/unstable/cachebase.h +++ b/src/unstable/cachebase.h | |||
@@ -32,11 +32,15 @@ namespace Bu | |||
32 | bDeleted( false ), | 32 | bDeleted( false ), |
33 | pObject( pObject ) | 33 | pObject( pObject ) |
34 | { | 34 | { |
35 | Bu::println("CacheEntry::CacheEntry: registering pObject (0x%1)"). | ||
36 | arg( reinterpret_cast<ptrdiff_t>(pObject), Bu::Fmt::hex() ); | ||
35 | } | 37 | } |
36 | 38 | ||
37 | virtual ~CacheEntry() | 39 | virtual ~CacheEntry() |
38 | { | 40 | { |
39 | mEntry.lock(); | 41 | mEntry.lock(); |
42 | Bu::println("CacheEntry::~CacheEntry: deleting pObject (0x%1)"). | ||
43 | arg( reinterpret_cast<ptrdiff_t>(pObject), Bu::Fmt::hex() ); | ||
40 | delete pObject; | 44 | delete pObject; |
41 | mEntry.unlock(); | 45 | mEntry.unlock(); |
42 | } | 46 | } |
@@ -497,7 +501,7 @@ namespace Bu | |||
497 | if( pEnt->iRefCount == 0 ) | 501 | if( pEnt->iRefCount == 0 ) |
498 | { | 502 | { |
499 | pEnt->mEntry.unlock(); | 503 | pEnt->mEntry.unlock(); |
500 | delete pEnt->pObject; | 504 | //delete pEnt->pObject; |
501 | delete pEnt; | 505 | delete pEnt; |
502 | } | 506 | } |
503 | else | 507 | else |
@@ -529,7 +533,7 @@ namespace Bu | |||
529 | pEnt->mEntry.unlock(); | 533 | pEnt->mEntry.unlock(); |
530 | throw Bu::ExceptionBase( Bu::String("Cache entry %1 cannot be erased, there are %2 active references.").arg( key ).arg( iCount ).end().getStr() ); | 534 | throw Bu::ExceptionBase( Bu::String("Cache entry %1 cannot be erased, there are %2 active references.").arg( key ).arg( iCount ).end().getStr() ); |
531 | } | 535 | } |
532 | delete pEnt->pObject; | 536 | //delete pEnt->pObject; |
533 | delete pEnt; | 537 | delete pEnt; |
534 | hCacheEntry.erase( key ); | 538 | hCacheEntry.erase( key ); |
535 | } | 539 | } |
@@ -559,7 +563,7 @@ namespace Bu | |||
559 | { | 563 | { |
560 | if( pEnt->isReadyForCleanup() ) | 564 | if( pEnt->isReadyForCleanup() ) |
561 | { | 565 | { |
562 | delete pEnt->pObject; | 566 | //delete pEnt->pObject; |
563 | delete pEnt; | 567 | delete pEnt; |
564 | } | 568 | } |
565 | } | 569 | } |