weak-references

Is this Runnable safe from memory leak?

若如初见. 提交于 2019-12-01 15:48:42
问题 I am a total beginner in Java and have created a simple Java Android snippet where in a Runnable after 1,5 seconds I change the TextView from Hello World to Hola Mundo . It works flawlessly, basically a WeakReference should prevent this memory leak from happening right? I have a doubt if there's absolutely no memory leak whenever device orientation occurs. I would love to check this but can't manage to change orientation in my emulated Android. This is the code: package com.example.helloworld

Why setting object that is undergoing deallocation to weak property results in crash

匆匆过客 提交于 2019-12-01 15:40:38
In Clang's Objective-C Automatic Reference Counting we see the following For __weak objects, the lvalue is updated to point to the new pointee, unless the new pointee is an object currently undergoing deallocation, in which case the lvalue is updated to a null pointer. This must execute atomically with respect to other assignments to the object, to reads from the object, and to the final release of the new pointee. In objc-weak.mm wee see the following chunk of code in weak_register_no_lock() : if (deallocating) { if (crashIfDeallocating) { _objc_fatal("Cannot form weak reference to instance (

Why setting object that is undergoing deallocation to weak property results in crash

自闭症网瘾萝莉.ら 提交于 2019-12-01 13:44:40
问题 In Clang's Objective-C Automatic Reference Counting we see the following For __weak objects, the lvalue is updated to point to the new pointee, unless the new pointee is an object currently undergoing deallocation, in which case the lvalue is updated to a null pointer. This must execute atomically with respect to other assignments to the object, to reads from the object, and to the final release of the new pointee. In objc-weak.mm wee see the following chunk of code in weak_register_no_lock()

Why variable with __weak qualifier retains an object?

♀尐吖头ヾ 提交于 2019-12-01 06:53:24
Here is my code: extern void _objc_autoreleasePoolPrint(); int main(int argc, const char * argv[]) { NSArray __weak *tmp = nil; @autoreleasepool { NSArray __strong *obj = [[NSArray alloc] init]; NSLog(@"obj &: %p", obj); tmp = obj; NSLog(@"tmp &: %p", tmp); _objc_autoreleasePoolPrint(); } NSLog(@"tmp: %@", tmp); // why not (null) ? return 0; } And console output: 2013-05-01 22:14:32.966 SimpleConsoleObjectiveCApplicationWithARC[40660:f07] obj &: 0x7fedf9403110 2013-05-01 22:14:32.969 SimpleConsoleObjectiveCApplicationWithARC[40660:f07] tmp &: 0x7fedf9403110 objc[40660]: ############## objc

Weak property in a Swift protocol may only be a class or class-bound protocol type

半世苍凉 提交于 2019-12-01 05:45:33
I would like to define a protocol which is used in a Viper architecture to establish a connection between a Viper components using a protocol with a weak property but I get the following error message: 'weak' may only be applied to class and class-bound protocol types, not 'Self.ViperViewClass' protocol ViperPresenter: class { associatedtype ViperViewClass weak var view: ViperViewClass! { get set } } Protocols can't currently require properties to be implemented as weak stored properties. Although the weak and unowned keywords are currently allowed on property requirements, they have no effect

Why variable with __weak qualifier retains an object?

拥有回忆 提交于 2019-12-01 04:15:24
问题 Here is my code: extern void _objc_autoreleasePoolPrint(); int main(int argc, const char * argv[]) { NSArray __weak *tmp = nil; @autoreleasepool { NSArray __strong *obj = [[NSArray alloc] init]; NSLog(@"obj &: %p", obj); tmp = obj; NSLog(@"tmp &: %p", tmp); _objc_autoreleasePoolPrint(); } NSLog(@"tmp: %@", tmp); // why not (null) ? return 0; } And console output: 2013-05-01 22:14:32.966 SimpleConsoleObjectiveCApplicationWithARC[40660:f07] obj &: 0x7fedf9403110 2013-05-01 22:14:32.969

How to force full garbage collection in .NET 4.x?

◇◆丶佛笑我妖孽 提交于 2019-12-01 03:20:50
I've a problem with WeakReferences in .NET 4.x, I was running tests to make sure some objects were not referenced anymore (using WeakReferences) and I noticed the behavior is not consistent across framework versions: using System; using System.Text; using NUnit.Framework; [TestFixture] public class WeakReferenceTests { [Test] public void TestWeakReferenceIsDisposed() { WeakReference weakRef = new WeakReference(new StringBuilder("Hello")); GC.Collect(); GC.WaitForPendingFinalizers(); GC.WaitForFullGCComplete(); GC.Collect(); var retrievedSb = weakRef.Target as StringBuilder; Assert.That

strange WeakReference behavior on Mono

五迷三道 提交于 2019-12-01 02:40:35
Testing code that uses WeakReference failed for me using Mono 2.11.3 (SGen) as well as the stable 2.10.8 version. In a simple code like this object obj = new object(); WeakReference wr = new WeakReference(obj); Assert.IsTrue(wr.IsAlive); obj = null; GC.Collect(); Assert.IsFalse(wr.IsAlive); the second assert will fail. Adding GC.WaitForPendingFinalizers doesn't help. Is this a bug in Mono or in my head? Thanks lupus It is not a bug, but an implementation detail where the Mono GC behaves differently from the MS GC. In this case, since you created the object obj in the same stack frame, it

Weak Self in Blocks

耗尽温柔 提交于 2019-12-01 01:03:45
Do I need to check if weak self is nil in blocks? I create weakSelf pointer like: __weak typeof(self) weakSelf = self; and in the beginning of the blocks I do if(!weakSelf){return;} is this unnecessary? or does it depend on whether I coded the rest correctly so that when the self dies, others die too? That check is unnecessary, and is giving you a false sense of security. Here's the problem: __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ if (!weakSelf) { return; } // THE LINE OF INTEREST [weakSelf doSomething]; }); At THE LINE OF INTEREST , some other thread

Can I hook when a weakly-referenced object (of arbitrary type) is freed?

三世轮回 提交于 2019-12-01 00:56:09
I'm writing a container class in Swift, which works like as java.util.WeakHashMap in Java. My current implementation is here. class WeakRefMap<Key: Hashable, Value: AnyObject> { private var mapping = [Key: WeakBox<Value>]() subscript(key: Key) -> Value? { get { return mapping[key]?.raw } set { if let o = newValue { mapping[key] = WeakBox(o) } else { mapping.removeValueForKey(key) } } } var count: Int { return mapping.count } } class WeakBox<E: AnyObject> { weak var raw: E! init( _ raw: E) { self.raw = raw } } In this implementation, holded objects in the container are weakly-referenced via