0

I am trying to do a simple DNS lookup with Swift code. So far, I have

    if  host != "\0" {
        let hostRef = CFHostCreateWithName(kCFAllocatorDefault, host.bridgeToObjectiveC()).takeRetainedValue()
        var resolved = CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, nil)
        let addresses = CFHostGetAddressing(hostRef, &resolved).takeRetainedValue() as NSArray

        for address: AnyObject in addresses {
            println(address)  // address is of type NSData.
        }
    }

as per Convert NSData to sockaddr struct in swift. (host is an NSString.)

However, my debugger log prints <10020000 4a7de064 00000000 00000000>, before exiting with an EXC_BAD_ACCESS (code=EXC_I386_GPFLT) on the first line, AFTER executing the if statement and printing the address data. All I'm trying to get is a string with an IP address, or if the host does not exist, a null string.

Community
  • 1
  • 1
Matt
  • 2,576
  • 6
  • 34
  • 52
  • 1
    At what line does it crash? – duci9y Jul 22 '14 at 21:23
  • @duci9y: Sorry, the first line (after the code has executed, so technically the last line/end of the `if` statement). I'll clarify that in the question. – Matt Jul 22 '14 at 21:24
  • I'm not sure if you are correct about that. Put break points on each line of code, and tell us about the last line that executes successfully, and the number of times it does so. – duci9y Jul 22 '14 at 21:32
  • @duci9y: It definitely executes to the end and loops through the `for` loop 11 times. Then the thread indicator goes to the opening `if` statement and crashes. – Matt Jul 22 '14 at 21:39
  • @duci9y: http://pastebin.com/78TpwZ1S. `host` is taken from an `NSTextField`. – Matt Jul 22 '14 at 21:55
  • Well well well, you shouldn't have tagged the question as ios when you're doing this in a Mac App. This is gonna be a long trip. Backtrace says something that wasn't meant to be released was sent a release call. Turn on NSZombies and check who it was. – duci9y Jul 22 '14 at 21:58
  • @duci9y: Good point, I'll change that. How do I turn on NSZombies, and what is that? – Matt Jul 22 '14 at 21:59
  • I suggest you search around for the steps to do that. – duci9y Jul 23 '14 at 07:39
  • @duci9y: this is the message I get: `*** -[__NSArrayM release]: message sent to deallocated instance 0x60000064f0c0` – Matt Jul 31 '14 at 20:54
  • Please post all of the code in the method causing problems. – duci9y Aug 01 '14 at 14:52
  • @duci9y: All I'm trying to do is get new code, not fix existing code. I'll post it when I have time, but if you have a solution that works, can you please put it as an answer? – Matt Aug 01 '14 at 18:22
  • How can I have a solution when you aren't telling me what the problem is? – duci9y Aug 03 '14 at 09:35
  • @duci9y: the problem is that I need a code snippet that takes a string `host`, looks up its DNS address, and returns that IP address in a string (or a null string if no address is found). – Matt Aug 04 '14 at 21:42
  • 1
    Thanks to Martin R who answered my similar question. Have a look! I hope this help you too. http://stackoverflow.com/questions/25890533/how-can-i-get-a-real-ip-address-from-dns-query-in-swift – Hristo Atanasov Sep 17 '14 at 14:19

1 Answers1

0

I have tested and found that AnyObject is casuse of crash.You do not need AnyObject as swift will infer type from array addresses

var host = "192.168.103.13"
    if  host != "\0" {
        let hostRef = CFHostCreateWithName(kCFAllocatorDefault, host.bridgeToObjectiveC()).takeRetainedValue()
        var resolved = CFHostStartInfoResolution(hostRef, CFHostInfoType.Addresses, nil)
        let addresses = CFHostGetAddressing(hostRef, &resolved).takeRetainedValue() as NSArray

        println(addresses)
        //Remove `AnyObject` as there is no need.Swift will infrence from array addresses
        for address in addresses {
            println(address)  // address is of type NSData.
        }
    }
codester
  • 36,891
  • 10
  • 74
  • 72
  • 1
    I'm trying to go from a hostname to an IP address. Also, this still doesn't give me the data that I want, which is a string with an IP address. – Matt Jul 22 '14 at 21:35
  • 1
    i found that this gives you something like `<10020000 3b7a9621 00000000 00000000>` where the second group of 8 symbols are the IP address in hex. So to produce the IP address you have to convert every 2 symbols from hex to decimal (like 3b7a9621 = 3b.7a.96.21 = 59.122.96.33). I hope there is bether solution for that case ... I need it too .. :) – Hristo Atanasov Sep 17 '14 at 09:33
  • I've just ran into this problem but with CFHostGetAddressing you should be calling takeUnretainedValue instead of takeRetainedValue. http://stackoverflow.com/questions/34488893/swift-crashes-in-cfrelease-when-leaving-scope – Joshua Waring Dec 28 '15 at 07:14