reference-counting

Why do garbage collectors wait before deallocating?

混江龙づ霸主 提交于 2019-11-30 06:29:11
I have a "why does it work that way?" question about garbage collection (any/all implementations: Java, Python, CLR, etc.). Garbage collectors deallocate an object when it is no longer in any scope; the number of references pointing to it is zero. It seems to me that a framework could deallocate as soon as the number of references reaches zero, but all implementations I've encountered wait a while and then deallocate many objects at a time. My question is, why? I'm assuming that the framework keeps an integer for each object (which I think Python does, because you have to call PyINCREF and

ARC and autorelease

喜欢而已 提交于 2019-11-30 00:34:23
autorelease is used for returned function object so the caller don't take ownership and callee will release the object in the future. However, ARC is capable to count ownership of caller and release it after use, that is, it can behavior just like Smart Pointer in C++. With ARC, it can get rid of autorelease because autorelease is non-deterministic. The reason I ask for this question is that I do see the returned object calls dealloc earlier in ARC than non-ARC code. This leads me to think that ARC can behvior like Smart Pointer and can make autorelease useless. Is it true or possible? The

What does assigning a literal string to an NSString with “=” actually do?

三世轮回 提交于 2019-11-29 13:32:45
What does the following line actually do? string = @"Some text"; Assuming that "string" is declared thusly in the header: NSString *string; What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released? Thanks! The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point that label to another address in memory! It doesn't change reference counting or do anything beyond that in

Why aren't _AddRef and _Release called on my Delphi object?

核能气质少年 提交于 2019-11-29 12:25:12
I'm really confused. // initial class type TTestClass = class( TInterfacedObject) end; {...} // test procedure procedure testMF(); var c1, c2 : TTestClass; begin c1 := TTestClass.Create(); // create, addref c2 := c1; // addref c1 := nil; // refcount - 1 MessageBox( 0, pchar( inttostr( c2.refcount)), '', 0); // just to see the value end; It should show 1, but it shows 0. No matter how many assignments we'll perform, the value would not change! Why not? Refcount is only modified when you assign to an interface variable, not to an object variable. procedure testMF(); var c1, c2 : TTestClass;

What is a reference cycle in python?

爷,独闯天下 提交于 2019-11-29 09:06:40
I have looked in the official documentation for python, but i cannot seem to find what a reference cycle is. Could anyone please clarify what it is for me, as i am trying to understand the GC module. Thank you in advance for your replies. A reference cycle simply means one or more objects referencing each other, such that if you drew it out on paper with arrows representing the dependencies you would see a cycle. The (almost) simplest reference cycle is having two objects a and b that refer to each other: a.other = b b.some_attr = a Naive garbage collectors work strictly off of whether or not

Why do garbage collectors wait before deallocating?

荒凉一梦 提交于 2019-11-29 06:11:44
问题 I have a "why does it work that way?" question about garbage collection (any/all implementations: Java, Python, CLR, etc.). Garbage collectors deallocate an object when it is no longer in any scope; the number of references pointing to it is zero. It seems to me that a framework could deallocate as soon as the number of references reaches zero, but all implementations I've encountered wait a while and then deallocate many objects at a time. My question is, why? I'm assuming that the framework

Atomic Reference Counting

岁酱吖の 提交于 2019-11-29 03:30:51
I'm trying to understand exactly how thread-safe, atomic reference counting works, for example as with std::shared_ptr . I mean, the basic concept is simple, but I'm really confused about how the decref plus delete avoids race conditions. This tutorial from Boost demonstrates how an atomic thread-safe reference counting system can be implemented using the Boost atomic library (or the C++11 atomic library). #include <boost/intrusive_ptr.hpp> #include <boost/atomic.hpp> class X { public: typedef boost::intrusive_ptr<X> pointer; X() : refcount_(0) {} private: mutable boost::atomic<int> refcount_;

Why VC++ Strings are not reference counted?

人盡茶涼 提交于 2019-11-29 01:40:56
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-write semantics: #include <iostream> #include <string> using namespace std; void foo(string s) { cout<<(void*)s.c_str()<<endl; string ss=s; cout<<(void*)ss.c_str()<<endl; char p=ss[0]; cout<<(void*)ss.c_str()<<endl; } int main() { string s="coocko"; cout<<(void

Is “self” weak within a method in ARC?

 ̄綄美尐妖づ 提交于 2019-11-28 10:20:48
I have a method that occasionally crashes. -(void)foo{ [self doSomething]; [self.delegate didFinish]; [self doSomethingElse]; } -doSomething works correctly, then I call to a delegate -didFinish. Within -didFinish, the reference to this object might be set to nil, releasing it under ARC. When the method crashes, it does so on -doSomethingElse. My assumption was that self would be strong within a method, allowing the function to complete. Is self weak or strong? Is there documentation on this? What would the reasoning be for it being strong or weak? Edit Upon being inspired by some of the

What does assigning a literal string to an NSString with “=” actually do?

眉间皱痕 提交于 2019-11-28 07:30:12
问题 What does the following line actually do? string = @"Some text"; Assuming that "string" is declared thusly in the header: NSString *string; What does the "=" actually do here? What does it do to "string"'s reference count? In particular, assuming that for some reason "string" is not otherwise assigned to, does it need to be released? Thanks! 回答1: The assignment is just that. The string pointer is basically a label that points to specific address in memory. Reassignment statement would point