FAQ Database Discussion Community


Why weak reference couldn't be supported in ARC enabled platform?

objective-c,xcode,pointers,automatic-ref-counting
I'm re-reading "Transitioning to ARC Release Notes" now. In the beginning, it says ARC is supported in Xcode 4.2 for OS X v10.6 and v10.7 (64-bit applications) and for iOS 4 and iOS 5. Weak references are not supported in OS X v10.6 and iOS 4. Why is this? Is...

Does Objective-C forbid use of structs?

objective-c,struct,automatic-ref-counting
I'm new to Objective C I tried using a simple struct and got arc forbids objective-c objects in struct Looking up ARC, it looks like this is the specification that defines Objective C syntaxt - is that correct? Secondly, how do I go about using struct if it's not allowed?...

self.dictionary[blah] = object with block that uses self: creates a ref-count loop in ARC?

objective-c,automatic-ref-counting,cycle
I've got a class using ARC that has a member property that's a mutable dictionary. I then set a value for a key in that dictionary. That value is an object holding a block that references self for the object. - (void)doSomethingLaterWithVar:(id)var forSlot:(id)slot { self.doThingsLater[slot] = [DoLaterThingie doAfterNSecs: 5 block:...

Objective-c readonly copy properties and ivars

objective-c,properties,automatic-ref-counting
I'm try to grok properties declared as both copy and readonly in objective-c, and specifically, whether I have to do the copy myself. In my init methods. Evidence suggests I do: @interface A : NSObject @property(nonatomic, copy, readonly) NSData *test; - (instancetype)initWithData:(NSData *)data; @end @implementation A - (instancetype)initWithData:(NSData *)data {...

iOS: View controllers are not releasing when dismissing

ios,objective-c,automatic-ref-counting
I am dismissing the current controller to my root controller like so. [navigationController popToRootViewControllerAnimated:YES]; However, all the view controllers in navigation controllers are not being released (I can tell because methods in the controller classes are being called twice and dealloc method is not being called). I need advice on...

Can ARC manage non-toll-free-bridged Core Foundation objects?

objective-c,automatic-ref-counting
If I have a non-toll-free-bridged Core Foundation object, can I safely transfer ownership to ARC, or is that privilege reserved for toll-free-bridged types? For example: - (id)myBundle { CFBundleRef b = CFBundleCreate(NULL, self.bundleURL); return b == NULL ? nil : (__bridge_transfer id)b; } - (UInt32)myBundleVersionNumber { return CFBundleGetVersionNumber((__bridge CFBundleRef)self.myBundle); }...

When a view controller is dismissed, should it empty the memory?

ios,objective-c,memory-management,uiviewcontroller,automatic-ref-counting
I'd like to learn about memory management in Objective-C which I find not that easy because I'm fairly new at Objective-C and ARC and I'm mostly used to script languages for which I don't have to deal that much (or not at all) with memory management. The app I'm working...

Reference Counting with Instruments

cocoa,swift,memory-management,automatic-ref-counting,instruments
I put the code below in applicationDidFinishLaunching:, and launched the app via Instruments, in conjunction with the Allocations tool. func applicationDidFinishLaunching(aNotification: NSNotification) { var a = Apartment() var b = a var c = a var d = a } Apple's documentation on memory management in Swift includes the following...

I have 2 issues preventing the conversion to ARC tool from running on xCode 6.3.1

ios,iphone,xcode,automatic-ref-counting
Issue 1... There is a whole lot of these releases that were automatically created when I initially wrote this code several years ago. I am assuming that all the others will go away after running the ARC migration tool. Why is this one being singled out, and how do I...

Is it safe to read a weak pointer while it's being deallocated?

objective-c,automatic-ref-counting,race-condition
Is it safe to read a nonatomic weak pointer from a different thread than the object is being deallocated from? In general, I know properties should be made atomic whenever there may be simultaneous accesses, at least one of which is a write operation. But I have to wonder if...

Is the memory assigned to a strong property released as soon as the property is set to nil?

objective-c,memory-management,automatic-ref-counting
I would like to know how to release a strong property/iVar. I would like to clarify why I intend to do this. I own an object via any one of these declaration: @interface MyClass : NSObject @property (readwrite, nonatomic) MyObject *hugeObject; @end OR @interface MyClass : NSObject { MyObject *hugeObject;...

Potential reference count issues unless grabbing fresh reference in background thread

ios,multithreading,core-data,automatic-ref-counting,nsthread
I have a second question after reading Marcus S. Zarra's (excellent) Core Data: Data Storage and Management for iOS, OS X, and iCloud (2nd edition) if I may. The book's section Asynchronously Adding the NSPersistentStore contains this piece of code (excerpt): dispatch_queue_t queue; queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ //...

Strong vs Retain in ARC

ios,objective-c,memory-management,properties,automatic-ref-counting
I know there has been numerous write ups on strong vs weak. But all docs says that both are 100% synonym for each other and you can use strong in replace of retain and vice versa. My question is if they are same why apple introduces new 'strong' keyword. I...

Autoreleasepool drained only at end of app?

ios,objective-c,memory-management,automatic-ref-counting
In ios a primary autorelease pool wraps around UIApplicationMain. If I don't manually set up other autorelease pools inside the app, does that mean that whenever I release an object in the app with autorelease, it will not actually be released until UIApplicationMain is done, the pool is drained, and...

using a strong NSProgress with downloadtaskwithrequest

ios,automatic-ref-counting,afnetworking-2
I'm facing a strong vs. autorelease problem : I'm using an object which have a strong NSProgress to manage some file download. For downloading, i'm using downloadtaskwithrequest from AFNetworking. My problem is that this method take a NSProgress * __autoreleasing * which is not compatible with my strong NSProgress :...

ARC(Automatic Reference Counting) in Action

swift,automatic-ref-counting
I am currently following the Apple Documentation. Here is my question: class Person { let name: String init(name: String) { self.name = name println("\(name) is being initialized") } deinit { println("\(name) is being deinitialized") } } class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() var reference1: Person? var reference2:...

NS_ENUM as property in protocol

ios,objective-c,automatic-ref-counting
I came across to strange behaviour. I used to have: @property (nonatomic) ApplicationState applicationState; directly in my Application class. Now it's extracted to protocol @protocol ApplicationProtocol <NSObject> @property (nonatomic) ApplicationState applicationState; ApplicationState is Enum typedef NS_ENUM(NSUInteger, ApplicationState) { ApplicationStateNormal = 0, ApplicationStateExpanded = 1, ApplicationStateMaximized = 2 }; Now. It...

Why is the memory allocated from componentsSeparatedByString never being allocated

ios,objective-c,memory-management,automatic-ref-counting
I have a iOS app which does alot of calculation and is using standard ARC for memory management. After I run it for a few minutes it crashes due to being out out memory. I checked with Instruments and most of the memory is being eaten up by allocations from...

iOS: pointer reference to an array

ios,objective-c,automatic-ref-counting
I have a table view with a search, and search scope buttons with two possible scopes. The table is empty until a search is executed. Each scope has it's own mutable array for the table's data source, we'll say scopeA_array and scopeB_array. To simplify some methods, I'm trying to create...

Generate Macular Grid in Javascript

javascript,automatic-ref-counting,circle,easeljs
How to generate macular grid in Javascript? In Macular grid contains a no.of circles that should in 'V' shape format. Creation of dotted circles in 'V' shape? let us know how to resolve this? To Identify the Macular grid shape, please refer the below link i have added a screenshot....

Deadline for moving iOS apps to ARC from Non-ARC

ios,automatic-ref-counting
Apple announced that mac apps that are submitted to appstore from May 1, 2015 must be ARC enabled. Is there any deadline for iOS apps to move to ARC from non-ARC?? Thanks in advance....

How efficient is changing window root viewcontroller?

ios,swift,uiviewcontroller,automatic-ref-counting,uiwindow
Im making an app which the user can log in to and log out. I check if a saved user exists (from earlier logins) and if a user is saved I log that user in and then change rootviewcontroller for the window to the viewcontroller that should appear when logged...

How to call dispatch_block_t directly?

objective-c,osx,automatic-ref-counting,grand-central-dispatch
I am trying to update some code for XCode 6.1 / Yosemite. It's a bit weird because it's a macro, but essentially it looks like: dispatch_block_t blk = ^{ [[self globalEventsHandler] someMethod self]; }; if([NSThread isMainThread]) blk(); else dispatch_async(dispatch_get_main_queue(), blk); This is causing compilation problems. I have already set OS_OBJECT_USE_OBJC=0...

beginAnimations with “context”variable in ARC?

ios,objective-c,animation,automatic-ref-counting,bridge
The question partially similar to existing ones but I still get error with memory management. The following non-ARC code work: [UIView beginAnimations:... context:[[NSNumber numberWithInt:i] retain]]; and somewhere in didStopSelector: NSNumber * n = (NSNumber *)context; ... [n release]; I tried to remove retain/release and to add copy (and combined these...

NSViewController not dealloc'd when outlets are bound

automatic-ref-counting,iboutlet,nsviewcontroller,nsstoryboard
I have the following files: Main.storyboard This is the same as the default storyboard created when creating a new project with a few additions: There is a button in the view which is connected to the button outlet of the view controller (instance of ViewController) and the doTheThing: action on...

Swift, two issues. 1) weak var 2) bang operator for @IBOutlet

swift,operators,automatic-ref-counting,iboutlet
Per: @IBOutlet weak var nameLabel: UILabel! Whenever I declare my IBOutlets, i just use var instead of weak var. But I've recently come across a few code templates that use weak var. Why do they do it? What's the added benefit? Why is there a bang operator at the end...

iOS concurrency: NSOperationQueue and ARC issue

objective-c,multithreading,concurrency,automatic-ref-counting,nsoperationqueue
I am currently implementing a multithreaded application and I encounter a strange race condition (leading to an ARC problem: error for object 0x7f8bcbd6a1c0: pointer being freed was not allocated). The app creates multiple NSOperations, each one is downloading and processing information. As in every one of these NSOperations an error...

Two exactly same SVG paths not drawn exactly the same way

svg,rendering,automatic-ref-counting,antialiasing
I'm drawing two exactly same paths on top of each other to an SVG canvas. The path behind can be seen even though the other path should hide it. Here's the SVG: <svg viewBox="0 0 100 100"> <!-- BLUE PATH --> <path id="blue" d="M 50,50 m 0,-48 a 48,48 0...

Transitioning an Objective C project to ARC and get rid of multiple app crashes

ios,crash,automatic-ref-counting,release,retain
I spent few days working on an app that was built by my friend last year before the release of iOS 8 and Xcode 6+ I believe. He used several libraries like PMCalender , FXforms, MyBlurIntroductionView, CCPickerView, SWRevealTableView etc and I added MBProgressHUD. Now before the app was running without...

Strong reference to `self` to keep the object alive (temporarily): evil?

ios,objective-c,automatic-ref-counting,uialertview,object-lifetime
I'm creating a wrapper for UIAlertView (I know about UIAlertController and about several already existing wrappers, it's also for educational purposes). Suppose it looks like this (very shortened version): @interface MYAlertView : NSObject -(void)show; @end @interface MYAlertView()<UIAlertViewDelegate> @end @implementation MYAlertView -(void)show { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Some title" message:@"Some...

Swift weak self function retention

swift,automatic-ref-counting
If I have a closure that references a weak var weakSelf = self, can I change that closure to a direct function reference, through weakSelf? struct ClosureHolder { let closure: () -> Void } class ClosureSource { func hello() { NSLog("hello") } func createWeakSelfWithinInnerClosureClosureHolder() -> ClosureHolder { weak var weakSelf...

Why Does it Matter if An Object is Nil When Destroyed?

objective-c,automatic-ref-counting
I'm reading an up-to-date book and ran across the ARC section. It explains these two ARC qualifiers: Weak: Weak indicates that the object should not be retained (does not increase retain count). The compiler will set to nil before destruction. __unsafe__unretained: Same as weak, but the object is not set...

Using dispatch_write to write NSData to unique file in background under ARC

ios,automatic-ref-counting,grand-central-dispatch,nsdata
I am attempting to create a file with a unique name and write data to it in the background. mktemp says Whenever it is possible, mkstemp() should be used instead, since it does not have the race condition. Using mkstemp results in an open file descriptor, so dispatch_write seems to...

prevent retain cycle in Swift function pointers

swift,automatic-ref-counting
How do I prevent a retain cycle when passing around functions as objects in Swift Imagine you have a datasource object like this import UIKit class MagicDataSource:NSObject,UITableViewDatasource { deinit { println("bye mds") } //cant use unowned or weak here var decorator:((cell:CustomCell)->Void)? func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath:...

Draw arc on canvas from two x,y points and a center x,y point

javascript,canvas,html5-canvas,automatic-ref-counting,trigonometry
i'm trying to draw a arc from two points (X, Y cords). But i can't figure out how to do it so that i can specify the start angle and end angle I got the center point(p2), radius = r. the start point(p1) and the end point(p3). like shown below...

Objective C - Self Zeroing weak pointer unexpected behaviour

objective-c,automatic-ref-counting,weak-references
I have recently upgraded from Mavericks to Yosemite and now my unit tests are failing. The problem boiled down to a typo in a weak pointer to string content. Please see the following sample code: NSString* value1; NSString* value2; __weak NSString* weakValue1; __weak NSString* weakValue2; NSMutableString* resultText = [NSMutableString new];...

Validity of these pointers

objective-c,automatic-ref-counting
I am currently trying to improve my grasp on pointers and ARC. Consider the following example @implementation Foobar -(NSArray *)methodA { return self.someArray; } -(NSArray *)methodB { return [[NSArray alloc] init]; } @end @interface Foo : NSObject @property(strong) NSArray * myArrayA; @property(strong) NSArray * myArrayB; @end @implementation Foo -(void)fooMethod {...

How do you remove icons from Launchpad on Mac after installing Arc and uploading an APK?

osx,google-chrome,automatic-ref-counting,google-chrome-arc,launchpad
I installed a few apk files for applications and now the icon's won't go away from the Launchpad on my Mac. Is there a file, database or some other location where these are stored? Thank you for any help....

viewDidLoad deallocated prematurely in ARC?

objective-c,osx,automatic-ref-counting
I'm writing an app which has a NSOutlineView, and an interface which implements NSOutlineViewDataSource. However, at runtime, the app crashes with an error that a message was sent to a deallocated instance. I did some debugging with zombies and and Instruments and I found the address that was deallocated. I...

Returning address of property

cocoa,cocoa-touch,properties,automatic-ref-counting
A function call in class A requires and argument as (NSOutputStream **) the stream i want to pass is hold in a property of class B @property (nonatomic, strong) NSOutputStream * outputStream; i wanted to make this property of class B accessible like this to the outer world (for class...

How to dealloc CGPoint, CGRect variables?

objective-c,memory,core-graphics,automatic-ref-counting,dealloc
I'm using Objective-C. I use automatic-ref-counting. I have a lot of CGPoint and CGRect variables in my code. I get memory errors. How can I dealloc after used.

How to disable ARC in Xcode 6

objective-c,xcode,automatic-ref-counting
I want to experiment with manual memory management, because I like to see what can happen if i call [NSObject release] even when i haven't released it for real. I'm not trying to get rid of ARC or anything, i just want to experiment. I've already tried to uncheck the...

In Objective C, When is weak object released under ARC?

objective-c,automatic-ref-counting,weak-references
Since the reference count becoming 0 will cause the object being released. I am curious when is a weak object released since the count is always 0, when I declare a weak one, do I need to worry about it will be released half way? For example NSObject ClassA @property...

NSURLConnection consuming huge memory

ios,automatic-ref-counting,nsurlconnection
I'm using NSURLConnection to interact with the server side and I have observed that when the server take time to respond the system allows about 40 mo. I don't know if I'm the only one to have this problem. thanks in advance.