Instances of NSObject acting as NSUrlConnection delegate appear not to be isolated - iphone

First post here, so I hope it is detailed enough.
While developing an Iphone App I am confronted with some strange behavour. A member variable of a certain instance of my "WebserviceConnection" class seems to obtain the value I assign to another instances of the same class.
For illustration: This is en excerpt of my log. I assume the 0x000000 is an instance ID. The fourth response should be "<-: 1".
2011-11-03 16:25:13.227 Dashboard[540:707] ->: 1, <WebserviceConnection: 0x11f950>
2011-11-03 16:25:13.256 Dashboard[540:707] ->: 0, <WebserviceConnection: 0x323db0>
2011-11-03 16:25:15.318 Dashboard[540:707] <-: 0, <WebserviceConnection: 0x323db0>
2011-11-03 16:25:15.325 Dashboard[540:707] <-: 0, <WebserviceConnection: 0x11f950>
The class is a NSUrlConnection delegate which exhibits this behavour when two connections are open at the same time.
This class: WebserviceConnection.h
(The ConnectionType is an enum)
#import "WebserviceConnection.h"
#import "WebserviceUtility.h"
#implementation WebserviceConnection
BOOL isCanceled;
NSDictionary *result;
ConnectionType connectionType;
id <WebserviceConnectionDelegate> delegate;
- (id)initWithDelegate:(id)webServiceDelegate connectionType:(ConnectionType) type {
delegate = webServiceDelegate;
connectionType = type;
isCanceled = NO;
NSLog(#"->: %i, %#", connectionType, self);
return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
switch (connectionType) {
case GetAllAlerts:
result = [WebserviceUtility getJsonFromData:data];
break;
case GetServerAlerts:
result = [WebserviceUtility getJsonFromData:data];
break;
case GetServers:
result = [WebserviceUtility getJsonFromData:data];
break;
default:
result = nil;
break;
}
}
- (void)displayErrorAlert {
UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:#"Fout" message:#"Verbinding met webservice niet mogelijk" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[errorMessage show];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if(!isCanceled) {
#try {
[delegate connection:connection ofType:connectionType didFinishWithError: [NSDictionary dictionaryWithObject:#"error" forKey:#"WebserverConnectionFailed"]];
}
#catch (NSException *e) {}
#finally {}
[self displayErrorAlert];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"<-: %i, %#", connectionType, self);
if(!isCanceled) {
[delegate connection:connection ofType:connectionType didFinishWithResult:result];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLCredential *credential = [WebserviceUtility getCredentials];
if ([challenge previousFailureCount] == 0) {
[[challenge sender] useCredential:credential
forAuthenticationChallenge:challenge];
}
else {
[delegate connection:connection ofType:connectionType didFinishWithError: [NSDictionary dictionaryWithObject:#"error" forKey:#"WebserverConnectionFailed"]];
[self displayErrorAlert];
}
}
- (void)delegateDidDealloc {
NSLog(#"!!: %i, %#", connectionType, self);
isCanceled = YES;
}
#end
Used like this:
- (void) getAllAlerts {
NSURLRequest *request = [WebserviceUtility getRequestForPath:#"/dashboard/DashboardAppleConnector.asmx/GetActiveAlerts"];
webserviceConnection = [[WebserviceConnection alloc] initWithDelegate:self connectionType:GetAllAlerts];
connection = [[NSURLConnection alloc] initWithRequest:request delegate: webserviceConnection];
}
When another ViewController with its own webserviceConnection instance uses its instance (similar to getAllAlerts) all goed pearshaped!
Any thoughts anyone?
Regards,
Bert

It looks like the problem is happening because of the way you are declaring your variables like connectionType. If you want them to be declared as instance variables, you should be putting them in the interface declaration:
#interface WebServiceConnection {
BOOL isCanceled;
NSDictionary *result;
ConnectionType connectionType;
id <WebserviceConnectionDelegate> delegate;
}
#end
By declaring them in the #implementation block you are actually creating global variables, not instance variables.
See this SO post for more information

The definition block:
BOOL isCanceled;
NSDictionary *result;
ConnectionType connectionType;
id <WebserviceConnectionDelegate> delegate;
Declares those four things to be global variables, exactly as if they weren't in the #implementation block. Simply putting things inside #implementation doesn't make them local to the object — it just explains which object all the subsequent method implementations belong to.
If you don't mind putting implementation specifics into your header files, you could move them into the #interface declaration, e.g.
#interface WebserviceConnection
{
BOOL isCanceled;
NSDictionary *result;
ConnectionType connectionType;
id <WebserviceConnectionDelegate> delegate;
}
// etc
#end
You can keep them purely internal to the implementation at the cost of some repetitive syntax by adding them to your class through a category, e.g.
#import "WebserviceConnection.h"
#import "WebserviceUtility.h"
#interface WebserviceConnection() // a category to add additional properties
#property (nonatomic, assign) BOOL isCanceled;
#property (nonatomic, retain) NSDictionary *result;
#property (nonatomic, assign) ConnectionType connectionType;
#property (nonatomic, assign) id <WebserviceConnectionDelegate> delegate;
#end
#implementation WebserviceConnection
// synthesising the properties also adds the named properties as instance variables
#synthesize isCanceled;
#synthesize result;
#synthesize connectionType;
#synthesize delegate;
- (id)initWithDelegate:(id)webServiceDelegate ... etc...
Aside: a method called getJsonFromData: should return a non-owning reference according to Cocoa naming conventions since it doesn't contain 'new', 'alloc', 'retain' or 'create'. Which, if you were to obey, would leave you with a dangling pointer in result in the code as presented.

Related

Delegate Methods not called in Class Instance

I'm creating an instance of a class called S3ObjectController (S3OC) that has one method and four delegate methods. I create an instance of my S3OC, call an instance method from the S3OC Class (which I know fires from NSLog statements) but none of the associated delegate methods are called within the S3OC class. I have the delegate set to self in the method and the delegate declared properly in the .h header.
Thoughts? just to be clear, it's the (void)request methods in the .m file below I'm thinking should be called that aren't. I'm getting EXC BAD ACCESS errors. Is self getting released by ARC?
The entire .m file of the S3OC class is below:
#import "S3ObjectController.h"
#implementation S3ObjectController
#synthesize string;
#synthesize s3GOR, s3Client;
-(void)method
{
NSLog(#"Method Called");
s3Client = [[AmazonS3Client alloc] initWithAccessKey:ACCESS_KEY_ID withSecretKey:SECRET_KEY];
s3GOR = [[S3GetObjectRequest alloc]initWithKey:string withBucket:[Constants pictureBucket]];
[s3GOR setDelegate:self];
[s3Client getObject:s3GOR];
NSLog(#"Method Finished");
}
-(void)request:(AmazonServiceRequest *)request didFailWithError:(NSError *)error
{
NSLog(#"Error %#",error);
}
-(void)request:(AmazonServiceRequest *)request didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"Response Key %#", response);
}
-(void)request:(AmazonServiceRequest *)request didReceiveData:(NSData *)data
{
NSLog(#"ObjectRequestKey = %#",request);
}
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
NSLog(#"Final Delegate Method");
}
Here's the header:"
#interface S3ObjectController : NSObject <AmazonServiceRequestDelegate>{
NSMutableData *responseData;
NSString *string;
AmazonS3Client *s3Client;
S3GetObjectRequest *s3GOR;
}
-(void)method;
#property (nonatomic, strong) NSString *string;
#property (nonatomic, strong) S3GetObjectRequest *s3GOR;
#property (nonatomic, strong) AmazonS3Client *s3Client;
#end
Finally, here's how I call the method in another class:
for (NSString *name in nameArray){
#try {
S3ObjectController *localS3 = [[S3ObjectController alloc]init];
localS3.string = name;
[localS3 method];
NSLog(#"called");
}
I think your suspicions about ARC are true. Because delegate properties are usually weak references, they aren't enough to keep the object from being released.
Make an NSArray that's an iVar and add the S3ObjectController to it. If the delegates still don't fire, you know it's something else...
Edit:
so declare an NSMutableArray in the header of the class that contains your for loop, initialize it somewhere like this:
myArray = [NSMutableArray arrayWithCapacity:0];
then use it like this:
for (NSString *name in nameArray){
#try {
S3ObjectController *localS3 = [[S3ObjectController alloc]init];
localS3.string = name;
[localS3 method];
[myArray addObject:localS3];
NSLog(#"called");
}
}

Memory management of container classes

I've made a container class to store a single tweet. Its initialized by passing in a dictionary object which is a single tweet.
I then store an array of these 'tweets' which I process through to display in a table.
The project is now finished and I am reviewing everything at the moment and I was wondering is there a better way to do this in the future. Is the memory handled correctly. I declare the string member vars with 'copy' and later in the dealloc I use a 'release' rather than just setting them to 'nil'.
Is my init ok or could that be improved?
Tweet.h
#import
#interface Tweet : NSObject
{
NSString * _userName;
NSString * _tweetText;
NSString * _tweetURL;
}
#property (nonatomic, copy) NSString * userName;
#property (nonatomic, copy) NSString * tweetText;
#property (nonatomic, copy) NSString * tweetURL;
- (id) initWithDict:(NSDictionary *)productsDictionary;
#end
Tweet.m
#implementation Tweet
#synthesize userName = _userName;
#synthesize tweetText = _tweetText;
#synthesize tweetURL = _tweetURL;
- (id) initWithDict:(NSDictionary *)productsDictionary
{
NSDictionary *aDict = [productsDictionary objectForKey:#"user"];
self.userName = [aDict objectForKey:#"screen_name"];
self.tweetText = [productsDictionary objectForKey:#"text"];
NSRange match;
match = [self.tweetText rangeOfString: #"http://"];
if (match.location != NSNotFound)
{
NSString *substring = [self.tweetText substringFromIndex:match.location];
NSRange match2 = [substring rangeOfString: #" "];
if (match2.location == NSNotFound)
{
self.tweetURL = substring;
}
else
{
self.tweetURL = [substring substringToIndex:match2.location];
}
}
else
{
self.tweetURL = nil;
}
return self;
}
-(void) dealloc
{
[self.tweetText release];
[self.tweetURL release];
[self.userName release];
[super dealloc];
}
#end
Many Thanks,
Code
At first sight, I see no inherent flaws here. That looks fine. I would prefer to do:
-(void) dealloc
{
[_tweetText release];
[_tweetURL release];
[_userName release];
[super dealloc];
}
But what you do is good as well.

Objective C Singleton class "defined but not used" warning!

I'm using a Singleton class and following is the code:
.h File: 
#import <Foundation/Foundation.h>
#interface Credential : NSObject {
NSString *UID;
NSString *UPASS;
}
#property(nonatomic,retain) NSString *UID;
#property(nonatomic,retain) NSString *UPASS;
static Credential *credential = NULL;
+(Credential*) sharedInstance;
/*
+ #property(nonatomic,retain) NSString *UID;
+ #property(nonatomic,retain) NSString *UPASS;
*/
#end
.m file:
#import "Credential.h"
#implementation Credential
#synthesize UID,UPASS;
-(void) dealloc{
[UID release];
[UPASS release];
[super dealloc];
}
+(Credential*) sharedInstance
{
#synchronized(self)
{
if (credential == NULL) {
credential = [[Credential alloc] init];
}
}
return credential;
}
#end
The following line produces warning "defined but not used"
static Credential *credential = NULL;
I couldn't figure out that I've been using credential variable in .m file under "sharedInstance" function then why am I getting this warning?
A strange issue to me!
Does the problem go away when you move the static variable to the top of the implementation (.m) file? And on a related note, I think that you would benefit from getting rid of the singleton altogether.

invalid CFArrayRef problem with Singleton object

I've built a singleton object to manage some data in my app
#interface MyCommon : NSObject {
NSArray *quizz;
int iCurrentQuestion;
};
+ (MyCommon *)singleton;
#property (retain) NSArray *quizz;
#property (assign) int iCurrentQuestion;
#end
MyCommon.m
#import "MyCommon.h"
// MyCommon.m:
#implementation MyCommon
static MyCommon * MyCommon_Singleton = nil;
#synthesize iCurrentQuestion;
+ (MyCommon *)singleton
{
if (nil == MyCommon_Singleton)
{
MyCommon_Singleton = [[MyCommon alloc] init];
NSLog(#"allocating MyCommon_Singleton at %#",MyCommon_Singleton);
}
else {
NSLog(#"accessing singleton : %#", MyCommon_Singleton);
}
return MyCommon_Singleton;
}
- (NSArray*) getQuizz{
return quizz;
}
- (void) setQuizz:(NSArray *)array {
quizz = [NSArray arrayWithArray:array];
NSLog(#"setQuizz : %#",quizz);
}
There is no problem for writing the quizz object (setQuizz), however when I try to access it for reading, I get a crash : the quizz looks invalid and Xcode notify me an invalid CFArrayRef
I don't know what's wrong with my code.
You provide a custom setter for quizz but it doesn't comply with how the property is declared.
You're not retaining quizz when you're setting a new value. It's likely to be released just after, leading to a crash when you access it.
You should write
- (void)setQuizz:(NSArray *)array {
if (quizz != array) {
NSArray *tmp = quizz;
quizz = [array retain]; // retain the new value
[tmp release]; // release the old one
}
NSLog(#"setQuizz : %#",quizz);
}
this is way more code than it needs to be. First if you are going to be providing your own method you should declare so in the #property declaration which you didn't. Also your not properly retaining your variables. Additionally you should be using dispatch_once() for a thread safe & fast way to guarantee the singleton is only created once.
#interface MyCommon : NSObject {}
#property(nonatomic, retain) NSArray *quiz;
#property (assign) int iCurrentQuestion;
+ (MyCommon *)singleton;
#end
#implementation MyCommon
#synthesize quiz;
#synthesize iCurrentQuestion;
-(id)init {
self = [super init];
if(self) {
quiz = [[NSMutableArray alloc init];
iCurrentQuestion = 0;
}
return self;
}
+ (MyCommon *)singleton {
static MyCommon *singleton = nil;
static dispatch_once_t pred;
dispatch_once(&pred, ^{
singleton = [[MyCommon alloc] init];
});
return singleton;
}
#end
then you just do
[MyCommon singleton].quiz = //some array

how do you make a "concurrent queue safe" lazy loader (singleton manager) in objective-c

I made this class that turns any object into a singleton, but I know that it's not "concurrent queue safe." Could someone please explain to me how to do this, or better yet, show me the code. To be clear I want to know how to use this with operation queues and dispatch queues (NSOperationQueue and Grand Central Dispatch) on iOS.
Thanks in advance,
Rich
EDIT: I had an idea for how to do it. If someone could confirm it for me I'll do it and post the code. The idea is that proxies make queues all on their own. So if I make a mutable proxy (like Apple does in key-value coding/observing) for any object that it's supposed to return, and always return the same proxy for the same object/identifier pair (using the same kind of lazy loading technique as I used to create the singletons), the proxies would automatically queue up the any messages to the singletons, and make it totally thread safe.
IMHO this seems like a lot of work to do, so I don't want to do it if it's not gonna work, or if it's gonna slow my apps down to a crawl.
Here's my non-thread safe code:
RMSingletonCollector.h
//
// RMSingletonCollector.h
// RMSingletonCollector
//
// Created by Rich Meade-Miller on 2/11/11.
// Copyright 2011 Rich Meade-Miller. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "RMWeakObjectRef.h"
struct RMInitializerData {
// The method may take one argument.
// required
SEL designatedInitializer;
// data to pass to the initializer or nil.
id data;
};
typedef struct RMInitializerData RMInitializerData;
RMInitializerData RMInitializerDataMake(SEL initializer, id data);
#interface NSObject (SingletonCollector)
// Returns the selector and data to pass to it (if the selector takes an argument) for use when initializing the singleton.
// If you override this DO NOT call super.
+ (RMInitializerData)designatedInitializerForIdentifier:(NSString *)identifier;
#end
#interface RMSingletonCollector : NSObject {
}
+ (id)collectionObjectForType:(NSString *)className identifier:(NSString *)identifier;
+ (id<RMWeakObjectReference>)referenceForObjectOfType:(NSString *)className identifier:(NSString *)identifier;
+ (void)destroyCollection;
+ (void)destroyCollectionObjectForType:(NSString *)className identifier:(NSString *)identifier;
#end
// ==--==--==--==--==Notifications==--==--==--==--==
extern NSString *const willDestroySingletonCollection;
extern NSString *const willDestroySingletonCollectionObject;
RMSingletonCollector.m
//
// RMSingletonCollector.m
// RMSingletonCollector
//
// Created by Rich Meade-Miller on 2/11/11.
// Copyright 2011 Rich Meade-Miller. All rights reserved.
//
#import "RMSingletonCollector.h"
#import <objc/objc-runtime.h>
NSString *const willDestroySingletonCollection = #"willDestroySingletonCollection";
NSString *const willDestroySingletonCollectionObject = #"willDestroySingletonCollectionObject";
RMInitializerData RMInitializerDataMake(SEL initializer, id data) {
RMInitializerData newData;
newData.designatedInitializer = initializer;
newData.data = data;
return newData;
}
#implementation NSObject (SingletonCollector)
+ (RMInitializerData)designatedInitializerForIdentifier:(NSString *)identifier {
return RMInitializerDataMake(#selector(init), nil);
}
#end
#interface RMSingletonCollector ()
+ (NSMutableDictionary *)singletonCollection;
+ (void)setSingletonCollection:(NSMutableDictionary *)newSingletonCollection;
#end
#implementation RMSingletonCollector
static NSMutableDictionary *singletonCollection = nil;
+ (NSMutableDictionary *)singletonCollection {
if (singletonCollection != nil) {
return singletonCollection;
}
NSMutableDictionary *collection = [[NSMutableDictionary alloc] initWithCapacity:1];
[self setSingletonCollection:collection];
[collection release];
return singletonCollection;
}
+ (void)setSingletonCollection:(NSMutableDictionary *)newSingletonCollection {
if (newSingletonCollection != singletonCollection) {
[singletonCollection release];
singletonCollection = [newSingletonCollection retain];
}
}
+ (id)collectionObjectForType:(NSString *)className identifier:(NSString *)identifier {
id obj;
NSString *key;
if (identifier) {
key = [className stringByAppendingFormat:#".%#", identifier];
}
else {
key = className;
}
if (obj = [[self singletonCollection] objectForKey:key]) {
return obj;
}
// dynamic creation.
// get a class for
Class classForName = NSClassFromString(className);
if (classForName) {
obj = objc_msgSend(classForName, #selector(alloc));
// if the initializer takes an argument...
RMInitializerData initializerData = [classForName designatedInitializerForIdentifier:identifier];
if (initializerData.data) {
// pass it.
obj = objc_msgSend(obj, initializerData.designatedInitializer, initializerData.data);
}
else {
obj = objc_msgSend(obj, initializerData.designatedInitializer);
}
[singletonCollection setObject:obj forKey:key];
[obj release];
}
else {
// raise an exception if there is no class for the specified name.
NSException *exception = [NSException exceptionWithName:#"com.RMDev.RMSingletonCollector.failed_to_find_class" reason:[NSString stringWithFormat:#"SingletonCollector couldn't find class for name: %#", [className description]] userInfo:nil];
[exception raise];
[exception release];
}
return obj;
}
+ (id<RMWeakObjectReference>)referenceForObjectOfType:(NSString *)className identifier:(NSString *)identifier {
id obj = [self collectionObjectForType:className identifier:identifier];
RMWeakObjectRef *objectRef = [[RMWeakObjectRef alloc] initWithObject:obj identifier:identifier];
return [objectRef autorelease];
}
+ (void)destroyCollection {
NSDictionary *userInfo = [singletonCollection copy];
[[NSNotificationCenter defaultCenter] postNotificationName:willDestroySingletonCollection object:self userInfo:userInfo];
[userInfo release];
// release the collection and set it to nil.
[self setSingletonCollection:nil];
}
+ (void)destroyCollectionObjectForType:(NSString *)className identifier:(NSString *)identifier {
NSString *key;
if (identifier) {
key = [className stringByAppendingFormat:#".%#", identifier];
}
else {
key = className;
}
[[NSNotificationCenter defaultCenter] postNotificationName:willDestroySingletonCollectionObject object:[singletonCollection objectForKey:key] userInfo:nil];
[singletonCollection removeObjectForKey:key];
}
#end
RMWeakObjectRef.h
//
// RMWeakObjectRef.h
// RMSingletonCollector
//
// Created by Rich Meade-Miller on 2/12/11.
// Copyright 2011 Rich Meade-Miller. All rights reserved.
//
// In order to offset the performance loss from always having to search the dictionary, I made a retainable, weak object reference class.
#import <Foundation/Foundation.h>
#protocol RMWeakObjectReference <NSObject>
#property (nonatomic, assign, readonly) id objectRef;
#property (nonatomic, retain, readonly) NSString *className;
#property (nonatomic, retain, readonly) NSString *objectIdentifier;
#end
#interface RMWeakObjectRef : NSObject <RMWeakObjectReference>
{
id objectRef;
NSString *className;
NSString *objectIdentifier;
}
- (RMWeakObjectRef *)initWithObject:(id)object identifier:(NSString *)identifier;
- (void)objectWillBeDestroyed:(NSNotification *)notification;
#end
RMWeakObjectRef.m
//
// RMWeakObjectRef.m
// RMSingletonCollector
//
// Created by Rich Meade-Miller on 2/12/11.
// Copyright 2011 Rich Meade-Miller. All rights reserved.
//
#import "RMWeakObjectRef.h"
#import "RMSingletonCollector.h"
#implementation RMWeakObjectRef
#dynamic objectRef;
#synthesize className, objectIdentifier;
- (RMWeakObjectRef *)initWithObject:(id)object identifier:(NSString *)identifier {
if (self = [super init]) {
NSString *classNameForObject = NSStringFromClass([object class]);
className = classNameForObject;
objectIdentifier = identifier;
objectRef = object;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(objectWillBeDestroyed:) name:willDestroySingletonCollectionObject object:object];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(objectWillBeDestroyed:) name:willDestroySingletonCollection object:[RMSingletonCollector class]];
}
return self;
}
- (id)objectRef {
if (objectRef) {
return objectRef;
}
objectRef = [RMSingletonCollector collectionObjectForType:className identifier:objectIdentifier];
return objectRef;
}
- (void)objectWillBeDestroyed:(NSNotification *)notification {
objectRef = nil;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[className release];
[super dealloc];
}
#end
The easiest (and thread safe) acces to singleton:
static SomeClass* shared_instance;
+(SomeClass*) sharedInstance {
#synchronized( shared_instance ) {
if( !shared_instance ) {
shared_instance = [[SomeClass alloc] init];
//some additional initialization here
}
}
return shared_instance;
}
Edit:
I think particular singleton class would be your best choice. Even for lazy loading. E.g. you need to access some array of users all over your app. Then you can create singleton class:
UserManager:
+(UserManager*) sharedManager;
-(NSArray*) allUsers;
-(NSArray*) recentUsers;
-(NSArray*) featuredUsers;
-(void) addUser:(User*) user;
-(void) removeUser:(User*) user;
etc...
Then you will be able to access that arrays in every view controller.
You should create singleton for other types.