automatic-ref-counting

ASIHTTPRequest / ASIFormDataRequest - referencing request object within blocks under ARC

南笙酒味 提交于 2019-11-28 06:24:33
Very similar to this question , I am trying to convert a project that uses ASIHTTPRequest & ASIFormDataRequest to ARC. In my view controller classes, I often refer to and use properties of the request object in the completion blocks (looking at the response code, response data etc): __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:SOME_URL]]; [request setCompletionBlock:^{ if([request responseStatusCode] == 200) ....etc When converting to ARC I get the warning: Capturing 'request' strongly in this block is likely to lead to a retain cycle What is

Make self weak in methods in Swift

99封情书 提交于 2019-11-28 06:21:32
I have a Swift class that needs to store a table of its own methods. Unfortunately this is causing a reference cycle, because its table retains references to self via the methods it stores. Example leaky code below: typealias Callback = ()->() class CycleInducingClass : NSObject { var myCallbacks = [Callback]() override init() { super.init() myCallbacks.append(myInternalFunction) } func myInternalFunction() { NSLog("lolol: %d", self.myCallbacks.count) } } The only solution I've found so far is to instead do this: myCallbacks.append({[unowned self] in self.myInternalFunction()}) That's pretty

Manual object lifetime with ARC

眉间皱痕 提交于 2019-11-28 05:34:22
问题 Examine the following code, and assume it was compiled under ARC: - (void)foo { NSOperationQueue *oq = [[NSOperationQueue alloc] init]; [oq addOperationWithBlock:^{ // Pretend that we have a long-running operation here. }]; } Although the operation queue is declared as a local variable, its lifetime continues beyond the scope of the method as long as it has running operations. How is this achieved? UPDATE: I appreciate Rob Mayoff's well-thought-out comments, but I think I did not ask my

Do we need to use __weak self inside UIAnimationBlocks in ARC?

不想你离开。 提交于 2019-11-28 04:44:46
Do we need to use __weak self inside UIAnimation Blocks as given below? Whether it will create retain cycle issue if we are not specifying self as weak? [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseInOut animations:^{ [self doSomething]; } completion:^(BOOL finished) { if (finished) { [self doSomething]; } }]; I am also confused in the following scenario. Any thoughts on this? please share your comments. [self.navController dismissViewControllerAnimated:animated completion:^{ [self doSomething]; }]; Should we use weak self here? This is not a retain

cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast

时间秒杀一切 提交于 2019-11-28 04:20:20
When converting an Objective-C program to a Objective-C ARC, I get the error: "cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast " The code is as follows: - (NSString *)_encodeString:(NSString *)string { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, // this is line in error NULL, (CFStringRef)@";/?:@&=$+{}<>,", kCFStringEncodingUTF8); return [result autorelease]; } What is a bridged cast? Have a look at the ARC documentation on the LLVM website. You'll have

Why Swift closure not capture self?

↘锁芯ラ 提交于 2019-11-28 03:59:37
问题 I was testing swift closure with Xcode playground. This is my code: import UIKit class A{ var closure: ()->() = {} var name: String = "A" init() { self.closure = { self.name = self.name + " Plus" } } deinit { print(name + " is deinit") } } var a: A? a = A() a = nil As what is expected, a is self contained by closure, so a is never released. But, when I add this line before the last line: a?.closure = { a?.name = "ttt" } Then, I found "A is deinit" in the output window, which means a is

Swift Managing Memory

不想你离开。 提交于 2019-11-28 03:59:23
This question was cleaned up and the important info moved to the answer below. I have some questions about memory management. I am building a photo editing app. So keeping memory usage low is important. Also I am not going to post code because I do not have a big memory leak when doing one specific thing. I just lose a couple of KB's/MB's with everything that happens. And going over tens of thousands of lines of code to find kilobytes is no fun ;) my app uses core data, lots of cifilter stuff, location, and the basics. My first view is just a tableview which costs me about 5mb of memory. Then

Should an NSString property under ARC be strong or copy?

China☆狼群 提交于 2019-11-28 03:46:29
When not compiling with ARC, it is recommended to use copy properties for data types such as NSString . I could not find proper documentation on the use of copy in ARC mode. Can someone tell me what's applicable for ARC? It is still recommended to copy because you want to avoid something passing a mutable string and then changing it without you knowing. A copy guarantees that the string you have will not change. Copying and ARC are orthogonal: you make copies of mutable objects to "freeze" their state; ARC keeps track of object's reference count. NSString objects may or may not be mutable.

NSThreads in Automatic Reference Counting(ARC)

丶灬走出姿态 提交于 2019-11-28 03:45:37
问题 i am trying to use NSThreads with ARC in 4.3.5. With iOS 5 everything works perfect, but if i try it on a older iOS like 4.3 its leaking. Normally i would use a Autoreleasepool for NSThreads but since there is no manual Autoreleasepool in ARC i don't know how to fix this. I get loads of Messages like "__NSAutoreleaseNoPool(): Object 0x4567b40 of class NSComparisonPredicate autoreleased with no pool in place - just leaking" in my Console after i start a Thread. NSThread detachNewThreadSelector

ARC __bridge modifiers demystified

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:35:14
I was asked recently by one of my friends about the new bridge modifiers that became active under ARC. He asked me if I knew which ones to use in specific times and what the difference between the different __bridge modifiers were. He asked me,"so how do they work, when do I use them, how do I use them , and how do they work "under the hood" "? Note: This was supposed to be a "Share your knowledge" type of question, where I answered the question myself, but I'm not sure I set it up properly. Because I learned what they were and how they operated just recently, I want to share with anyone else