NSString alloc outside autorelease pool creates mysterious memory leak? - objective-c

the following error is produced at runtime when manually allocating and releasing memory:
objc[10430]: Object 0x109014b60 of class __NSCFData autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() to debug
I searched the internet but usually these errors are caused by not releasing memory or using autoreleased shorthands... I cannot find any unreleased memory, nor am I using an autoreleased shorthand... Hoping you can help!
My code is:
#import "try.h"
#implementation try
- (try*)initWithSwing
{
self = [super init];
if (self) {
[self tellAboutSwing:YES];
}
return self;
}
- (void) tellAboutSwing: (BOOL) swing {
NSString *s = [[NSString alloc] initWithFormat: #"%# %#", #"Do we have a swing on our playground?", (swing ? #"Yes" : #"No")];
// cast to a char
const char *c = [s UTF8String];
[s release];
printf("%s", c);
}
- (void) dealloc {
printf( "Deallocing Playground\n" );
[super dealloc];
}
int main(void) {
try *t;
t = [[try alloc] initWithSwing];
[t release];
}
#end
Putting an autorelease pool inside the instance method fixes the issue, but that shouldn't be necessary with NSString alloc. While I am new to Objective-C I think I have grasped the concept of owning and releasing variables, yet I am having difficulty finding my error here.
Here's my .h file:
#include <Foundation/Foundation.h>
#interface try : NSObject
- (void) tellAboutSwing: (BOOL) swing;
#end
Help would be greatly appreciated :)
Regards,
Robert

IIRC -[NSString UTF8String] uses an autoreleased NSData to actually hold the bytes of the UTF-8 C string.
Update: Here's a simpler way using just C functions:
- (void) tellAboutSwing: (BOOL) swing {
printf("%s %s", "Do we have a swing on our playground?", (swing ? "Yes" : "No"));
}

You should just use an NSAutoreleasePool. It's normal to have one and at some point you won't find an alternative which is not creating autoreleased objects internally.
I think you should check out some tutorials and guides on memory management and the basics, otherwise you will have a hard time understaning Objective-C.
For example, there is a great course on iTunesU which is free and explains the basics of Objective-C.

Related

is it allowed to call [obj copy] in ObjC with ARC?

There's one thing I don't understand regarding ARC: how should we now treat local variables that were created using [... copy]? If I make a property with (copy) flag, ARC will handle this automatically, but as far as I know there's no __copy flag for variables.
I've tested this with such code:
#interface Foo : NSString
#end
#implementation Foo
- (void) dealloc {
NSLog(#"%p deallocated", self);
}
- (NSUInteger) length {
return 1;
}
- (unichar) characterAtIndex: (NSUInteger) i {
return 'x';
}
#end
- (void) foo {
Foo *f = [[Foo alloc] init];
NSLog(#"%p", f);
Foo *f2 = [f copy];
NSLog(#"%p", f2);
}
What I get is:
0x102406530
0x102015f10
0x102406530 deallocated
I never get "0x102015f10 deallocated", which would suggest the copied variable doesn't get released. It doesn't even get autoreleased, because when I made another method [Foo foo] that returned an autoreleased object, I did get a "deallocated" message a moment later.
So is there any way I can cause it to be released without converting it to a property?
Ok, my bad - ARC does actually handle copied objects properly. I got wrong results because of using NSString for the test, because I wanted to use a class that already implemented copying instead of implementing it explicitly; when I repeated the test on a class inheriting from NSObject and implementing copyWithZone: by returning [[Foo alloc] init], I got two "deallocated" messages. Thanks to #Paul.s for pointing that out.

Overriding methods in Objective-C

I am relatively new to Objective-C, and I have been thinking about the fact that all methods are, in effect, virtual. I created this little console program:
#import <Foundation/Foundation.h>
#interface BaseClass : NSObject
{
}
- (void) virtualMethod: (NSInteger) integer;
#end
#interface DerivedClass : BaseClass
{
}
- (void) virtualMethod: (NSString *) string;
#end
#implementation BaseClass
- (void) virtualMethod: (NSInteger) integer
{
NSLog(#"%ld", integer);
}
#end
#implementation DerivedClass
- (void) virtualMethod: (NSString *)string
{
NSLog(#"%#", string); // Program received signal: "EXC_BAD_ACCESS". -- as expected
}
#end
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *arr = [NSArray arrayWithObjects: [BaseClass new], [DerivedClass new], nil];
for (int i = 0; i < arr.count; i++)
{
BaseClass *b = (BaseClass *)[arr objectAtIndex: i];
[b virtualMethod: i];
}
NSLog(#"\n\nTapos na!\n\n");
[pool drain];
return 0;
}
As expected, I got an EXC_BAD_ACCESS in the derived virtualMethod:, since after all, it doesn't take an integer, it takes an NSString * as parameter. The virtual mechanism is based on selectors, and doesn't seem to take signatures into account.
My question: is there something in the language that could prevent such an override with a different signature from happening? Some way to tell the compiler that virtualMethod: should always have the same signature, or to make the compiler issue some kind of hint, warning or error if the signature doesn't match?
I know that a good programmer always indicates the names of types a method should have, but that is a convention, not a language rule. I am asking about a compiler feature to prevent the problem from happening.
Well, there is... but you probably don't want to use it. There's a build setting in Xcode called "Strict Selector Matching" (which passes through to the compiler as -Wstrict-selector-match). This will warn you if the compiler finds two selectors that have different parameter or return types.
Unfortunately, the warning comes up even if the types are different, but compatible. As such, if you turn it on, you'll get a bunch of spurious warnings places that you wouldn't expect to be ambiguous. Feel free to try it out, though.
If you'd like to read more about it, Matt Gallagher wrote up a nice post about it. In the meantime, I'm afraid there's not a great solution here.
The current version of XCode gives you the following warning:
"Conflicting parameter types in implementation of '[method]': '[the type needed]' vs '[the type you wrongly chose]'"

Subclassing NSMutableString

I know the first thing to do when one has a question with Obj-C is to read the Apple docs.
And I did. I read p.95-p.102 of the Cocoa Fundamentals Guide. So this is what I know about composite object creation from class clusters.
Subclassing of class clusters should be done if provided functions need to be modified.
Composite objects must override primitive methods of the super class. (And I also read that the primitive methods of the super class of the super class have to be overridden as well.)
So I applied what I know.
Here's my code...
#import <Foundation/Foundation.h>
#interface GOObjectKeyName : NSMutableString
{
NSMutableString *embeddedNSMutableString;
}
//+ GOObjectKeyName;
//- init;
//From NSString
- (NSUInteger) length;
// From NSString
- (unichar) characterAtIndex: (NSUInteger) index;
// From NSString
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange;
// From NSMutableString
- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString;
//- (void) dealloc;
#end
#import "GOObjectKeyName.h"
#implementation GOObjectKeyName
/*
+ GOObjectKeyName
{
return [[[self alloc] init] autorelease];
}
- init
{
self = [super init];
if (self)
{
embeddedNSMutableString = [[NSMutableString allocWithZone: [self zone]] init];
}
return self;
}
*/
- (NSUInteger) length
{
return [embeddedNSMutableString length];
}
- (unichar) characterAtIndex: (NSUInteger) index
{
return [embeddedNSMutableString characterAtIndex: index];
}
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
{
[embeddedNSMutableString getCharacters: buffer range: aRange];
}
- (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString
{
[embeddedNSMutableString replaceCharactersInRange: aRange withString: aString];
}
/*
- (void) dealloc
{
[embeddedNSMutableString release];
[super dealloc];
}
*/
#end
The commented methods are commented because I wasn't sure if they were necessary. I just wrote them down because I saw them in the example. And you also might see that it's basically another NSMutableString class. But don't worry, there is a bigger purpose to it, I just want to know that the basics are correct before I continue since this is my first time with composite objects.
And as you know... IT DOESN'T WORK!!
Please understand my frustration after hours of trial and error.
If someone could guide me here it would be a great relief.
Oh, and if you don't mind I have another small question.
The example on the apple docs describe the NSNumber analogy. But is NSNumber actually more efficient then using int, char, and other fundamental types?
Just curious.
It would certainly help if you would characterize the failure beyond "it doesn't work."
However, your -init method is commented out, so there's no way that embeddedMutableString is going to get created. And even if you uncomment it, you call +alloc to create embeddedMutableString, but you never initialize it.
After looking at the documentation, it seems that a subclass's -init method should look like this (warning: untested code):
-(id)init
{
if (self = [super init]) {
embeddedMutableString = [[NSMutableString alloc] init];
}
return self;
}
You will, of course, also have to provide a -dealloc that releases embeddedMutableString and calls [super dealloc].
As far as NSNumber, no, it's not more efficient than basic types like int but it does have other advantages. NSNumber is a class, so you can store instances of NSNumber in Cocoa's collection classes such as NSArray and NSSet. You can write them to property lists. NSDecimalNumber avoids the imprecision that comes from converting between decimal and binary notations. And so on...
To answer your NSNumber question, no, NSNumber is not more efficient than the C primitives. NSNumber is an encapsulation for the C primitives, essentialy allowing primitives to be inserted into objects like NSArray and NSSet. If you need that encapsulation ability, but want the speed of an actual int or float, it's simple enough to write your own NSNumber-like class that works for just one type.

Array Via Setter?

This is just a test to help me learn Objective-C, it uses NSMutableArray to add tire objects to an instance variable in a car object:
// INTERFACE
#interface CarBody : NSObject {
NSMutableArray *tires;
}
// Should this be (id *) it works but I was convinced it would be pointer?
- (void) addTire:(id)newTire;
#end
#interface TireSnow : NSObject {
}
#end
// IMPLEMENTATION
#implementation CarBody
- (void) addTire:(id)newTire {
[tires addObject:newTire];
// ** Release here or in main()?
}
- (id) init {
[super init];
tires = [[NSMutableArray alloc] init];
NSLog(#"_init: %#", NSStringFromClass([self class]));
return self;
}
- (void) dealloc {
NSLog(#"_deal: %#", NSStringFromClass([self class]));
[tires release];
[super dealloc];
}
#end
I do have a few questions ...
In the addTire method, is the (id) right, I thought it was going to be (id *)
Releasing the item I am adding to the array, should I do it inside the setter or in main() after I call it?
Am I allocating / releasing the NSMutableArray (tires) in the right place, it feels right?
Is there a way to do this with NSArray (as I only want 4 tires), I did try this but got mixed up trying to alloc the array and define its size.
thanks in advance for any help ...
gary
EDIT:
I am reading the memory management rules, but they do take some time to absorb and do require a certain level of understanding that is difficult to gain when starting out. What I am wondering about in this situation is where would I release the newSnowTire that I alloc in main. When I add it to the array in the setter does that create a new object in the array (thats my understanding) so my thinking was that I would need to release the instance I got from alloc?
// MAIN
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CarBody *newCarBody_001;
TireSnow *newSnowTire_001;
newCarBody_001 = [[CarBody alloc] init];
newSnowTire_001 = [[TireSnow alloc] init];
[newCarBody_001 addTire:newSnowTire_001];
// Clean up
[newCarBody_001 release];
[newSnowTire_001 release];
[pool drain];
return 0;
}
EDIT_002:
Just added the code to generate all 4 tires with the tire release moved into the loop after the setter is called.
// CREATE TIRES
for(int loopCounter=0; loopCounter<4; loopCounter++) {
newSnowTire_001 = [[TireSnow alloc] init];
[newCarBody_001 addTire:newSnowTire_001];
[newSnowTire_001 release];
}
I just checked this and it is correct ...
NewSnowTire_001 (alloc) RetainCount = 1
NewSnowTire_001 (addTire) RetainCount = 2
NewSnowTire_001 (release) RetainCount = 1
NewSnowTire_001 Finally Released by dealloc method.
(id) or (TireSnow*) is similar, I had problems with understanding this in the beginning too. So basically an object is of a pointer type (kind of), but the id is already a pointer, so you don't need a * after it.
In main. Releasing should happen in the same place as the alloc/retain/copy.
Seems okay to me.
You can use [[NSMutableArray alloc] initWithCapacity:4]. This is only a hint to the array, it will automatically expand if you insert more items. Check [tires length] in the addTire method.
Your -init should look more like this:
-(id)init
{
if (self = [super init]) {
// init here
}
return self;
}
This allows self to be nil if something breaks in the init-chain.
You should use id (not id*). Objective-C do not have a concept of a root object as you have in for example Java, where java.lang.Object is the root class for any and all classes. Cocoa adds two root classes (classes without a super class) named NSObject, and less common NSProxy. id is a pointer to any object regardless of super class.
It is unfortunate that id, and also Class, are defined as a pointers, which means they are the only places where you should not add the '*' character when defining references. An unfortunate legacy from the old days.
Release in main, you should always release objects int he same scope that you create or retain them. The addTire: method is exceptionally god example of this, never release objects that has been handed to you as an argument. Only release objects handed to you as a result (And even then only from the alloc, new and copy method).
The allocation and release of the instance variable tires is a schoolbook example of where it should be done. I would expand the init to check for the super class result, as this though (Never trust super to always work, or even return the same instance):
- (id) init {
self = [super init];
if (self) {
tires = [[NSMutableArray alloc] init];
NSLog(#"_init: %#", NSStringFromClass([self class]));
}
return self;
}
You can use NSArray if you have access to all four tires from the start. Best way would probably be to require the tires in the init method. If that is not a possibility then you have nothing to gain from using an NSArray over a NSMutableArray.
The type id is defined like this (in objc.h):
typedef struct objc_object {
Class isa;
} *id;
So id is already a pointer to an object. An id* would be a pointer to a pointer.
As for where you should release the tire — there's nothing in the code you posted that shows a need to release it at all. That object never claims ownership of the tire, so it has no need to release it. If something claimed ownership of the tire somewhere else in your code, then that object has a responsibility to release its claim when it's finished.
This is explained in the Objective-C memory management rules. It's pretty short and a must-read.

Objective C Memory Management Confusion

I was reading the apple documentation for memory management, and came across something that I just don't understand. Basically, I don't understand why one does not need need to retain an instance variable through the "getter" method. I wrote this little program to see what would happen. I thought there would be a crash, but I am obviously missing something.
// main.m
// Test
//
#import <Foundation/Foundation.h>
#import "Test.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
//Initialize the test object
Test *t = [[Test alloc] init];
//Set the value to 5
[t setMyNum:[NSNumber numberWithInt:5]];
//Save a temp number that points to the original number
NSNumber *tempNum = [t myNum];
//release old number and retain new
[t setMyNum:[NSNumber numberWithInt:7]];
//Shouldn't this crash because tempNum is pointing to a deallocated NSNumber???
NSLog(#"the number is %#",tempNum);
[p drain];
return 0;
}
Doesn't tempNum point to a deallocated object??
All help is appreciated.
EDIT
This is the code in the getter and setter methods
#import "Test.h"
#implementation Test
- (void)setMyNum:(NSNumber *)newNum {
[newNum retain];
[myNum release];
myNum = newNum;
}
-(NSNumber *)myNum {
return myNum;
}
#end
As you can see I am calling release on the old object.
EDIT
It was suggested, and I thought rightfully so that the reason the tempNum is still around is because it hadn't been autoreleased from the pool yet. But even after moving the [pool drain] to right before the NSLog message, there is not crash??? Weird.
Since you are not explicitly releasing any objects, nothing is being deallocated until the autorelease pool is allowed to drain. Try inserting [p drain] before the last NSLog call. It should crash the NSLog call.
Additionally, if you are not retaining the NSNumber in your setMyNum: method, you will find that it will crash if you add [p drain] before tempNum is assigned.
To clarify the original question, calling a getter method doesn't (and shouldn't) necessarily imply that the caller wants to take ownership (i.e. retain) the variable. If that was the case, this code would leak:
NSLog("Number is %#", [t myNum]);
Also, it appears that NSNumber has an optimization whereby for small numbers, they cache the NSNumber objects, retain an extra copy, and return that version. So for small constants, [NSNumber numberWithInt: N] will return an object with 2 reference counts (available via [theNumber retainCount]). To explicitly see what happens, use a larger constant in the program, an NSNumber will retain a 'fresh' object with a reference count of 1 (that will also be autoreleased).
#import "Test.h"
#implementation Test
- (void)setMyNum:(NSNumber *)newNum
{
[newNum retain];
[myNum release];
myNum = newNum;
}
-(NSNumber *)myNum
{
return myNum;
}
#end
Here in the setter method [myNum release] which releases the mynum, but then we are again giving some new value that is newnum, hence from the getter method the temporary number gets the number which has not been deallocated till the [p drain] so there will not be any crash.
#import "Test.h"
#implementation Test
(void)setMyNum:(NSNumber *)newNum
{
[newNum retain];
[myNum release];
myNum = newNum;
}
(NSNumber *)myNum
{
return myNum;
}
#end
Here is the setter method [myNum release]; Which releases the myNum, but then we are again giving some new value that is newNum, hence from the getter method the temporary number gets the number which has not been deallocated till the [p drain]; so there will not be any crash. Even if the following code will not crash as there is autorelease pool but no autorelease method.
[t setMyNum:[NSNumber numberWithInt:70]];
So releasing the pool will not deallocate the number.