0

我使用 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)

谢谢,安杰洛。

4

2 回答 2

1

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

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

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

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

编辑:所以您在链接中进行了编辑。所以现在我们有一些代码。但是,该代码与上下文无关,并且您已经告诉我们它有效,因此我们不会在那里找到答案。

编辑2:

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

所以我是对的。这是一个内存问题。您正在向已释放的对象发送消息。奇怪的是,它似乎是您的应用程序委托已被释放。

于 2011-05-05T08:10:30.367 回答
0

埃里克,你的评论很到位。我已经弄清楚了我的问题的位置。

让我解释一下:就像我评论的那样,我正在从应用程序委托移动到一个屏幕,然后从那个屏幕移动到另一个屏幕。

本质上,在最后一个屏幕的(视图)视图控制器中,我正在创建我的应用程序委托的一个实例,以将最后一个屏幕的视图控制器设置为我的应用程序代理的 currentViewController 属性。我出于不同的原因维护它。

在我设置完 currentViewController 属性后,我也是 DEALLOCATING 应用程序委托的实例。一旦我删除了那条线,我就很好了,我的应用程序已经启动并运行了。

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

感谢你,埃里克,为我指明了正确的方向。

编辑:埃里克,请告诉我你是如何从崩溃日志中知道的,这是我的应用程序委托正在被释放它。

安杰洛。

于 2011-05-06T10:44:39.160 回答