[CrystalSpace] #929: Circular csRef/csWeakRef with explicit reset in destructor results in assert

"CrystalSpace" <[email protected]>
Newsgroups gmane.comp.graphics.crystalspace.tracker
Message-ID <[email protected]>
#929: Circular csRef/csWeakRef with explicit reset in destructor results in
assert
---------------------+------------------------------------------------------
 Reporter:  thebolt  |       Owner:  admin
     Type:  defect   |      Status:  new  
 Priority:  major    |   Milestone:       
Component:  libs     |     Version:  V1.9 
 Keywords:           |  
---------------------+------------------------------------------------------
 The original code is csTextureHandleNull and csTextureManagerNull, where
 destruction of csTextureHandleNull results in following call chain, that
 ends up asserting (CS_ASSERT so only in debug-mode, probably does not
 cause any problem in release)

 {{{
 #3  0x0028a54c in CS::Debug::AssertMessage (expr=0xb55fe2 "scfRefCount !=
 0",
     filename=0xb55fbc "./include/csutil/scf_implementation.h", line=302,
     msg=0xb55ff4 "Refcount incremented from inside dtor") at
 /home/mos/src/CS/libs/csutil/csassert.cpp:80
 #4  0x00b54afb in scfImplementation<csTextureHandleNull>::IncRef
 (this=0xb565bba0)
     at ./include/csutil/scf_implementation.h:301
 #5  0x00b520b5 in csRef<iBase>::csRef<csTextureHandleNull>
 (this=0xbfffe98c, newobj=0xb565bba0)
     at ./include/csutil/ref.h:181
 #6  0x00b514cb in csWeakRef<csTextureHandleNull>::Get<csRef<iBase> >
 (this=0xbfffe9cc) at ./include/csutil/weakref.h:308
 #7  0x00b5036f in csWeakRef<csTextureHandleNull>::~csWeakRef
 (this=0xbfffe9cc, __in_chrg=<optimized out>)
     at ./include/csutil/weakref.h:180
 #8  0x00b4f70f in csTextureManagerNull::UnregisterTexture
 (this=0xb7d56758, handle=0xb565bba0)
     at /home/mos/src/CS/plugins/video/render3d/null/null_txt.cpp:206
 #9  0x00b4ef08 in csTextureHandleNull::~csTextureHandleNull
 (this=0xb565bba0, __in_chrg=<optimized out>,
     __vtt_parm=<optimized out>) at
 /home/mos/src/CS/plugins/video/render3d/null/null_txt.cpp:88
 #10 0x00b4efa1 in csTextureHandleNull::~csTextureHandleNull
 (this=0xb565bba0, __in_chrg=<optimized out>,
     __vtt_parm=<optimized out>) at
 /home/mos/src/CS/plugins/video/render3d/null/null_txt.cpp:89
 #11 0x00b54a84 in scfImplementation<csTextureHandleNull>::DecRef
 (this=0xb565bba0)
     at ./include/csutil/scf_implementation.h:295
 #12 0x080a6adf in csRef<iTextureHandle>::~csRef (this=0xb5b16648,
 __in_chrg=<optimized out>) at ./include/csutil/ref.h:206
 }}}

 A simplified code which exhibits the problem is

 {{{
 class Manager;

 class Client : public scfImplementation0<Client>
 {
 public:
   Client (Manager *b);
   ~Client ();

 private:
   Manager *b;
 };

 class Manager
 {
 public:
   void RegisterClient (Client *a);
   void UnregisterClient (Client *a);

 private:
   csWeakRefArray<Client> clientArray;
 };

 Client::Client (Manager *b)
   : scfImplementationType (this), b(b)
 {
 }

 Client::~Client ()
 {
   if (b) {
     b->UnregisterClient (this);
   }
 }

 void Manager::RegisterClient (Client *a)
 {
   clientArray.Push (a);
 }

 void Manager::UnregisterClient (Client *a)
 {
   size_t index = clientArray.Find (a);
   if (index != csArrayItemNotFound) clientArray.DeleteIndexFast (index);
 }

 void weakRefTest::testCyclicTeardown()
 {
   Manager *ourManager;
   Client *ourClient;

   ourManager = new Manager;
   ourClient = new Client (ourManager);

   ourManager->RegisterClient (ourClient);
   ourClient->DecRef ();

   // Clean up
   delete ourManager;

   CPPUNIT_ASSERT(1 == 1);
 }
 }}}

-- 
Ticket URL: <http://trac.crystalspace3d.org/trac/CS/ticket/929>
CrystalSpace <http://www.crystalspace3d.org/>
Free open-source 3D SDK.
------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of 
discussion for anyone considering optimizing the pricing and packaging model 
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.