Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leaks on iOS #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CovidCertificate.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
48CCA621265F93D200887F63 /* CovidCertificateSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 48CCA620265F93D200887F63 /* CovidCertificateSDK */; };
48CCA623265F93DB00887F63 /* CovidCertificateSDK in Frameworks */ = {isa = PBXBuildFile; productRef = 48CCA622265F93DB00887F63 /* CovidCertificateSDK */; };
48ECCD2A264F0797008678DD /* StaticContentViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DC8ABDF2264A652800FDE0E1 /* StaticContentViewModel.swift */; };
5220AD9C28A3A8BB00460116 /* Client.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5220AD9B28A3A8BB00460116 /* Client.swift */; };
5220AD9E28A3A8D800460116 /* Server.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5220AD9D28A3A8D800460116 /* Server.swift */; };
6E0281EF2655022A00582904 /* Impressum in Resources */ = {isa = PBXBuildFile; fileRef = 6E0281EE2655022A00582904 /* Impressum */; };
6E0281F02655022A00582904 /* Impressum in Resources */ = {isa = PBXBuildFile; fileRef = 6E0281EE2655022A00582904 /* Impressum */; };
6E0281F226550E1100582904 /* InfoBoxView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E0281F126550E1100582904 /* InfoBoxView.swift */; };
Expand Down Expand Up @@ -454,6 +456,8 @@
274E946A2671E46200110DC5 /* swiss_governmentrootcaii.der */ = {isa = PBXFileReference; lastKnownFileType = file; path = swiss_governmentrootcaii.der; sourceTree = "<group>"; };
481D1B252685406700A5F804 /* TransferError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransferError.swift; sourceTree = "<group>"; };
4825D131273D5F3E00F4F71E /* SDKOptionsManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SDKOptionsManager.swift; sourceTree = "<group>"; };
5220AD9B28A3A8BB00460116 /* Client.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Client.swift; sourceTree = "<group>"; };
5220AD9D28A3A8D800460116 /* Server.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Server.swift; sourceTree = "<group>"; };
6E0281EE2655022A00582904 /* Impressum */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Impressum; sourceTree = "<group>"; };
6E0281F126550E1100582904 /* InfoBoxView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoBoxView.swift; sourceTree = "<group>"; };
6E0281F42655280500582904 /* InfoBoxButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoBoxButton.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1168,6 +1172,8 @@
6EE55D9F2760A01F001F9E1F /* VerifierHomescreenBottomView.swift */,
6EC1735E2642A1B4002E27DB /* VerifierHomescreenViewController.swift */,
6EA5C3632644298E00EA066F /* VerifierHomescreenInfoView.swift */,
5220AD9B28A3A8BB00460116 /* Client.swift */,
5220AD9D28A3A8D800460116 /* Server.swift */,
);
path = Homescreen;
sourceTree = "<group>";
Expand Down Expand Up @@ -1980,6 +1986,7 @@
DCBF64E92679B92C00450D51 /* StackScrollViewController.swift in Sources */,
6EC172E726429E31002E27DB /* DebugLog.swift in Sources */,
6EC172E826429E31002E27DB /* UBButton.swift in Sources */,
5220AD9C28A3A8BB00460116 /* Client.swift in Sources */,
6EC172E926429E31002E27DB /* UIStackView+CC.swift in Sources */,
6EC172EA26429E31002E27DB /* UBKeyboardObserver.m in Sources */,
F8130CE02685B99F00A29326 /* Collection+Safe.swift in Sources */,
Expand Down Expand Up @@ -2007,6 +2014,7 @@
6EC172F326429E31002E27DB /* UIDevice+ScreenType.swift in Sources */,
6EA5C37226444E8600EA066F /* VerifyCheckViewController.swift in Sources */,
6EC172F426429E31002E27DB /* StackScrollView.swift in Sources */,
5220AD9E28A3A8D800460116 /* Server.swift in Sources */,
F88DA6B4268C786D001DB072 /* Error+NetworkError.swift in Sources */,
F8E6C5632683808F00A9E112 /* DebugSwitchView.swift in Sources */,
6EC172F526429E31002E27DB /* ClickthroughStackView.swift in Sources */,
Expand Down
27 changes: 27 additions & 0 deletions Verifier/Screens/Homescreen/Client.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
/*
* Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

import Foundation


class Client {
var server: Server

init (server: Server) {
self.server = server

self.server.add(client: self) //This line creates a Retain Cycle -> Leak!
}

deinit {
print("Client di init")
}
}
56 changes: 56 additions & 0 deletions Verifier/Screens/Homescreen/LeakyObject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
/*
* Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

import Foundation

class LeakySwiftObject {
var cycle: LeakySwiftObject?
let bigObject = BigObject()

@inline(never)
init(cycleSize: Int = 0) {
var head = self

for _ in 0..<cycleSize {
head = LeakySwiftObject(cycle: head)
}

self.cycle = head

print("init LeakySwiftObject")
}

private init(cycle: LeakySwiftObject) {
self.cycle = cycle
}

deinit {
print("deinit LeakySwiftObject")
}
}

class BigObject {
let sizeInBytes = 10_000_000

var data: Data
init() {
// When using random data, a pointer back to one of the objects
// is possible (even likely) to occur in the data.
// This triggers the leak detector to see cycles that really aren't there.
// Note that this also shows that the leak detector can't be 100% reliable:
// If by chance the app contains a data that *could be, but isn't* a pointer to
// a leaked object, the leak detector will not detect that leak.
// let range = 0..<Int8.max
// let bytes = [0..<sizeInBytes].map { _ in Int8.random(in: range) }
let bytes: [Int8] = (0..<sizeInBytes).map { Int8.init(truncatingIfNeeded: $0) }
data = Data(bytes: bytes, count: sizeInBytes)
}
}
35 changes: 35 additions & 0 deletions Verifier/Screens/Homescreen/Server.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
/*
* Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

import Foundation


class Server {

let id: Int

var clients: [Client] //Because this reference is strong

func add(client: Client) {
self.clients.append(client)
}


init(id: Int) {
self.id = id
self.clients = []
}

deinit {
print("Server di init")
}
}

42 changes: 42 additions & 0 deletions Verifier/Screens/Homescreen/SwiftActionHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
/*
* Copyright (c) 2021 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

import Foundation

class SwiftActionHandler {
@inline(never)
private func privateFunction(depth: Int) {
if depth == 0 {
sleep(1)
} else {
privateFunction(depth: depth - 1)
}
}

@inline(never)
func callRecursiveFunction(depth: Int) {
privateFunction(depth: depth)
}

@inline(never)
func crash() {
raise(SIGSEGV)
}

@inline(never)
func doAfterRecursing(depth: Int = 0, action: () -> ()) {
if depth == 0 {
action()
} else {
doAfterRecursing(depth: depth - 1, action: action)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ extension Notification.Name {

class VerifierHomescreenViewController: HomescreenBaseViewController {
// MARK: - Init



init() {
super.init(color: .cc_greyish)
print("Creating Server...")
let server = Server(id: 3)
let client = Client(server: server)
server.add(client: client)
}

// MARK: - Subviews
Expand Down