I disabled ARC.
I have a property in a class declared as follows:
@property(copy) NSString* name
I set name with a constant line:
[greeter setName:@"Jane"];
I implemented dealloc for my class as follows:
-(void)dealloc{ [super dealloc]; }
I was expecting a memory leak because I did not release name . I use Xcode 6.2, and Product>Analyze does not identify any leaks, and neither the tools nor the tools: Product>Profile, choose Leaks, hit the red Record button .
Here is the relevant code:
// // Greeter.h // Flashlight2 // #import <Foundation/Foundation.h> @interface Greeter : NSObject @property(copy) NSString* name; -(NSString*)description; -(void)dealloc; @end
....
// // Greeter.m // Flashlight2 // #import "Greeter.h" @implementation Greeter -(NSString*)description { NSString* msg = [[NSString alloc] initWithString:@"I am a Greeter"]; return [msg autorelease]; } -(void)dealloc{ [super dealloc]; } @end
...
@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. Greeter* greeter = [[Greeter alloc] init]; [greeter setName:@"Jane"]; //constant string in memory for life of program NSLog(@"%@", greeter); [greeter release]; return YES; }
After thinking about this for a while, the only explanation I can come up with is that the name setter does not actually copy the constant string. It seems that Obj-C is checking the type for the string being assigned to the property, and since it is a constant string, Obj-C simply sets (?) the-pointer-to-the-constant-string to name-pointer . Is something like this happening?
memory-leaks objective-c ios8
7stud
source share