reference-counting

Why doesn't std::shared_ptr use reference linking?

佐手、 提交于 2019-12-06 01:47:40
std::shared_ptr needs to allocate a control block on the heap which holds the reference count. There was another approach I learnt from http://ootips.org/yonat/4dev/smart-pointers.html which keeps all the references in a doubly linked list. It doesn't need additional allocations nor a counter but the reference object itself is larger. Is there a benchmark or any clear reason showing one implementation is better than the others? The standard does in theory allow a linked list to be used, but because copying a shared_ptr must be threadsafe it would be harder to implement that with a linked list.

ThreadSanitizer reports “data race on operator delete(void*)” when using embedded reference counter

余生颓废 提交于 2019-12-05 10:19:15
Please have a look at the following code: #include <pthread.h> #include <boost/atomic.hpp> class ReferenceCounted { public: ReferenceCounted() : ref_count_(1) {} void reserve() { ref_count_.fetch_add(1, boost::memory_order_relaxed); } void release() { if (ref_count_.fetch_sub(1, boost::memory_order_release) == 1) { boost::atomic_thread_fence(boost::memory_order_acquire); delete this; } } private: boost::atomic<int> ref_count_; }; void* Thread1(void* x) { static_cast<ReferenceCounted*>(x)->release(); return NULL; } void* Thread2(void* x) { static_cast<ReferenceCounted*>(x)->release(); return

Microsoft objects, the Release() functions return value?

怎甘沉沦 提交于 2019-12-05 08:29:51
I'm curious because I couldn't find out about this on MSDN. I've found the Release() function is present in various COM objects which I'm obviously supposed to use for deleting pointers. But I'm not sure what does it return exactly? I used to think it would return the number of references which still exist to the object remaining, therefore something like: while( pointer->Release() > 0 ); Would obviously release all references to that pointer? Or am I not seeing something? *note I'm talking about this from the concept of the IDirect3DTexture9::Release() function Your theory is true. COM memory

Why doesn't the JVM destroy a resource as soon as its reference count hits 0?

倖福魔咒の 提交于 2019-12-05 04:06:12
I have always wondered why the garbage collector in Java activates whenever it feels like it rather than do: if(obj.refCount == 0) { delete obj; } Are there any big advantages to how Java does it that I overlooked? Thanks Each JVM is different, but the HotSpot JVM does not primarily rely on reference counting as a means for garbage collection. Reference counting has the advantage of being simple to implement, but it is inherently error-prone. In particular, if you have a reference cycle (a set of objects that all refer to one another in a cycle), then reference counting will not correctly

Is a dynamic array automatically deallocated when it goes out of scope?

倖福魔咒の 提交于 2019-12-04 23:02:37
in this example procedure foobar; var tab:array of integer; begin setlength(tab,10); end; is the array destroyed or the memory is leaking? The memory is freed. (That is, no memory leak!) The array is automatically freed, but I've seen obscure cases where it isn't for some reason. I solved it by setting the array to nil. 来源: https://stackoverflow.com/questions/3113296/is-a-dynamic-array-automatically-deallocated-when-it-goes-out-of-scope

Why does this string have a reference count of 4? (Delphi 2007)

自作多情 提交于 2019-12-04 22:57:01
This is a very Delphi specific question (maybe even Delphi 2007 specific). I am currently writing a simple StringPool class for interning strings. As a good little coder I also added unit tests and found something that baffled me. This is the code for interning: function TStringPool.Intern(const _s: string): string; var Idx: Integer; begin if FList.Find(_s, Idx) then Result := FList[Idx] else begin Result := _s; if FMakeStringsUnique then UniqueString(Result); FList.Add(Result); end; end; Nothing really fancy: FList is a TStringList that is sorted, so all the code does is looking up the string

Reference-counting for objects

╄→гoц情女王★ 提交于 2019-12-04 20:10:21
问题 In my code I use a small data-storing class, which is created in different places. To avoid memory leaks and simplify things, I want to use reference counting, so I did type TFileInfo = class (TInterfacedObject, IInterface) and removed all my manual calls to TFileInfo.Free. Unfortunately Delphi reported a lot of memory leaks. Searching on SO I found the following question explaining why this doesn't work: Why aren't descendants of TInterfacedObject garbage collected? There is a workaround

STL class for reference-counted pointers?

↘锁芯ラ 提交于 2019-12-04 17:17:06
问题 This should be trivial but I can't seem to find it (unless no such class exists!) What's the STL class (or set of classes) for smart pointers? UPDATE Thanks for the responses, I must say I'm surprised there's no standard implementation. I ended up using this one: http://archive.gamedev.net/reference/articles/article1060.asp 回答1: With the exception of the already mentionned TR1 shared_ptr, there is no reference-counted pointer in STL. I suggest you use boost::shared_ptr (downloading boost will

C++ weak_ptr creation performance

不羁的心 提交于 2019-12-04 12:24:06
问题 I've read that creating or copying a std::shared_ptr involves some overhead (atomic increment of reference counter etc..). But what about creating a std::weak_ptr from it instead: Obj * obj = new Obj(); // fast Obj * o = obj; // slow std::shared_ptr<Obj> a(o); // slow std::shared_ptr<Obj> b(a); // slow ? std::weak_ptr<Obj> c(b); I was hoping in some faster performance, but i know that the shared pointer still have to increment the weak references counter.. So is this still as slow as copying

Reference Counting in C++ OO-Style

被刻印的时光 ゝ 提交于 2019-12-04 10:38:14
问题 I came accross an intriguing implementation of a base class on the C++ FAQ that, according to my naive understanding, could serve as an alternative to some of the smart pointer implementations (e.g. shared_ptr). Here's the example code verbatim, but please follow the link above for an explanation: class Fred { public: static Fred create1(std::string const& s, int i); static Fred create2(float x, float y); Fred(Fred const& f); Fred& operator= (Fred const& f); ~Fred(); void