Iphone 从类调用方法时EXC_访问错误
全部, 我有一个视图控制器,我们叫它testViewController,它调用另一个类名扫描中的方法,方法名initNetworkCommunication 以下是视图控制器的按钮方法:Iphone 从类调用方法时EXC_访问错误,iphone,objective-c,xcode,Iphone,Objective C,Xcode,全部, 我有一个视图控制器,我们叫它testViewController,它调用另一个类名扫描中的方法,方法名initNetworkCommunication 以下是视图控制器的按钮方法: -(IBAction) test { Scan *canConnect = [[Scan alloc] init]; [canConnect initNetworkCommunication]; } 这是课堂 //scan.h @interface Scan : NSObject <
-(IBAction) test
{
Scan *canConnect = [[Scan alloc] init];
[canConnect initNetworkCommunication];
}
这是课堂
//scan.h
@interface Scan : NSObject <NSStreamDelegate>
{
NSInputStream *inputStream;
NSOutputStream *outputStream;
}
-(void) scan;
-(void) initNetworkCommunication;
@property (nonatomic, retain) NSInputStream *inputStream;
@property (nonatomic, retain) NSOutputStream *outputStream;
@end
//scan.m
#import "Scan.h"
@implementation Scan
@synthesize inputStream, outputStream;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
-(void) initNetworkCommunication
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.17.1", 2004, &readStream, &writeStream);
NSLog(@"readStream %@", readStream);
NSLog(@"writeStream %@", writeStream);
inputStream = (NSInputStream *) readStream;
outputStream = (NSOutputStream *) writeStream;//this __strong may work!
NSLog(@"inputStream %@", inputStream);
NSLog(@"outputStream %@", outputStream);
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
NSLog(@"She be opened, sir!");
return;
}
//more code is here, dealloc, etc
我正在获得EXC\u BAD\u访问权限,当我启用NSZombieEnable时,我的调试会话如下所示:
2011-07-15 13:09:46.210 Project[1176:f203] readStream <__NSCFInputStream: 0x6e7a2a0>
2011-07-15 13:09:46.212 Project[1176:f203] writeStream <__NSCFOutputStream: 0x6e7a340>
2011-07-15 13:09:46.213 Project[1176:f203] inputStream <__NSCFInputStream: 0x6e7a2a0>
2011-07-15 13:09:46.214 Project[1176:f203] outputStream <__NSCFOutputStream: 0x6e7a340>
2011-07-15 13:09:46.215 Project[1176:f203] She be opened, sir!
2011-07-15 13:09:46.220 Project[1176:f203] *** -[Scan respondsToSelector:]: message sent to deallocated instance 0x6e79b20
Current language: auto; currently objective-c
(gdb)
它在三个星号处崩溃,这是我的僵尸信息
我该怎么办
编辑:在Instruments中运行程序后,我发现没有内存泄漏。让我感兴趣的是,在工具中显示为SocketStream::dispatchSignalFromSocketCallbackUnlockedSocketStream将inputStream、outputStream更改为self,并在各处使用点语法 到处都是这样 因为如果你合成这个属性,你必须使用self和dot语法来保留它们
self.inputStream = (NSInputStream *) readStream;
self.outputStream = (NSOutputStream *) writeStream;//this __strong may work!
我建议你改变函数的名称
initNetworkCommunication to some将inputStream、outputStream更改为带点语法的self 到处都是这样 因为如果你合成这个属性,你必须使用self和dot语法来保留它们
self.inputStream = (NSInputStream *) readStream;
self.outputStream = (NSOutputStream *) writeStream;//this __strong may work!
我建议你改变函数的名称
initNetworkCommunication to some您忘记了保留作为流的委托的扫描对象 在关闭流之前,您的流代理将接收消息,如果该代理被解除分配,则您的应用程序将崩溃
在泄漏canConnect对象的示例代码中,应该没有问题,但在实际代码中,您可能过早释放了扫描对象。您忘记了保留作为流的委托的扫描对象 在关闭流之前,您的流代理将接收消息,如果该代理被解除分配,则您的应用程序将崩溃
在泄漏canConnect对象的示例代码中,这应该没问题,但在实际代码中,您可能会过早释放扫描对象。只需从属性中删除inputStream和outputStream,让它们仅保留类变量即可。 在ARC的情况下,它会有所帮助
就我而言,它解决了所有问题。用另一种方法是不可能解决的。只要从属性中删除inputStream和outputStream,让它们只保留类变量即可。 在ARC的情况下,它会有所帮助
就我而言,它解决了所有问题。无法以其他方式解决此问题。而且,具有讽刺意味的是,一个过度释放的对象导致了崩溃。是的,请尝试使用Instruments运行僵尸在Instruments中运行程序后,我发现没有内存泄漏。让我感到困惑的是,在仪器中显示为SocketStream::DispatchSignalFromSocketCallbackUnlockedSocketStream,讽刺的是,一个过度释放的对象导致了崩溃。是的,尝试用仪器运行僵尸在仪器中运行程序后,我看不到内存泄漏。让我感兴趣的是在仪器中显示为SocketStream::DispatchSignalfromSocketCallbackUnlockedSocketStream无论您在何处更改或更改not@madhureadStream和writeStream是通过Create函数获得的,因此已经有了所有权。这只会让它变得更糟,到处都是。哦,好吧,值得一试。无论你在哪里改变或改变not@madhureadStream和writeStream是通过Create函数获得的,因此已经有了所有权。这只会让它变得更糟,到处都是。哦,好吧,值得一试。就是这样。我最终将扫描移动到视图控制器,它工作正常。现在我至少知道我的代码可以工作了,我很快就会把它扔回一个单独的类中。@James,你有没有想过这个问题?我也有同样的问题,就是这样。我最终将扫描移动到视图控制器,它工作正常。现在我至少知道我的代码可以工作了,我很快就会把它扔回一个单独的类中。@James,你有没有想过这个问题?我也有同样的问题。