reference-counting

why aren't descendants of TInterfacedObject garbage collected?

点点圈 提交于 2019-12-09 00:06:29
问题 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. however, when checking this, i turned on ReportMemoryLeaksOnShutdown and found they're not being freed after all. these objects are being created in a frame that's

Implementing reference counts with C++

為{幸葍}努か 提交于 2019-12-08 07:20:45
问题 I'm working on my digital signal processing framework. To provide a data exchange interface, I wrap all buffers in a Data class, which has a reference count based GC mechanism(the system is simple enough so I believe ref count can handle this). It works like this: When a Data instance is constructed, it sets its ref count to zero. When the instance is dispatched to N DSP modules, add N to its ref count. When a DSP module finishes with the instance, it reduces the ref count. When the ref count

Conditionally move T out from Rc<T> when the count is 1

落花浮王杯 提交于 2019-12-08 02:04:31
问题 Is there a way to move an object from an Rc<T> when the count is 1 ? I am thinking about how one would implement: fn take_ownership<T>(shared: Rc<T>) -> Result<T, Rc<T>> { ... } The semantics would be that you get T if the count is 1 and you get back shared otherwise so you can try again later. 回答1: The standard library provides the Rc::try_unwrap function: fn try_unwrap(this: Rc<T>) -> Result<T, Rc<T>> Returns the contained value, if the Rc has exactly one strong reference. Otherwise, an Err

How to better initialize a reference counter for a non-creatable COM object?

雨燕双飞 提交于 2019-12-07 11:22:15
问题 I have a COM interface with a method that returns an object: interface ICreatorInterface { HRESULT CreateObject( IObjectToCreate** ); }; The key is that calling ICreatorInterface::CreateObject() is the only way to retrieve an object implementing IObjectToCreate interface. In C++ I could do it this way: HRESULT CCreatorInterfaceImpl::CreateObject( IObjectToCreate** result ) { //CObjectToCreateImpl constructor sets reference count to 0 CObjectToCreateImpl* newObject = new CObjectToCreateImpl();

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

拥有回忆 提交于 2019-12-07 06:06:04
问题 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)-

Microsoft objects, the Release() functions return value?

橙三吉。 提交于 2019-12-07 04:48:28
问题 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

Detecting memory leak in reference counted objects

烈酒焚心 提交于 2019-12-07 02:49:15
问题 I am trying to print on which line addref and release is called.Here is code In code below I have created on ReferenceCount class whose main functionality to increase and decrease refernce count. Referencemanager class keeps track of reference count and deletes the object once it reaches 0. Test1 is test class .In main I am creating Test1 pointer and wrapping it with CReferenceManager class. Now during creation of CReferenceManager class AddRef is called and while destruction Release would be

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

a 夏天 提交于 2019-12-06 17:19:31
问题 in this example procedure foobar; var tab:array of integer; begin setlength(tab,10); end; is the array destroyed or the memory is leaking? 回答1: The memory is freed. (That is, no memory leak!) 回答2: 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

PHP null and copy-on-write

流过昼夜 提交于 2019-12-06 11:08:52
Suppose I want to have two variables and have them both equal to null . (More realistically, I am thinking about an array that contains a large amount of null s, but the "two variables" scenario is sufficient for the question.) Obviously, I can do this in more than one way. I can do this (method 1): $a = null; $b = $a; By my understanding, the result of this is that there is one zval that is pointed to by two entries in the symbol table: 'a' and 'b' . But alternatively one might do this (method 2): $a = null; $b = null; Naively one would expect that this should result in two different zvals,

Value stored during its initialization is never read

谁都会走 提交于 2019-12-06 01:55:40
I am trying to create a Game so that I can change its data and save it back. I get two errors that are on the commented lines. Why am I getting these errors. I allocated the Game so I should have to release it correct. Here is my code to save my Game Game *newGame = [[Game alloc] init];//error 1 newGame = [gamesArray objectAtIndex:gameNumber]; [newGame setTheShotArray:shotArray]; [gamesArray replaceObjectAtIndex:gameNumber withObject:newGame]; NSString *path = [self findGamesPath]; [NSKeyedArchiver archiveRootObject:gamesArray toFile:path]; [newGame release];//error 2 I get error 1 which says