master-nevi / UPnAtom

An open source Universal Plug and Play library with a focus on media streaming coordination using the UPnP A/V profile; written in Swift but for both Objective-C and Swift apps.
MIT License
137 stars 55 forks source link

"attempted to retain deallocated object" crash on device discovery #18

Open zbrox opened 8 years ago

zbrox commented 8 years ago

The example app on the swift-2.1 branch crashes on discovery of only some UPnP devices with a "attempted to retain deallocated object" error.

#0  0x00000001007ed418 in _swift_abortRetainUnowned ()
#1  0x00000001007fb0d8 in swift_unknownRetainUnowned ()
#2  0x000000010035f92c in UPnPServiceParser.(init(UPnPServiceParser.Type) -> (supportNamespaces : Bool, upnpService : AbstractUPnPService, descriptionXML : NSData) -> UPnPServiceParser).(closure #4) at [...]/UPnAtom/Source/UPnP Objects/AbstractUPnPService.swift:340
#3  0x000000010034e798 in thunk ()
#4  0x000000010033fccc in AbstractSAXXMLParser.parser(NSXMLParser, didEndElement : String, namespaceURI : String?, qualifiedName : String?) -> () at [...]/UPnAtom/Source/Parsers/AbstractSAXXMLParser.swift:153
#5  0x00000001003401bc in @objc AbstractSAXXMLParser.parser(NSXMLParser, didEndElement : String, namespaceURI : String?, qualifiedName : String?) -> () ()
#6  0x00000001839f7708 in _endElementNs ()
#7  0x0000000197e3d29c in xmlParseEndTag2 ()
#8  0x0000000197e39274 in xmlParseTryOrFinish ()
#9  0x0000000197e37e78 in xmlParseChunk ()
#10 0x00000001839f4a00 in -[NSXMLParser parseData:] ()
#11 0x00000001839f4c58 in -[NSXMLParser parseData:] ()
#12 0x00000001839f4e04 in -[NSXMLParser parseFromStream] ()
#13 0x000000010033e704 in AbstractSAXXMLParser.(startParser in _8BCF080863E3EC808A94616401B0C7C2)(NSXMLParser) -> EmptyResult at [...]/UPnAtom/Source/Parsers/AbstractSAXXMLParser.swift:110
#14 0x00000001003404e8 in AbstractSAXXMLParser.(parse(AbstractSAXXMLParser) -> (data : NSData) -> EmptyResult).(closure #1) at [...]/UPnAtom/Source/Parsers/AbstractSAXXMLParser.swift:96
#15 0x0000000101604ee8 in autoreleasepool(() -> ()) -> () ()
#16 0x000000010033e604 in AbstractSAXXMLParser.parse(data : NSData) -> EmptyResult at [...]/UPnAtom/Source/Parsers/AbstractSAXXMLParser.swift:98
#17 0x0000000100358e74 in UPnPServiceParser.parse() -> Result<UPnPServiceParser.ParserUPnPService> at [...]/UPnAtom/Source/UPnP Objects/AbstractUPnPService.swift:376
#18 0x0000000100353a04 in AbstractUPnPService.init(usn : UniqueServiceName, descriptionURL : NSURL, descriptionXML : NSData) -> AbstractUPnPService? at [...]/UPnAtom/Source/UPnP Objects/AbstractUPnPService.swift:82
#19 0x000000010037e538 in ConnectionManager1Service.init(usn : UniqueServiceName, descriptionURL : NSURL, descriptionXML : NSData) -> ConnectionManager1Service? ()
#20 0x000000010037e4b8 in ConnectionManager1Service.__allocating_init(usn : UniqueServiceName, descriptionURL : NSURL, descriptionXML : NSData) -> ConnectionManager1Service? ()
#21 0x00000001003f7848 in UPnPRegistry.(createUPnPObject in _7D977E0CCE069527737ED3D090664CDE)(usn : UniqueServiceName, descriptionURL : NSURL, descriptionXML : NSData) -> AbstractUPnP? at [...]/UPnAtom/Source/Management/UPnPRegistry.swift:141
#22 0x00000001003fa908 in UPnPRegistry.(addUPnPObject in _7D977E0CCE069527737ED3D090664CDE)(forSSDPDiscovery : SSDPDiscovery, descriptionXML : NSData, upnpObjects : inout [UniqueServiceName : AbstractUPnP]) -> () at [...]/UPnAtom/Source/Management/UPnPRegistry.swift:238
#23 0x000000010040082c in UPnPRegistry.((getUPnPDescription in _7D977E0CCE069527737ED3D090664CDE)(UPnPRegistry) -> (forSSDPDiscovery : SSDPDiscovery) -> ()).(closure #1).(closure #1) at [...]/UPnAtom/Source/Management/UPnPRegistry.swift:221
zbrox commented 8 years ago

Using po $arg1 on the exception breakpoint I get the following, clearer error:

fatal error: unexpectedly found nil while unwrapping an Optional value
4914441056
zbrox commented 8 years ago

I tried with an older XCode to run the version on the master branch and there the crash doesn't occur.

master-nevi commented 8 years ago

So it's consistently breaking when it parses the device's service list? AbstractUPnPService.swift:340 is where <*><device><serviceList><service> is parsed.

Can you send me a copy of the upnp description xml that was causing this to happen?

It should look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<root configId="10230464" xmlns="urn:schemas-upnp-org:device-1-0" xmlns:dlna="urn:schemas-dlna-org:device-1-0">
  <specVersion>
    <major>1</major>
    <minor>1</minor>
  </specVersion>
  <device>
    <deviceType>urn:schemas-upnp-org:device:MediaServer:1</deviceType>
    <friendlyName>Kodi (ordershop-lm)</friendlyName>
    <manufacturer>XBMC Foundation</manufacturer>
    <manufacturerURL>http://kodi.tv/</manufacturerURL>
    <modelDescription>Kodi - Media Server</modelDescription>
    <modelName>Kodi</modelName>
    <modelNumber>15.2 Git:2015-10-19-02e7013</modelNumber>
    <modelURL>http://kodi.tv/</modelURL>
    <UDN>uuid:b77dbf33-abc8-8c30-bc51-c0723a3f724c</UDN>
    <presentationURL>http://192.168.1.252:8080/</presentationURL>
    <dlna:X_DLNADOC xmlns:dlna="urn:schemas-dlna-org:device-1-0">DMS-1.50</dlna:X_DLNADOC>
    <iconList>
      <icon>
        <mimetype>image/png</mimetype>
        <width>256</width>
        <height>256</height>
        <depth>8</depth>
        <url>/icon256x256.png</url>
      </icon>
      <icon>
        <mimetype>image/png</mimetype>
        <width>120</width>
        <height>120</height>
        <depth>8</depth>
        <url>/icon120x120.png</url>
      </icon>
      <icon>
        <mimetype>image/png</mimetype>
        <width>48</width>
        <height>48</height>
        <depth>8</depth>
        <url>/icon48x48.png</url>
      </icon>
      <icon>
        <mimetype>image/png</mimetype>
        <width>32</width>
        <height>32</height>
        <depth>8</depth>
        <url>/icon32x32.png</url>
      </icon>
      <icon>
        <mimetype>image/png</mimetype>
        <width>16</width>
        <height>16</height>
        <depth>8</depth>
        <url>/icon16x16.png</url>
      </icon>
    </iconList>
    <serviceList>
      <service>
        <serviceType>urn:microsoft.com:service:X_MS_MediaReceiverRegistrar:1</serviceType>
        <serviceId>urn:microsoft.com:serviceId:X_MS_MediaReceiverRegistrar</serviceId>
        <SCPDURL>/X_MS_MediaReceiverRegistrar/b77dbf33-abc8-8c30-bc51-c0723a3f724c/scpd.xml</SCPDURL>
        <controlURL>/X_MS_MediaReceiverRegistrar/b77dbf33-abc8-8c30-bc51-c0723a3f724c/control.xml</controlURL>
        <eventSubURL>/X_MS_MediaReceiverRegistrar/b77dbf33-abc8-8c30-bc51-c0723a3f724c/event.xml</eventSubURL>
      </service>
      <service>
        <serviceType>urn:schemas-upnp-org:service:ContentDirectory:1</serviceType>
        <serviceId>urn:upnp-org:serviceId:ContentDirectory</serviceId>
        <SCPDURL>/ContentDirectory/b77dbf33-abc8-8c30-bc51-c0723a3f724c/scpd.xml</SCPDURL>
        <controlURL>/ContentDirectory/b77dbf33-abc8-8c30-bc51-c0723a3f724c/control.xml</controlURL>
        <eventSubURL>/ContentDirectory/b77dbf33-abc8-8c30-bc51-c0723a3f724c/event.xml</eventSubURL>
      </service>
      <service>
        <serviceType>urn:schemas-upnp-org:service:ConnectionManager:1</serviceType>
        <serviceId>urn:upnp-org:serviceId:ConnectionManager</serviceId>
        <SCPDURL>/ConnectionManager/b77dbf33-abc8-8c30-bc51-c0723a3f724c/scpd.xml</SCPDURL>
        <controlURL>/ConnectionManager/b77dbf33-abc8-8c30-bc51-c0723a3f724c/control.xml</controlURL>
        <eventSubURL>/ConnectionManager/b77dbf33-abc8-8c30-bc51-c0723a3f724c/event.xml</eventSubURL>
      </service>
    </serviceList>
  </device>
</root>
zbrox commented 8 years ago

Sorry for the late response. Here is the description xml.

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:schemas-upnp-org:device-1-0" xmlns:dlna="urn:schemas-dlna-org:device-1-0">
    <specVersion>
        <major>1</major>
        <minor>0</minor>
    </specVersion>
    <device>
        <deviceType>urn:schemas-upnp-org:device:MediaServer:3</deviceType>
        <manufacturer>Parrot SA</manufacturer>
        <manufacturerURL>http://www.parrot.com/</manufacturerURL>
        <modelNumber>2014.05.22</modelNumber>
        <serialNumber>0</serialNumber>
        <dlna:X_DLNADOC />
        <friendlyName>Folk</friendlyName>
        <modelDescription>Folk is Pulsar implementation of Filesystems On Local networKs</modelDescription>
        <modelName>pulsar-test-platform</modelName>
        <UDN>uuid:8c9a81b5-4989-4390-ac9b-7403a073da29</UDN>
        <iconList>
            <icon>
                <mimetype>image/png</mimetype>
                <width>120</width>
                <height>120</height>
                <depth>32</depth>
                <url>/pulsar-test-platform-120x120x32.PNG_LRG</url>
            </icon>
            <icon>
                <mimetype>image/jpeg</mimetype>
                <width>120</width>
                <height>120</height>
                <depth>32</depth>
                <url>/pulsar-test-platform-120x120x32.JPEG_LRG</url>
            </icon>
        </iconList>
        <serviceList>
            <service>
                <serviceType>urn:schemas-upnp-org:service:ContentDirectory:3</serviceType>
                <serviceId>urn:upnp-org:serviceId:ContentDirectory</serviceId>
                <SCPDURL>/xml/ContentDirectory-NoTrack.xml</SCPDURL>
                <controlURL>/Control/pulsar-test-platform/RygelContentDirectory</controlURL>
                <eventSubURL>/Event/pulsar-test-platform/RygelContentDirectory</eventSubURL>
            </service>
            <service>
                <serviceType>urn:schemas-upnp-org:service:ConnectionManager:2</serviceType>
                <serviceId>urn:upnp-org:serviceId:ConnectionManager</serviceId>
                <SCPDURL>/xml/ConnectionManager.xml</SCPDURL>
                <controlURL>/Control/pulsar-test-platform/RygelSourceConnectionManager</controlURL>
                <eventSubURL>/Event/pulsar-test-platform/RygelSourceConnectionManager</eventSubURL>
            </service>
            <service>
                <serviceType>urn:microsoft-com:service:X_MS_MediaReceiverRegistrar:1</serviceType>
                <serviceId>urn:microsoft-com:serviceId:X_MS_MediaReceiverRegistrar</serviceId>
                <SCPDURL>/xml/X_MS_MediaReceiverRegistrar1.xml</SCPDURL>
                <controlURL>/Control/pulsar-test-platform/RygelMediaReceiverRegistrar</controlURL>
                <eventSubURL>/Event/pulsar-test-platform/RygelMediaReceiverRegistrar</eventSubURL>
            </service>
            <service>
                <serviceType>urn:parrot-com:service:X_PulsarService:1</serviceType>
                <serviceId>urn:parrot-com:serviceId:X_PulsarService</serviceId>
                <SCPDURL>/xml/PulsarService.xml</SCPDURL>
                <controlURL>/Control/pulsar-test-platform/PulsarService</controlURL>
                <eventSubURL>/Event/pulsar-test-platform/PulsarService</eventSubURL>
            </service>
        </serviceList>
    </device>
</root>
zbrox commented 8 years ago

I forgot to mention that I managed to work around this crash. Initially I was using all the ssdp types to discover as in the example app. When I changed those to include only the urns of the devices and services I was interested in it stopped crashing.

zbrox commented 8 years ago

It still occurs but very rarely.