reference-counting

Initializing a property, dot notation

泪湿孤枕 提交于 2019-12-17 01:40:10
问题 Is it a bad idea to use the dot notation to initialize retain properties to nil in my init methods? With any ordinary property like this: @property (nonatomic, retain) id foo; Say in my init method I set self.foo = nil . The synthesized method first releases or autoreleases foo (not exactly sure of the underlying impementation). Is foo guaranted to be nil before the first setter or getter call? Or would it point to random garbage unless I explicitly set foo = nil without the dot notation? 回答1

Simple reference counting: smart pointers

社会主义新天地 提交于 2019-12-12 10:49:42
问题 I would like to implement a simple reference counting using smart pointers. The variable pointer represents pointer to stored object, reference_count represents total count of copies of the object. if we initialize an object using NULL: reference_count = -1 else reference_count = 1 copy ctor and operator = increment reference_count destructor decrement reference_count and if there is no other reference to pointed object performs its deletion. Here is my code: #ifndef smart_pointer_H #define

PHP null and copy-on-write

白昼怎懂夜的黑 提交于 2019-12-12 10:14:09
问题 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

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

二次信任 提交于 2019-12-12 09:30:21
问题 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;

Thread safety of the reference count of a C++/CX WinRT pointer

房东的猫 提交于 2019-12-11 06:21:15
问题 I was under the impression that the reference count to WinRT objects was thread safe, given the use case. But I've run into a bug that I don't know any other way to explain. For example, the following code crashes quite quickly: ref class C sealed { public: C() { } virtual ~C() {} }; [Windows::Foundation::Metadata::WebHostHidden] public ref class MainPage sealed { public: MainPage() : _latest(nullptr) { InitializeComponent(); Windows::System::Threading::ThreadPool::RunAsync( ref new Windows:

How to delete a function argument early?

☆樱花仙子☆ 提交于 2019-12-11 04:07:37
问题 I'm writing a function which takes a huge argument, and runs for a long time. It needs the argument only halfway. Is there a way for the function to delete the value pointed to by the argument if there are no more references to it? I was able to get it deleted as soon as the function returns, like this: def f(m): print 'S1' m = None #__import__('gc').collect() # Uncommenting this doesn't help. print 'S2' class M(object): def __del__(self): print '__del__' f(M()) This prints: S1 S2 __del__ I

How can I get reference count for a managed object?

风格不统一 提交于 2019-12-11 03:48:22
问题 .NET profilers can show reference count to managed objects. How do they count them? 回答1: They use unmanaged APIs which provide access to the profiler. ICorProfilerCallback and ICorProfilerCallback2 are the main ones. These are the the interfaces that .NET profilers use. There are some more references like this. You can use the methods for class loads (ClassLoadFinished()) and unloads (ClassUnloadFinished()) to track this information. 来源: https://stackoverflow.com/questions/1471205/how-can-i

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

混江龙づ霸主 提交于 2019-12-10 02:58:33
问题 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 回答1: 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

Migrating to Unified API and new reference counting

廉价感情. 提交于 2019-12-09 23:36:05
问题 I had a Xamarin.iOS Classic project and migrated it to Unified API. I have a view controller in my code (simplified version): public class TestViewController : UIViewController { private WeakReference<UIView> _viewRef; public override void ViewDidLoad() { base.ViewDidLoad(); _viewRef = new WeakReference<UIView>(View); StartTimer(); } private async void StartTimer() { await Task.Delay(1000); // _viewRef is not alive here } } [Register("AppDelegate")] public class AppDelegate :

Does WinRT have Garbage Collection?

大城市里の小女人 提交于 2019-12-09 07:53:52
问题 Does WinRT have Garbage Collection? Or does it do reference counting as does COM? 回答1: I found this article, which cites Microsoft's Martyn Lovell: "WinRT objects are reference counted like COM for memory management, with weak references to avoid circularity." Apparently this was mentioned at his talk on WinRT internals at the BUILD convention. 来源: https://stackoverflow.com/questions/7424710/does-winrt-have-garbage-collection