FAQ Database Discussion Community


Block leak with __block variable

ios,objective-c,memory-leaks,objective-c-blocks,weak-references
I have a big memory leak that I have pinpointed to happen in/on requestContentEditingInputWithOptions: method. If I understand it right it happens with the img variable. If I make it __block __weak the image is nil already after I assign it (img = [UIImage...]). Am I being silly somewhere? Or...

C++ weak_ptr.lock() segfault

c++,qt,weak-references
So I have this function to add listeners and it converts shared pointers of a class so that I can call it later when I get a notification. void registerListener(std::shared_ptr<T> listener) { if (!listener) { qCWarning(OBSERVER_LOGGER) << "Attempted to register a null observer."; return; } // TODO make a foreach...

In Objective C, When is weak object released under ARC?

objective-c,automatic-ref-counting,weak-references
Since the reference count becoming 0 will cause the object being released. I am curious when is a weak object released since the count is always 0, when I declare a weak one, do I need to worry about it will be released half way? For example NSObject ClassA @property...

Swift delegation - when to use weak pointer on delegate

swift,delegates,weak-references
Can someone explain when and when not to use a 'weak' assignment to a delegate pointer in Swift, and why? My understanding is that if you use a protocol that is not defined as a class you cannot, nor want to, assign your delegate pointer to weak. protocol MyStructProtocol{ //whatever...

Weak Self in Blocks

ios,objective-c,weak-references,weak-ptr
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...

Weak References stays alive

c#,garbage-collection,weak-references
I have an interface: IRemoteDataChangedListener public interface IRemoteDataChangedListener<TData> { void DataReceived(TData newData); } And a class, RealtimeEventService public class RealtimeEventService : IRealtimeEventService { private readonly IEventListener listener; private readonly List<Tuple<Type, WeakReference>> dataCreated; public RealtimeEventService(IEventListener eventListener) { this.dataCreated = new List<Tuple<Type, WeakReference>>(); this.listener = eventListener; this.listener.EventReceived...

Weak reference to closure in Swift

swift,closures,weak-references
I have the following code to create an observable property for data binding. It's in the works so I'm not sure what the final implementation is going to be and I'm still pretty new to Swift. class Observable<T> { typealias Observer = T -> Void var value: T { didSet...

Why is the implementation of events in C# not using a weak event pattern by default?

c#,.net,events,weak-references
This question may lead to speculative answers but I presume there's a well thought design decision behind the implementation of event in c#. The event pattern in c# keeps the subscriber alive as long as the publisher of the event is alive. Thus, if you don't unsubscribe, you're leaking memory...

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

ios,swift,dictionary,weak-references
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]...

Is WeakReference what I need?

java,caching,weak-references
I am developing an ordering system and here is my scenario: I have a class that holds the new orders in such like a "caching" strategy. At this time, I am using a HashMap like this: public static final Map<Long, Order> orders = new HashMap<Long, Order>(); Where I identify an...

Objective C - Self Zeroing weak pointer unexpected behaviour

objective-c,automatic-ref-counting,weak-references
I have recently upgraded from Mavericks to Yosemite and now my unit tests are failing. The problem boiled down to a typo in a weak pointer to string content. Please see the following sample code: NSString* value1; NSString* value2; __weak NSString* weakValue1; __weak NSString* weakValue2; NSMutableString* resultText = [NSMutableString new];...

“__weak MyClass *myWeakClass = self” using myWeakClass VS using self

ios,objective-c,self,weak-references
I am trying to learn AVFoundation sample code "AVCam". In the sample code project, AVCamViewController.m class, there is an implementation like __weak AVCamViewController *weakSelf = self; [self setRuntimeErrorHandlingObserver:[[NSNotificationCenter defaultCenter] addObserverForName:AVCaptureSessionRuntimeErrorNotification object:[self session] queue:nil usingBlock:^(NSNotification *note) { AVCamViewController *strongSelf = weakSelf; My question is why we want to first declare a...

Do I need to check a WeakReference inside an AsyncTask?

android,android-asynctask,weak-references
I'm using the following pattern taken from the Android developers docs: class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> { private final WeakReference<ImageView> imageViewReference; private int data = 0; public BitmapWorkerTask(ImageView imageView) { // Use a WeakReference to ensure the ImageView can be garbage collected imageViewReference = new WeakReference<ImageView>(imageView); } // Decode...

AlertDialog NullPointerException in Android source code

java,android,garbage-collection,android-alertdialog,weak-references
A user reported a NullPointerException happening on the line of onClick of my AlertDialog: new AlertDialog.Builder(this) .setTitle(getString(R.string.dialog_error)) .setMessage(getString(R.string.create_failed_error)) .setCancelable(false) .setPositiveButton(getString(R.string.dialog_got_it), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // #L286 dialog.cancel(); mButton.render(); } }) .create().show(); The error is thus rooted in the android.internal.app.AlertController on Line...

weak self in gcd in swift closure

swift,closures,grand-central-dispatch,weak-references
apiFunc(user: User.currentUser, start: 0, limit: Constants.numberOfItemInOnePage, success: { [weak self] (friends) -> Void in dispatch_async(dispatch_get_main_queue(), { [weak self] () -> Void in if let strongSelf = self { strongSelf.friendList = friends strongSelf.loading = false strongSelf.tableView.reloadData() } }) }, failure: nil) error Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1 happens...

Giving my class a static getInstance(Context)

java,android,weak-references
Say I have a class that wants to provide instances of itself via a static method. The instances require the use of a Context, so the method will be called like this: Foo foo = Foo.getInstance(context); I'm thinking of doing it like this: public class Foo { private static final...

swift: weak cannot be applied to an outlet

ios,swift,weak-references,iboutlet,zbar-sdk
I am using libzBar for scanning QRCode in swift but I am getting these errors. . First error is about weak referencing and second is undeclared type. At first I thought I haven't included the ZBarReaderView.h but after checking the file Inspector I found that the files are correctly added...

Convert Obj C code to Swift weak self

objective-c,swift,uipageviewcontroller,weak-references,self-reference
I am using the code here but I'm having difficulty to convert the __weak __typeof(self) weakSelf = self; to Swift. Is there a way for me to convert the following code into Swift? I'm stucked convert to Swift even after reading the material said that [unowned self] or [weak self]....