reference-counting

When is NS_RETURNS_RETAINED needed?

假装没事ソ 提交于 2019-12-04 01:49:07
Take the below example: - (NSString *)pcen NS_RETURNS_RETAINED { return (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (__bridge CFStringRef) self, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8); } Is it correct to put the NS_RETURNS_RETAINED there? Another example: + (UIImage *)resizeImage:(UIImage *)img toSize:(CGSize)size NS_RETURNS_RETAINED { UIGraphicsBeginImageContextWithOptions(size, NO, 0.0); [img drawInRect:...]; UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resizedImage; } That

does dispatch_async copy internal blocks

瘦欲@ 提交于 2019-12-03 21:05:52
问题 Given the following (manual reference counting): void (^block)(void) = ^ { NSLog(@"wuttup"); } void (^async_block)(void) = ^ { block(); } dispatch_async(dispatch_get_main_queue(), async_block); Will "block" be copied rather than thrown off the stack and destroyed? 回答1: I believe, the answer is Yes. The outer block will be asynchronously dispatched which causes the runtime to make a copy on the heap for this block. And as shown below, and described in the Block Implementation Specification -

Reference-counting for objects

眉间皱痕 提交于 2019-12-03 13:50:17
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 presented there but it requires me (at least if i get it right) to write a custom interface IFileInfo and

Why are Python Ref Counts to small integers surprisingly high?

倖福魔咒の 提交于 2019-12-03 12:33:57
In this answer I found a way to get a reference count of objects in Python. They mentioned using sys.getrefcount() . I tried it, but I'm getting an unexpected result. When there is 1 reference, it seems as though the count is 20. Why is that? I looked at the documentation but it doesn't seem to explain the reason. This object happens to have 20 references to it at the time of the first sys.getrefcount call. It's not just the references you created; there are all sorts of other references to it in other modules and in the Python internals, since (this is an implementation detail) the standard

STL class for reference-counted pointers?

我们两清 提交于 2019-12-03 11:06:46
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 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 be enough, there is nothing to compile, its implementation is header-only). You may also want to have a

Does WinRT have Garbage Collection?

浪尽此生 提交于 2019-12-03 09:57:24
Does WinRT have Garbage Collection? Or does it do reference counting as does COM? 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

C++ weak_ptr creation performance

可紊 提交于 2019-12-03 08:07:47
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 a shared_ptr into another? Howard Hinnant In addition to Alec's very interesting description of the

Is there a non-reference-counted base class like TInterfacedObject?

夙愿已清 提交于 2019-12-03 06:01:25
I need a base class like TInterfacedObject but without reference counting (so a kind of TNonRefCountedInterfacedObject ). This actually is the nth time I need such a class and somehow I always end up writing (read: copy and pasting) my own again and again. I cannot believe that there is no "official" base class I can use. Is there a base class somewhere in the RTL implementing IInterface but without reference counting which I can derive my classes from? In the unit Generics.Defaults there is a class TSingletonImplementation defined. Available in Delphi 2009 and above. // A non-reference

Is atomic decrementing more expensive than incrementing?

喜欢而已 提交于 2019-12-03 05:52:09
In his Blog Herb Sutter writes [...] because incrementing the smart pointer reference count can usually be optimized to be the same as an ordinary increment in an optimized shared_ptr implementation — just an ordinary increment instruction, and no fences, in the generated code. However, the decrement must be an atomic decrement or equivalent, which generates special processor memory instructions that are more expensive in themselves, and that on top of that induce memory fence restrictions on optimizing the surrounding code. The text is about the implementation of shared_ptr and I am not sure

Delphi Interface Reference Counting

£可爱£侵袭症+ 提交于 2019-12-02 22:57:37
I ran into a strange situation while testing something today. I have a number of interfaces and objects. The code looks like this: IInterfaceZ = interface(IInterface) ['{DA003999-ADA2-47ED-A1E0-2572A00B6D75}'] procedure DoSomething; end; IInterfaceY = interface(IInterface) ['{55BF8A92-FCE4-447D-B58B-26CD9B344EA7}'] procedure DoNothing; end; TObjectB = class(TInterfacedObject, IInterfaceZ) procedure DoSomething; end; TObjectC = class(TInterfacedObject, IInterfaceY) public FTest: string; procedure DoNothing; end; TObjectA = class(TInterfacedObject, IInterfaceZ, IInterfaceY) private FInterfaceB: