message sent to deallocated instance 0x141dafb0 iPhone SDK - iphone

i am getting this error "message sent to deallocated instance 0x141dafb0" its comming from a UIBarButtonItem when its beeing pressed on the application. any help would be greatly appreciated
Error:
*** -[PeerConnection performSelector:withObject:withObject:]: message sent to deallocated instance 0x14143ff0
PeerConnection.h
#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>
#interface PeerConnection : NSObject <GKPeerPickerControllerDelegate, GKSessionDelegate> {
UIBarButtonItem *StartConnection;
}
- (IBAction) StartConnectionAction;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *StartConnection;
#end
PeerConnection.m
#import "PeerConnection.h"
#implementation PeerConnection
#synthesize StartConnection;
- (IBAction) StartConnectionAction {
NSLog(#"Start Connection to the other IPhones");
[StartConnection release];
}
- (void)dealloc {
[super dealloc];
}
#end
i have enabled Zombie and that is all its giving to me

Don't release your StartConnection button until -dealloc. Releasing that bar button item in -StartConnectionAction is your problem--anything the UI tries to do with it after that will call a zombie.

In your case, you have released the StartConnection object. Now, when the automatic dealloc is called, the reference was not found (as already removed) and hence you got the error.

I had the same error, but was using a singleton with autorelease on shared method, took off autorelease from there and added on it's dealloc , and all works fine now.

Old Thread; But thought I might add.
If your app isn't using ARC; Use The Analyse Feature to find all the problems that may arise due to releasing/retaining objects.
Shortcut is command + shift + B
Totally useful !

This may be due to access of the instance that is already removed during GC. The error occurs in a case when you use autorelease.
ThePlannerAppDelegate *delg = [(ThePlannerAppDelegate *)[[UIApplication sharedApplication] delegate] autorelease];
Now this is most likely the GC will destroy the reference although delg points to the main window delegate.
My point is use autorelease safely.
Important: The error will occur when a message will be sent to an dead reference.

Related

Calling method in object gives SIGBART

Simple question, but this gives me an error and I can't seem to resolve it.
In my object (UIViewController) I have declared a method in the .h
-(void)setCurrentLoc:(CLLocation *)loc;
and in the .m
-(void)setCurrentLoc:(CLLocation *)loc
{
currentLocation = loc;
}
All good. But this is how I initiate this object (which works fine) but when I call setCurrentLoc: it gives SIGBART
.h
IBOutlet VectorViewController *vectorView;
.m
vectorView = [[VectorViewController alloc] init];
...
//In another method I call:
[vectorView setCurrentLoc:location]; // Error/SIGBART line
Is there something I missed? Maybe not making it global or something?
Error:
-[UIView setCurrentLoc:]: unrecognized selector sent to instance 0x841cab0
-[UIView setCurrentLoc:]: unrecognized selector sent to instance 0x841cab0
This means that you are sending the message to a pointer that is no longer pointing at your VectorViewController, but is now pointing at a UIView. This usually happens when the object has been autoreleased or released and the memory re-used.
Check carefully what happens between when you create vectorView and when you call it again in your other method. You seem to be setting and accessing the instance variable directly - you should probably be accessing it via properties (hard to be specific without knowing if you are using ARC or not) and making sure those properties are declared appropriately.
Based on the code you've written, you don't need to provide a setCurrentLoc method, use a property instead and synthesize instead.
in your VectorViewController class, use:
#property (nonatomic, retain) IBOutlet CLLocation * currentLocation;
in your #implementation, add:
#synthesize currentLocation;
and in your dealloc method, add:
self.currentLocation = nil;
Leave your existing setCurrentLocation call as is.

objective-c/iphone sdk newcomer question

At present I'm building a basic application to learn Objective-C and the iPhone SDK.
I'm creating NSObject with getters and setters to get to grips with how these works. I've successfully added a property and getters and setters to my main controller, currently I'm trying to create a separate class which I can create a new instance of within my controller but it crashes when I try and use the setter.
Thank you in advance for your time, sorry if this question is as stupid as I'm sure it is.
Here's the header for my class
QuizQuestion.h
#import <Foundation/Foundation.h>
#interface QuizQuestion : NSObject {
NSString *question;
}
#property (retain) NSString* question;
#end
QuizQuestion.m
#import "QuizQuestion.h"
#implementation QuizQuestion
#synthesize question;
- (void) dealloc
{
[question release];
[super dealloc];
}
#end
And here is my controller code (i've cut some out)
#implementation Quiz2ViewController
#class QuizQuestion; // Is this correct?
- (void)viewDidLoad {
QuizQuestion *aQuestion;
//gets here fine, but crashes (the app closes) when I set question.
[aQuestion setQuestion:#"hello world"];
[super viewDidLoad];
}
#end
As well as #class I tried import "QuizQuestion.h" and I get the same issue.
You're not actually allocating an instance of the QuizQuestion class—your aQuestion variable isn’t pointing to anything in particular, so trying to send it a message, like -setQuestion:, is sending that message to... well, there’s no telling where, and sending things messages that aren’t meant for them is a surefire way to crash your app. What you need to do is this:
QuizQuestion *aQuestion = [[QuizQuestion alloc] init];
[aQuestion setQuestion:#"hello world"];
You also need to call [aQuestion release] at some point, or you’ll leak the memory associated with it.
You need to allocate space and initialize the QuizQuestion.
QuizQuestion *aQuestion = [[QuizQuestion alloc] init];
before setting the question.
I think you need to read up on some documentation before you try any more coding. Apple has several intro programming guides that are very good. The problem you are having is addressed in this section:
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAllocInit.html
QuizQuestion *aQuestion = [[QuizQuestion alloc] init];
[aQuestion setQuestion:#"hello world"];
[aQuestion release];

ios + use asynudpsocket from the app delegate

I have created a simple app using the AsynUDPSocket class. It listens to simple text messages from a UDP client.
When I put the relevant code into a view controller, everything works fine. But when I try the same in the app delegate my app crashes. This thing worked fine in the app delegate a few days back. Now I dont know what's wrong.
I used the code from this post
.Can someone please elaborate?
EDIT: Code
My App Delegate's .h file contents
//
// MacSocketTestAppDelegate.h
// MacSocketTest
//
// Created by core on 04/05/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "AsyncUdpSocket.h"
#import "SocketController.h"
#import "NextController.h"
#class SocketController;
#class SocketTest;
#interface MacSocketTestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
SocketTest *connection;
AsyncUdpSocket *aSyncSocket;
UIViewController *currentViewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet SocketController *mySC;
#property (nonatomic, retain) SocketTest *connection;
#property (nonatomic, retain) AsyncUdpSocket *aSyncSocket;
#property (nonatomic, retain) UIViewController *currentViewController;
#end
My App Delegate's .m file contents
//
// MacSocketTestAppDelegate.m
// MacSocketTest
//
// Created by core on 04/05/11.
// Copyright __MyCompanyName__ 2011. All rights reserved.
//
#import "MacSocketTestAppDelegate.h"
#import "SocketController.h"
#import "SocketTest.h"
#import "NextController.h"
#implementation MacSocketTestAppDelegate
#synthesize window;
#synthesize mySC;
#synthesize connection;
#synthesize aSyncSocket;
#synthesize currentViewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
aSyncSocket=[[AsyncUdpSocket alloc] initWithDelegate:self]; //We are the delegate for the asynchronous socket object.
[aSyncSocket bindToPort:30000 error:nil]; //We want to listen on port 30000. Don't care about errors for now.
[aSyncSocket receiveWithTimeout:-1 tag:1]; //Start listening for a UDP packet.
SocketTest *instanceST = [SocketTest alloc];
[self setConnection:instanceST];
SocketController *instanceSocketController = [[SocketController alloc] initWithNibName: #"SocketController" bundle: nil];
[self setMySC:instanceSocketController];
[instanceSocketController release];
[[self window] setRootViewController:[self mySC]];
[window makeKeyAndVisible];
return YES;
}
//Other methods hidden
#pragma mark AsyncUdpSocket Delegate Method
//This method is called by the AsyncUdpSocket object when a packet is received:
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
NSString *theLine=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; //Convert the UDP data to an NSString
NSLog(#"%#", theLine);
[theLine release];
[aSyncSocket receiveWithTimeout:-1 tag:1]; //Listen for the next UDP packet to arrive...which will call this method again in turn.
return YES; //Signal that we didn't ignore the packet.
}
- (void)dealloc {
[window release];
[super dealloc];
}
#end
EDIT: Crash log
The Debugger has exited with status 0.
[Session started at 2011-05-05 15:29:18 +0530.]
GNU gdb 6.3.50-20050815 (Apple version gdb-1469) (Wed May 5 04:36:56 UTC 2010)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 1706.
Pending breakpoint 1 - ""MacSocketTestAppDelegate.m":41" resolved
MacSocketTest(1706) malloc: recording malloc stacks to disk using standard recorder
MacSocketTest(1706) malloc: stack logging compaction turned off; size of log files on disk can increase rapidly
MacSocketTest(1706) malloc: process 1547 no longer exists, stack logs deleted from /tmp/stack-logs.1547.MacSocketTest.bEuUBJ.index
MacSocketTest(1706) malloc: stack logs being written into /tmp/stack-logs.1706.MacSocketTest.SJLaue.index
(gdb) continue
Current language: auto; currently objective-c
2011-05-05 15:29:46.698 MacSocketTest[1706:207] *** -[MacSocketTestAppDelegate respondsToSelector:]: message sent to deallocated instance 0x5954e00
(gdb)
Thanks,
Angelo.
Well, with the information you're giving us I would guess you probably messed up the memory management. I guess so because that's the most common cause for crashes.
I used the code from this post.
What post? There's no code in this post.
In order to get a decent answer that's not just based on statistical reasons for crashes you should provide code, crash log and stack trace.
EDIT: So you edited in the link. So now we have some code. However that code is out of context and you've already told us that it works, so we're not going to find the answer there.
EDIT 2:
2011-05-05 15:29:46.698 MacSocketTest[1706:207] *** -[MacSocketTestAppDelegate respondsToSelector:]: message sent to deallocated instance 0x5954e00
So I was right. It was a memory issue. You're sending a message to a deallocated object. What's weird is that it seems to be your app delegate that has been deallocated.
Erik, your comments were spot on. I have figured out my problem's location.
Let me explain:
Like I commmented, I'm moving from the app delegate to one screen, and from that screen to another one.
In essence, in the last screen's (view's) view controller, I was creating an instance of my app delegate to set the last screen's view controller as my currentViewController property of my app delegate. I'm maintaining it for different reasons.
I was, also, DEALLOCATING, the instance of the app delegate right after I was done setting my currentViewController property. Once I removed that line, I was good and my app was up and running.
I think I should deallocate it in the dealloc method. Please correct my understanding, if I'm wrong.
Thanks a million to you, Erik, for pointing me in the right direction.
EDIT: Erik, please tell me how you knew from the crash logs, that it was my app delegate that was being deallocated it.
Angelo.

IPHONE UIIMAGEVIEW can't release?

guys.i'm new to iphone.
i encounter a problem.the uiimageview can't release...here is the code.
#import <UIKit/UIKit.h>
#interface imageNavView : UIImageView {
int index;
}
#property int index;
#end
#import "imageNavView.h"
#implementation imageNavView
#synthesize index;
- (id)initWithFrame:(CGRect)frameRect{
self = [super initWithFrame:frameRect];
if (self) {
// Custom initialization.
self.userInteractionEnabled=YES;
self.multipleTouchEnabled=YES;
self.opaque=YES;
self.autoresizingMask=(UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleWidth|
UIViewAutoresizingFlexibleRightMargin
|UIViewAutoresizingFlexibleLeftMargin
|UIViewAutoresizingFlexibleTopMargin
|UIViewAutoresizingFlexibleBottomMargin);
index=0;
}
return self;
}
- (void)dealloc {
NSLog(#"before image dealloc %i",[self retainCount]);-------- log is 1
[super dealloc];
NSLog(#"after image dealloc %i",[self retainCount]);-------- log is 1,why itn't bad access?
}
#end
why does the second log NSLog(#"after image dealloc %i",[self retainCount]); is 1,not a bad access,so the imageview is not released.
i can't understand,any possible way can make this happen?...any advice will be gratefull.thanks in advance!
dealloc is a call-back method while system releases an object.
The following description is a api doc of dealloc,
Instead, an object’s dealloc method is invoked indirectly through the release NSObject protocol method (if the release message results in the receiver's retain count becoming 0).
This is an excellent example of exactly why you should never call retainCount. The absolute retain count of an object is an implementation detail and it will often be of a value that makes no sense.
The behavior of calling a method after something is deallocated is entirely undefined. In this case, it may crash, it may not. If you turned on zombie detection, it would definitely crash.
Since there is absolutely no way that retainCount could ever return 0, there is no reason for object deallocation to decrement the retain count to 0.

Unrecognized selector sent to instance" problem

my code broke somewhere along the way, and crashes when using the navigation bar buttons.
Error message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView newMemoViewController:didAddMemo:]: unrecognized selector sent to instance 0x5b55a60'
When debugging, the program does run the cancel method, and throws an exception at the #synthesize line. However, I cannot see anything wrong with it.
The symptoms are identical, so I am including the relevant code only for the Cancel button:
NewMemoViewController.h
#import <UIKit/UIKit.h>
#protocol NewMemoDelegate;
#class AKVoiceMemo;
#interface NewMemoViewController : UIViewController {
#private
AKVoiceMemo *voiceMemo;
id <NewMemoDelegate> delegate;
}
#property (nonatomic, retain) AKVoiceMemo *voiceMemo;
#property (nonatomic, assign) id <NewMemoDelegate> delegate;
#end
#protocol NewMemoDelegate <NSObject>
- (void)newMemoViewController:(NewMemoViewController *)newMemoViewController didAddMemo:(AKVoiceMemo *)voiceMemo;
#end
NewMemoViewController.m
#import "NewMemoViewController.h"
#synthesize delegate;
- (void)viewDidLoad {
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButtonItem;
[cancelButtonItem release];
}
- (void)cancel {
[self.delegate newMemoViewController:self didAddMemo:nil];
}
Your help would be appreciated.
Edit: the delegate is the RootViewController:
- (void)newMemoViewController:(NewMemoViewController *)newMemoViewController didAddMemo:(AKVoiceMemo *)voiceMemo {
if (voiceMemo){
// Show the note in a new view controller
// TODO: Implement this
}
[self dismissModalViewControllerAnimated:YES];
}
You're probably setting the delegate of NewMemoViewController to a UIView object instead of an object that implements the NewMemoDelegate protocol.
The error message is telling you that a newMemoViewController:didAddMemo: message was sent to a UIView object and the UIView object didn't know what to do with it. Since your cancel method calls newMemoViewController:didAddMemo: on the delegate, it is the delegate which is the UIView object that doesn't recognize the newMemoViewController:didAddMemo: message. In other words, your delegate is a UIView and it doesn't implement the NewMemoDelegate protocol.
If you are correctly setting the delegate, then #jtbandes makes a great point: The delegate is probably being released and a UIView object is taking over the same memory location, thus "becoming" the delegate by accident. You're doing the right thing by using the assign attribute for your delegate; that's fairly standard Cocoa practice. However, you do need to make sure that the delegate is retained by another object, and that object needs to make sure that the delegate sticks around as long as NewMemoViewController needs it to.
I'm guessing you've over-released the delegate. I notice you have #property (assign) ... delegate;. This means that whenever you set the delegate, that object must be retained by something else as well.
The other possibility is the delegate is actually a UIView, but I'm guessing it's the other case.