ios +使用应用程序委托中的 asynudpsocket

发布于 2024-11-05 05:17:18 字数 4805 浏览 1 评论 0原文

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

当我将相关代码放入视图控制器时,一切正常。但是当我在应用程序委托中尝试相同的操作时,我的应用程序崩溃了。几天前,这件事在应用程序委托中运行良好。现在我不知道出了什么问题。

我使用了 中的代码这篇文章 .有人可以详细说明一下吗?

编辑:编写

我的应用程序委托的 .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)

谢谢, 安杰洛。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-11-12 05:17:19

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

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

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

为了获得一个不只是基于崩溃统计原因的合理答案,您应该提供代码、崩溃日志和堆栈跟踪。

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

编辑2:

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

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

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.

还不是爱你 2024-11-12 05:17:19

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

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

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

在设置完 currentViewController 属性后,我还立即释放了应用程序委托的实例。一旦我删除了那条线,我就很好了,我的应用程序就启动并运行了。

我想我应该在 dealloc 方法中取消分配它。如果我的理解有误,请指正。

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

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

安杰洛。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文