Packet Tunnel Provider - deinit

I've added a deinit function at the Packet Tunnel Provider (Network Extension)
Code Block
deinit {
        NSLog("PacketTunnelProvider deinit")
    }

And I noticed that it's not being called when I'm disconnecting the VPN

Calling stopTunnelWithReason because: Configuration was disabled

After a little investigation it seems that the problem is related to packetFlow: I have this function

Code Block
func readPacketsFromTUN(_ packets: [Data], protocols: [NSNumber]) {
for i in 0...packets.count-1 {
//handle packet
}
packetFlow.readPackets { inPackets, inProtocols in
self.readPacketsFromTUN(inPackets, protocols: inProtocols)
}
}


It seems that if (just for the test) I won't call packetFlow.readPackets each time, after I'm stopping the VPN it will get to the deinit function. However, I can't remove this call..

Is it a bug on my side, or at the Extension?


Answered by DTS Engineer in 652492022
Your deinitialiser is called when the last reference to your object is released. Your readPacketsFromTUN(…) method is guaranteed to retain such a reference (per the self on line 7).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Follow up: I tried to remove all the //handle packets code, so for testing purposes, this is the function:
Code Block
func readPacketsFromTUN(_ packets: [Data], protocols: [NSNumber]) {
packetFlow.readPackets { inPackets, inProtocols in
  self.readPacketsFromTUN(inPackets, protocols: inProtocols)
  }
}


And the problem still reproduces.
Removing the repeated calls to packetFlow.readPackets, will "fix" the problem and deinit will be called.
Isn't it suggesting about some memory leak at the extension?
Accepted Answer
Your deinitialiser is called when the last reference to your object is released. Your readPacketsFromTUN(…) method is guaranteed to retain such a reference (per the self on line 7).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Great, I don't know how I missed it!
Fixed it now, thank you very much!

(P.S - missed it because I'm using exit(0) to terminate the Extension, to fix a bug where connecting 'very fast' after last disconnection would cause disconnection after 20 sec)
Packet Tunnel Provider - deinit
 
 
Q