Я создал простое приложение, используя класс AsynUDPSocket. Он слушает простые текстовые сообщения от UDP-клиента.
Когда я помещаю соответствующий код в контроллер представления, все работает нормально. Но когда я пытаюсь сделать то же самое в приложении делегат, мое приложение вылетает. Эта штука отлично работала в приложении-делегате несколько дней назад. Теперь я не знаю, что случилось.
Я использовал код из этого поста
Кто-нибудь может уточнить, пожалуйста?
РЕДАКТИРОВАТЬ: код
Содержимое моего делегата приложения .h 1011 *
//
// 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
Содержимое файла .m моего делегата приложения
//
// 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
РЕДАКТИРОВАТЬ: журнал аварий
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)
Спасибо,
Angelo.