我使用 AsynUDPSocket 类创建了一个简单的应用程序。它侦听来自 UDP 客户端的简单文本消息。
当我将相关代码放入视图控制器时,一切正常。但是当我在应用程序委托中尝试相同的操作时,我的应用程序崩溃了。几天前,这件事在应用程序委托中运行良好。现在我不知道出了什么问题。
我使用了这篇文章中的代码 。有人可以详细说明吗?
编辑:代码
我的 App Delegate 的 .h 文件内容
//
// 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
我的 App Delegate 的 .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)
谢谢,安杰洛。