From: Vlad K. <hv...@us...> - 2011-03-30 11:04:21
|
> 30.03.2011 12:32, Vlad Khorsun wrote: >>> There is no difference. Whether pointer was invalid from the beginning or become, >>> result is the same. >> >> Your example is a crash in *user* code, not in ours. > > Only if called function is virtual and is called via reference to base class. Otherwise > crash will occur in _your_ code as soon as it try to access any member variable. Do you know what is interfaces ? It is pure virtual classes and all methods is virtual. >> Reference counting prevents MT races in most cases, it is already used by our code >> internally and allows user to not add additional locking layer for MT safety. > > Ok, here is a code: > > Interface* a = new RefCountedClass; > Interface* b = NULL; > b = a; > delete a; > b->Anything(); > > I don't understand how reference counting works here. Could you explain? This code can't be used as example because user physically can't call new\delete for interface pointer obtained from our code. See below (simplificated): SmartPtr<IAttachment*> att = fb->attachDatabase(...); // refcount == 1 SmartPtr<IAttachment*> b = att; // recount = 2 b = NULL; // refcount = 1 a->detach(); // refcount = 1 but detach really happens !!! a->XXX // refcount = 1, returns isc_bad_db_handle a = NULL; // refcount = 0, YValve-s object freed With Delphi you will just use IAttachment, without SmartPtr, as Delphi's "interface" have built-in compiler magic to work with reference counts. Regards, Vlad |