Questions tagged [deinit]

Swift programming language object destructor

A deinitializer is called immediately before a class instance is deallocated. You write deinitializers with the deinit keyword, similar to how initializers are written with the init keyword. Deinitializers are only available on class types.

102 questions
1
vote
3 answers

Swift: How to deallocate properties when init throw?

There is a memory leak with this example code. The pointer1 and pointer2 allocate before Person inits successfully. If the init function throws an Error. The deinit function will never be executed. So the pointer1 and pointer2 will never be…
Yanni
  • 580
  • 2
  • 6
  • 21
1
vote
1 answer

De-initialzing a ViewController after dismissal?

I have two viewControllers in my App, the code for the first viewController is as illustrated below: import UIKit class firstViewController: UIViewController { // The below two variables will be passed from the…
devJo
  • 63
  • 7
1
vote
4 answers

How to send a last message and close the socket when a SwiftUI view is dismissed?

I have a SwiftUI sheet view with an ObservedObject that handles tcp communication, when this sheet gets dismissed I need it to send a last tcp message and then close the socket. The onDisappear event never seems to get triggered (Edit: found out the…
Watermamal
  • 357
  • 3
  • 12
1
vote
2 answers

Swift - Viewcontrollers not deinit when segue is performed

I am fairly new to the whole concept of memory management and ARC. I've recently looked into it and found mixed success. Firstly it should be noted that some ViewController do deinitialize but I am still learning and cleaning up my application.…
1
vote
1 answer

Swift - Is the correct place to call .removeObserver always deinit()?

New StackOverflow user here (first time posting, long time lurking w/o an account). Before I begin, these are some previously answered questions that I've found to be helpful but have not completely resolved my issue: How to safely removeObserver…
10623169
  • 984
  • 1
  • 12
  • 22
1
vote
1 answer

Deinit not called where blocks used in swift 3.0

I have used blocks in cell for getting switch value but now my problem is that deinit not called where i used the blocks. it is completely working previously but in swift 3.0 it is not working. My switch cell : import UIKit class CellSwitch:…
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
1
vote
1 answer

Is cleaning up strong references in deinit a correct pattern?

There are several resources (blog, SO question, plus I've seen it used everywhere) that recommend removing an observer from the NotificationCenter in the deinit of the UIViewController, e.g.: deinit { …
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
1
vote
1 answer

Table view deinit not called

I have a normal table view in a completely new project , with no extra code. In my storyboard I have 2 view controllers , and a navigation controller embedded to First Controller . In First Controller I have a table view with button and label. I…
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38
1
vote
1 answer

I call URLSession.share.datatask and before it can complete the user pops the uiviewcontroller

I am not sure how to prevent an error in my app. I need to call a server using URLSession.shared.dataTask which works perfectly fine if I allow it to complete. However, if I pop the uiviewcontroller off the stack before the…
Gregory Williams
  • 453
  • 5
  • 18
1
vote
1 answer

Is it safe to make a network call when a View Controller gets deinited?

Basically, I have a UIViewController that controls a chatroom. When the UIViewController gets deinited in any way, then the current user needs to leave the channel. Currently, my code is: class ChatViewController: SLKTextViewController { var…
David
  • 7,028
  • 10
  • 48
  • 95
1
vote
0 answers

deinit in Swift behavior

I wrote the following code in a Swift playground : class View : UIView { let name = "myView" deinit { print("deinit View") } } class Controller: NSObject { var view: View init(view : View) { self.view = view …
Bobj-C
  • 5,276
  • 9
  • 47
  • 83
1
vote
3 answers

Im confused on how can I manipulate the properties inside an instance when it gets deinitialized?

I have this from either the Apple documentation or the swift book When an instance gets deinitialized, you still have access to the properties inside the instance and can manipulate them as needed before the instance totally goes away. I'm…
Ali
  • 505
  • 4
  • 12
1
vote
1 answer

SKPaymentQueue.defaultQueue().removeTransactionObserver(self) Removing and adding

Right so i have this game on the app store called misshapen, in my game i have in app purchases which work fine however i have found a bug which will make the game crash if someone presses the in app purchase button then goes to the main menu (the…
Eli
  • 668
  • 2
  • 13
  • 37
1
vote
2 answers

Using NSTimer is Swift causes 'deinit' not to call

After using NSTimer, the class deinit method is never called. The class is written in Swift, and I call the timer at the "init" like so: init(eventsDistributor : EventsDistributor, orderSide : ORDER_SIDE, stockViewModel : StockViewModel, orderType :…
MCMatan
  • 8,623
  • 6
  • 46
  • 85
0
votes
1 answer

memory leaking in custom button memory problems

Trying to figure out why the deinit is not called in OptionsButton class func getActionButtonView(delegate: DiscoveryActionViewDelegate) -> UIView { switch delegate.actionType { case .showVariants: let optionButton = OptionsButton(frame:…
Al-Zubair
  • 3
  • 2