Reputation: 13
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
Upvotes: 1
Views: 672
Reputation: 540
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.
Upvotes: 1
Reputation: 28242
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"));
}
Upvotes: 5