reference-counting

How to implement thread safe reference counting in C++

How do you implement an efficient and thread safe reference counting system on X86 CPUs in the C++ programming language? I always run into the problem that the critical operations not atomic, and the available X86 Interlock operations are not sufficient for implementing the ref counting system. The following article covers this topic,...

why aren't descendants of TInterfacedObject garbage collected?

i have a class based on TInterfacedObject. i add it to TTreeNode's Data property. TFacilityTreeItem=class(TInterfacedObject) private m_guidItem:TGUID; m_SomeOtherNode:TTreeNode; public end; i create many instances of this object & had assumed that because they're reference counted, i shouldn't need to Free them. that'd be handy....

How to implement reference counted objects in Delphi

I have a graph like structure. I don't know exactly when to destroy the objects in traditional Delphi manner, instead I would like to implement something like reference counted objects. I know that I can use something like object.GetReference and object.Release instead of Free, and use a private variable for reference counting, but is th...

What is the best way to implement smart pointers in C++?

I've been evaluating various smart pointer implementations (wow, there are a LOT out there) and it seems to me that most of them can be categorized into two broad classifications: 1) This category uses inheritance on the objects referenced so that they have reference counts and usually up() and down() (or their equivalents) implemented....

Swig and refrence counted c++ classes

Many of my c++ objects implement rerfrence counting through AddRef and FreeRef methods. If FreeRef reduces the refrence count to 0 then the object deletes its self. All methods which return a refrence counted object dont increment the refrence. This makes it simple since a smart pointer can then simply increment the count apon recieving...

Why VC++ Strings are not reference counted?

STL standard do not require from std::string to be refcounted. But in fact most of C++ implementations provide refcounted, copy-on-write strings, allowing you passing string by value as a primitive type. Also these implementations (at least g++) use atomic operations making these string lock-free and thread safe. Easy test shows copy-on...

How to detect cycles when using shared_ptr

shared_ptr is a reference counting smart pointer in the Boost library. The problem with reference counting is that it cannot dispose of cycles. I am wondering how one would go about solving this in C++. Please no suggestions like: "don't make cycles", or "use a weak_ptr". Edit I don't like suggestions that say to just use a weak_ptr...

Reference-counting for objects

Hi. 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. Sear...

Why don't purely functional languages use reference counting?

In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the possibility of cycles. Am is right? If so, why don't they? I understand that reference counting i...

Why no Reference Counting + Garbage Collection in C#?

I come from a C++ background and I've been working with C# for about a year. Like many others I'm flummoxed as to why deterministic resource management is not built-in to the language. Instead of deterministic destructors we have the dispose pattern. People start to wonder whether spreading the IDisposable cancer through their code is ...

Microsoft objects, the Release() functions return value?

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, theref...

How to go about fixing a memory leak in PHP

My PHP app has an import script that can import records. At the moment, it is importing from a CSV file. It is reading each line of the CSV file, one line at a time using fgetcsv, and for each line it is doing a lot of processing on that record, including database queries, and then moving on to the next line. It shouldn't need to keep...

Detecting when an object is passed to a new thread in C++?

I have an object for which I'd like to track the number of threads that reference it. In general, when any method on the object is called I can check a thread local boolean value to determine whether the count has been updated for the current thread. But this doesn't help me if the user say, uses boost::bind to bind my object to a boost:...

What solutions are there for circular references?

When using reference counting, what are possible solutions/techniques to deal with circular references? The most well-known solution is using weak references, however many articels about the subject imply that there are other methods as well, but keep repeating the weak-referencing example. Which makes me wonder, what are these other me...

How can I determine if an object is reachable within an object graph in C#?

I have a pretty complex object graph G with an object o1 in G. G is to be written into a database using NHibernate. However, if there already is a persistent entry of o1 (let's call it o1_p ) in the database, I substitute o1 for o1_p. Hence there should be no redundant entries in the database. Now I let NHibernate do its job and afterwar...

WeakReference implementation in .NET

I understand and appreciate the usefulness of the System.WeakReference class in the .NET framework, but am curious as to the implementation details. How is WeakReference implemented in .NET? MSDN discusses the usage of WeakReference in detail, but has little details that I've seen on how this works under the hood. How does the CLR t...

C++ Storing large data in std::list<> ..should I use reference counting?

How do people normally manage copying a list of large objects around? Here's my situation: Currently I have this: typedef std::vector<float> Image; and I'm storing it in a std::list<Image> lst; The Image.size() is quite large (each is ~3-5 MB). I'm passing (copying) the list around. Is it a correct understanding on my part that...

C++: Multi threading and reference counting

Currently ive got some reference counted classes using the following: class RefCounted { public: void IncRef() { ++refCnt; } void DecRef() { if(!--refCnt)delete this; } protected: RefCounted():refCnt(0){} private: unsigned refCnt; //not implemented RefCounted(RefCounted&); RefC...

x86 equivalent for LWARX and STWCX

I'm looking for an equivalent of LWARX and STWCX (as found on the PowerPC processors) or a way to implement similar functionality on the x86 platform. Also, where would be the best place to find out about such things (i.e. good articles/web sites/forums for lock/wait-free programing). Edit I think I might need to give more details as ...

Delphi: Since when are interface references no longer released at the end of a with-block?

I recently stumbled over a problem caused by some very old code I wrote which was obviously assuming that interface references used in a with statement would be released as soon as the with-block is left - kind of like an implicit try-finally-block (similar to C#'s using-statement if I understood correctly). Apparently (in Delphi 2009) ...