Ervin E
Ervin E

Reputation: 452

exc_bad_access code=1 When a method call is moved from viewDidLoad

Thanks for reading!

I'm new to iOS and following along a tutorial on a chat app communicating via telnet http://www.raywenderlich.com/3932/networking-tutorial-for-ios-how-to-create-a-socket-based-iphone-app-and-server

The tutorial is a single view controller with 2 views: the first for joining the chat session, the second for the chat view itself. This tutorial is a bit old but I was able to follow along successfully on latest XCode 5.1.1.

What I wanted to do is create a second view controller and be able to specify the IP/hostname and then connect. So I moved the call to [self initNetworkCommunication]; from viewDidLoad to a button press from a separate viewcontroller (which is still connected to the same ViewController class as the first view.

I successfully make a telnet connection BUT as soon as I start send messages, I get the exc_bad_access code=1 error. I created a button on the first view controller to do the same thing and I encountered no issues....

added this to ViewController.m and removed the call from viewDidLoad

- (IBAction)connectToServer:(id)sender {
    [self initNetworkCommunication];

}

So, how do I avoid this error?

*I have placed my code here https://github.com/ervine13/TelnetChatTest

Upvotes: 1

Views: 458

Answers (2)

chrs
chrs

Reputation: 6106

What happens is that you have created two global variables, which is generally not a good idea.

Now you load the main view, which is also controlling the popover view controller. This means that two instances of the same view controller class will be instantiated. Now when you press settings and the popover appears, the main view will be deallocated because no one has ownership of it. Therefore you could keep a strong reference to it in prepareForSegue: however that is not what I did to make it work.

I followed the singleton pattern and created a simple ServerHelper which holds a shared in- and out-put stream. Then I have created a simple controller for the Connect view, called ConnectViewController.

You can download the source here: http://www29.zippyshare.com/d/16861250/9924/TelnetChatTest-master.zip

Edit To connect to the server on the fly go into your ConnectViewController.mand add

[[ServerHelper sharedInstance] connectToIP:self.ipAddress.text port:80];

to the top of the connectToServer:

In the server helper add the following to the @interface ServerHelper: NSObject

- (void)connectToIP:(NSString *)IP port:(int)port;

Replace the current setStreamDelegate: with this:

- (void)setStreamDelegate:(id<NSStreamDelegate>)streamDelegate {
    _streamDelegate = streamDelegate;
    self.inputStream.delegate = streamDelegate;
    self.outputStream.delegate = streamDelegate;
}

Remove everything the ServerHelper's init and implement the following:

- (void)connectToIP:(NSString *)IP port:(int)port
{
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStringRef ipRef = (__bridge CFStringRef)IP;
    CFStreamCreatePairWithSocketToHost(NULL, ipRef, port, &readStream, &writeStream);

    self.inputStream = (NSInputStream *)CFBridgingRelease(readStream);
    self.outputStream = (NSOutputStream *)CFBridgingRelease(writeStream);

    self.inputStream.delegate = self.streamDelegate;
    self.outputStream.delegate = self.streamDelegate;

    [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [_inputStream open];
    [_outputStream open];
}

Upvotes: 1

Josshad
Josshad

Reputation: 964

You create streams in second instance of "ViewController" class. So in first controller input and output streams aren't initialised.

You should create(for example) another controller for popover and create delegate for main controller. After tapping connect button on popover you should sent some message from popover controller to delegate ([delegate connectToServer]), and then init streams in main controller.

Upvotes: 0

Related Questions