Search code examples
ios4memory-leaksftpexc-bad-accessassert

CFWriteStreamRef EXC_BAD_ACCESS Problem? FTP


I'm trying to connect to an ftp server through iPhone SDK, here's my faulty code (simplified):

NSURL *serverURL;
NSString *serverUsername;
NSString *serverPassword;
CFURLRef serverURLRef;
CFStringRef serverPasswordRef;
CFStringRef serverUsernameRef;

serverURL = [[NSURL alloc] initWithString: @"XX.XX.XX.XX"];
serverURLRef = (__bridge CFURLRef) serverURL;

serverUsername = [[NSString alloc] initWithString: @"********"];
serverUsernameRef = (__bridge CFStringRef)serverUsername;

serverPassword = [[NSString alloc] initWithString: @"********"];
serverPasswordRef = (__bridge CFStringRef)serverPassword;


CFWriteStreamRef stream = CFWriteStreamCreateWithFTPURL(kCFAllocatorDefault, serverURLRef);
    CFWriteStreamSetProperty(stream, kCFStreamPropertyFTPUserName, serverUsernameRef);
    CFWriteStreamSetProperty(stream, kCFStreamPropertyFTPPassword, serverPasswordRef);
    CFWriteStreamSetProperty(stream, kCFStreamPropertyFTPUsePassiveMode, kCFBooleanFalse);

    CFWriteStreamOpen(stream);

I get the error of "EXC_BAD_ACCESS" for the line: CFWriteStreamSetProperty(stream, kCFStreamPropertyFTPUserName, serverUsernameRef);

When I comment out that line, it gets the same error on the next line. I think this may have to do with memory leakage, but I don't really know exactly where I'm going wrong.

Thanks in advance.

EDIT:

I added an assert, which seems to be showing me that the stream is nil even after it is declared: assert(stream!=nil);

The assertion fails on this line.

I tried running the app in Instruments, and it said "leaks discovered", but did not list them: leaks in instruments


Solution

  • I found the solution to my own problem for anyone that is curious. I had forgotten to precede the URL with "ftp://", which, when calling the "CFWriteStreamCreateWithFTPURL" function, messed things up by not setting up the stream due to an invalid URL. Therefore, "stream" was set equal to 'nil' because it had not been set up properly.