ios +从app delegate使用asynudpsocket

时间:2011-05-05 07:56:49

标签: iphone udp asyncsocket

我使用AsynUDPSocket类创建了一个简单的应用程序。它侦听来自UDP客户端的简单文本消息。

当我将相关代码放入视图控制器时,一切正常。但是,当我在应用程序代理中尝试相同时,我的应用程序崩溃了。几天前,这个东西在app代理中工作得很好。现在我不知道出了什么问题。

我使用了this post中的代码 。有人可以详细说明吗?

编辑:代码

我的应用代表的.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

我的应用代表的.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)

谢谢, 安吉洛。

2 个答案:

答案 0 :(得分:1)

嗯,根据你给我们的信息,我猜你可能搞砸了内存管理。我想是的,因为这是导致崩溃的最常见原因。

  

我使用了这篇文章的代码。

什么帖子?这篇文章中没有代码。

为了得到一个不错的答案,不仅仅是基于崩溃的统计原因,你应该提供代码,崩溃日志和堆栈跟踪。

编辑:所以你在链接中进行了编辑。所以现在我们有了一些代码。然而,该代码已脱离了上下文,您已经告诉我们它的工作原理,所以我们不会在那里找到答案。

编辑2:

2011-05-05 15:29:46.698 MacSocketTest[1706:207] *** -[MacSocketTestAppDelegate respondsToSelector:]: message sent to deallocated instance 0x5954e00

所以我是对的。这是一个记忆问题。您正在向已释放的对象发送消息。有点奇怪的是,它似乎是你的应用代表已被解除分配。

答案 1 :(得分:0)

Erik,你的意见很明显。我已经弄明白问题的位置了。

让我解释一下: 就像我提交的那样,我正在从应用代表移动到一个屏幕,从那个屏幕移动到另一个屏幕。

本质上,在最后一个屏幕(视图)的视图控制器中,我创建了一个app appateate的实例,将最后一个屏幕的视图控制器设置为我的app delegate的currentViewController属性。我是出于不同的原因维护它。

在完成设置currentViewController属性之后,我也是DEALLOCATING,即app委托的实例。一旦我删除该行,我就很好,我的应用程序启动并运行。

我想我应该在dealloc方法中释放它。如果我错了,请纠正我的理解。

万分感谢你,Erik,指出我正确的方向。

编辑:Erik,请告诉我你是如何从崩溃日志中知道的那样,我的应用代表正在被解除分配。

安吉洛。