loadNibNamed:owner:options crashing app after conversion to ARC - objective-c

I'm having what I'm sure is a stupid issue after converting my project to ARC. This method has started crashing on the first line:
- (MyView*) loadMyView
{
NSArray* elements = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:nil options:nil];
return (MyView*)[elements objectAtIndex:0];
}
This worked fine before the conversion to ARC. The app crashes in the simulator with the following message:
[CFArray release]: message sent to deallocated instance
Can anybody tell me what I'm doing wrong?

Well, after sleeping on it I decided to just delete the Nib and start over to see if that helped and sure enough, it works now. I don't know what the conversion to ARC did to cause this issue but deleting the Nib and recreating it from scratch solved the problem.

Try changing the owner: parameter from nil to self
NSArray* elements = [[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil];
I'm guessing because you are setting the owner to nil, your array is being created with a retain count of zero, and is therefore being deallocated right away.

I had the same issue. It usually happens if you have inconsistencies in your nib file. In mine I had some buttons that were still connected through IBOutlets to some objects that I deleted in the header file. Hope this helps.

Related

'+entityForName: nil is not a legal NSPersistentStoreCoordinator for searching for entity name

Getting the exception later in the program when _managedObjectModel is being used. Below is initialiser code.
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:self.managedObjectModelName withExtension:#"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
The problem is that _managedObjectModel is pointing to a garbage memory location.
After analysing GPR and assembly code i found that the ManagedObjectModel was being initialised properly but, when the content from schema is being loaded into it - the object pointer starts pointing at random locations way before the object is, or somewhere in between the object's allocated memory.
If i try to access underlying content using something like this : [_managedObjectModel->_entities allKeys], i do get all the keys in schema.
Has anyone ever faced such issues ?
This is probably something similar to https://github.com/typhoon-framework/Typhoon/issues/138
self.managedObjectModelName is same as your data model name?
I guess some where here is problem in names itself.

Trying to replace deprecated loadnibnamed:owner

I'm trying to replace the deprecated
[NSBundle loadNibNamed:#"Subscriptions" owner:self];
with this instead (only thing I can find that's equivalent)
[[NSBundle mainBundle] loadNibNamed:#"Subscriptions" owner:self topLevelObjects:nil];
but the dialog pops up and disappears right away instead of staying open like it was doing with the deprecated call.
This code is inside a viewcontroller like this.
- (id)init{
self = [super init];
if (self) {
//[NSBundle loadNibNamed:#"Subscriptions" owner:self];
[[NSBundle mainBundle] loadNibNamed:#"Subscriptions" owner:self topLevelObjects:nil];
}
return self;
}
and I'm calling it from the appdelegate like this.
SubscriptionsViewController *subscriptionsViewController = [[SubscriptionsViewController alloc] init];
[subscriptionsViewController.window makeKeyAndOrderFront:self];
Is there anything I'm missing? It seems straight forward to me.
The dialog appearing and then disappearing is a sign of possible object collection - with a strong reference to the dialog it will be collected and lost.
The deprecated call retained ownership of the top-level objects in the nib, the new call does not.
Therefore the properties of the owner object that refer to top-level objects must be strong, or you need to keep the top-level objects array. This is contrary to the old recommendation where such properties were weak.
Properties which reference non-top-level objects in the nib can still be weak.
I just had a similar problem when using loadNibNamed: owner: topLevelObjects: and always got an error like
[__NSArrayM insertObject:atIndex:]: object cannot be nil' terminating with uncaught exception of type NSException abort() called
Because my top level objects where nil.
I finally discovered that the nib file I was loading had its Interface Builder version set to "Xcode 4.6". When I set that to 6.2, everything worked fine again.

Unrecongized Selector sent to Instance

Ive been through similar questions on here, but can't seem to relate it to my app!
My problem is when i run the program i get at error message
[UIView setAttString:]: unrecognised selector sent to instance 0x7538c60
Ive debugged the code down to 3 lines in the ViewController class - these are:
NSString *path = [[NSBundle mainBundle] pathForResource:#"g1" ofType:#"txt"];
NSAttributedString* text = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
[(CTView*)self.view setAttString: text];
Im pretty sure its with the final line, but as this line has been lifted from an example app I haven't really grasped the proper understanding of what it does, and therefore can't see anything wrong with it! Any obvious or common errors I could try to resolve? I don't know how much of my code is needed for the clever folk out there to help me out - let me know and I can add more snippets!
Thanks in advance!
In human terms, that snippet is loading a string from your app's resource file, then sending it to the object referenced by self.view by means of the setAttrString: message.
The error you're seeing shows up when an object receives a message that it doesn't recognize. I don't know what a CTView is, but you should ensure that:
The CTView class does actually have a method called setAttrString:
The object referenced by self.view is actually an instance of CTView.
You can verify that latter with the following:
BOOL isCorrectClass = [self.view isMemberOfClass:[CTView class]];
...or just look at it in the debugger at runtime.
Looks like CTView response to setAttString, but your self.view is a UIView instead of CTView. And usually the self.view of a viewController is just a UIView.
If you created the view as an outlet in your IB, make sure you set it to be CTView class.
Or share where did you alloc inited the "self.view" in your last line of code.

Passing a reference and copying the object from another controller. Objects keep disappearing

I know this is a relativly easy question, but I just can't figure out how to solve this problem:
One of my views will receive a dragOperation and the performDragOperation method should pass the NSURL to my AppDelegate which puts it in an mutArray...
The problem is, that I pass over a reference and the object disappears the moment performDragOperation is done. So I tried several things:
in performDragOperation:
//Puts the reference itself in the folderPaths Array
AppDelegate *appDelegate = [NSApp delegate];
[[appDelegate folderPaths] addObject:referenceTotheNSURLObject];
/* tried creating a NSString and putting it in too. Same result because local variables will disappear */
So I created a method in AppDelegate and tried different things:
- (void)addFilePath:(NSURL *)filePath {
NSURL *copyOfURL = [filePath copy];
[[self folderPaths] addObject:copyOfURL];
}
This makes the most sense to me and the only reason I can think about why this doesn't work is, because copyOfURL itself is a pointer and it points to an localObject that will disappear the moment the addFilePath method is finished? But I'm not allowed to use
NSURL copyOfURL = [filePath copy];
I also tried recreating an NSString object again. Same results.
- (void)addFilePath:(NSURL *)filePath {
NSString *pathString = [filePath absoluteString];
[[self folderPaths] addObject:pathString];
}
I know it will be some relatively simply solution but I'm stuck and can't figure it out. Thanks for your time!

Released array EXC_ACCESS_ERROR and Cocos2D

I recently posted a question here about some memory issues I was having. I've got that fixed now thanks to this wonderful community but I'm facing another problem. I'm using Cocos2d to develop a game and I'm trying to remove a Sprite from and array. The problem arises when I try and release the temporary array I'm using to keep track of the sprites to remove.
NSMutableArray *spritesToRemove = [[NSMutableArray alloc] init];
// Loop through all sprites
for(CSSprite *sprite in _sprites){
if(sprite.toRemove){
[spritesToRemove addObject: sprite];
}
}
// loop through sprites to be removed
for(CSSprite *removeableSprite in spritesToRemove){
[_sprites removeObject: removeableSprite];
// Cocos2d code to remove a sprite
[self removeChild: removeableSprite cleanup: YES];
}
[spritesToRemove release]; // EXC_BAD_ACCESS error
I get a feeling the reason I'm getting the error is because I'm releasing the sprite object in [self removeChild: removeableSprite cleanup: YES]; before actually releasing the array. It all works fine if I remove the line [spritesToRemove release] but I obviously then have a memory leak on my hands.
I've tried moving the removal of the sprites around and I can get the memory thing sorted by completely omitting the line [self removeChild: removeableSprite cleanup: YES]; but then Cocos2d throws the same EXC_BAD_ACCESS error from within CCNode at [child visit]; of -(void) visit
Thanks again for your help :-)
EDIT: I enabled NSZombie and I got this message:
*** -[Sprite release]: message sent to deallocated instance 0xfa94cf0
Which to me kind of suggests my initial thought, somewhere an entry in the array is being released to soon. Would that be correct? If so is there anyway for me to find out where?
I managed to find the issue (and to be honest I feel a little silly :-P ). I was releasing the sprite manually once I added it to the array, which wasn't in the code provided so you guys couldn't have found it. The sprite was already set up to be autoreleased and thus was being cleared twice - causing my error.