Questions tagged [strong-references]

Strong references are regular references that are protected from garbage collection, in contrast to weak references.

Strong references are regular references that are protected from garbage collection, in contrast to .

86 questions
37
votes
5 answers

How can identify strong reference cycles in Swift?

Is there a tool or method to locate strong references cycles in my SWIFT code? A strong reference cycle is when two instances of classes reference each other without the proper safeties (weak/unowned) hence preventing the garbage collector from…
31
votes
2 answers

weak or strong for IBOutlet and other

I have switched my project to ARC, and I don't understand if I have to use strong or weak for IBOutlets. Xcode do this: in interface builder, if a create a UILabel for example and I connect it with assistant editor to my ViewController, it create…
Piero
  • 9,173
  • 18
  • 90
  • 160
28
votes
4 answers

Why isn’t my weak reference cleared right after the strong ones are gone?

I am a little bit stubborn, but I want to understand weak and strong references well, so that's why I'm asking you once again. Consider this: __weak NSString* mySecondPointer = myText; NSLog(@"myText: %@", myText); The result is myText: (null)…
14
votes
2 answers

How do weak and strong references look like in objective-c?

Wikipedia states "In computer programming, a weak reference is a reference that does not protect the referenced object from collection by a garbage collector". How do those two types of references look like in code? Does a weak reference is a…
Centurion
  • 14,106
  • 31
  • 105
  • 197
14
votes
3 answers

What is difference between self.timer = nil vs [self.timer invalidate] in iOS?

Can anyone explain me self.timer=nil vs [self.timer invalidate]? What exactly happens at the memory location of self.timer? In my code self.timer=nil doesn't stops the timer but [self.timer invalidate] stops the timer. If you require my code I…
Nasir
  • 1,617
  • 2
  • 19
  • 34
14
votes
4 answers

NSTimer memory management

When I execute this code: [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(showButtons) userInfo:nil repeats:NO]; do I need to nil it or release it, ot whatever for memory management? I am using ARC
Alessandro
  • 4,000
  • 12
  • 63
  • 131
10
votes
1 answer

Swift Weak Reference Much Slower than Strong Reference

I'm building a physics engine in Swift. After making some recent additions to the engine and running the benchmarking tests I noticed the performance was drastically slower. For example, in the screenshots below you can see how the FPS dropped from…
10
votes
2 answers

Objective-C: Weak attritube don't work as expected

Possible Duplicate: Why do weak NSString properties not get released in iOS? I'm a newbie to Objective C and I've got some questions that I cannot answer myself. I have a block of code for testing __weak variable (I'm using ARC, of course):…
hoang Cap
  • 704
  • 6
  • 18
7
votes
3 answers

Swift - Expecting a leak after strongly capturing self in closure

Can anyone please explain why this doesn't leak? I'm capturing self within a closure so I would have two strong pointers pointing at each other, therefore, the deinit message shouldn't ever be called for the Person object. First, this is my class…
RGML
  • 3,429
  • 2
  • 22
  • 18
7
votes
3 answers

Strong and weak modifiers with ios interface elements

in my projects I don't use Interface Builder and I've noticed one thing that I don't know how to explain. Yet. So, to the point. When we are using IB and defining elements of user interface like UILabel or UIButton in our controller we use this ugly…
pawel.kalisz
  • 1,246
  • 1
  • 18
  • 26
6
votes
1 answer

Does ARC hold a count for unowned reference?

Is it true that ARC keeps a count of unowned references to an object? So, if the strong reference count of an object reaches 0 and the unowned reference count of that object is > 0 the object is de-initialized but not de-allocated? And only when the…
6
votes
3 answers

How NSMapTable works

I'm trying to figure out how NSMapTable works So I'm trying in playground the following code: class Person { var name: String init(name: String ) { self.name = name print("\(name) is being initialized") } deinit { …
emacos
  • 541
  • 1
  • 8
  • 17
5
votes
3 answers

Is it possible to hook objects being collected by GC?

Suppose I have a WeakReference of a target strong reference. I'd like to be informed when the target object itself is being collected by the GC. Is it possible? EDIT: Adding code to the finalizer/destructor is not an option here. I need something…
ceztko
  • 14,736
  • 5
  • 58
  • 73
5
votes
1 answer

Property Initialization with closures

I was looking into ARC and strong reference cycles and ran into this code of mine: class TestClass: UIView { let button: UIButton = { let view = UIButton() view.frame = CGRect(x: 50, y: 50, width: 200, height: 200) view.backgroundColor…
Sasha Kolsky
  • 432
  • 1
  • 5
  • 14
5
votes
1 answer

Confusion about where should put the [unowned self]

I have a retained cycle so my viewcontroller's deinit won't be called, and I'm trying to resolve this my adding [unowned self], but I'm not too sure where to put the unowned in my cases: Case 1 class YADetailiViewController: UIViewController { var…
Kesong Xie
  • 1,316
  • 3
  • 15
  • 35
1
2 3 4 5 6