Questions tagged [weak]
128 questions
3
votes
1 answer
__weak and autorelease pool in ARC in Xcode 4.2
My project use ARC. I tested with the code below:
NSString __weak *string;
@autoreleasepool {
string = [NSString stringWithString:@"AAA"];
}
NSLog(@"string: %@", string);
I think it output as:
string: (null)
but it actually…

vietstone
- 8,784
- 16
- 52
- 79
3
votes
1 answer
Swift 5.7: is `guard let self` enough now?
I used to write stuff like this before Swift 5.7:
// random async completion block
{ [weak self] in
guard let weakSelf = self else { return }
weakSelf.someString = ""
}
Now I would prefer writing this:
// random async completion block …

JamesDerrick248
- 101
- 4
3
votes
2 answers
What is difference between "[self] in" & "[weak self] in" in Swift?
I just found out that I can write "[self]" in the closures instead of [weak self] in but I'm not sure if it is safe or not.
more detail:
Before:
func roundShape(corners: CACornerMask, radius: CGFloat) {
DispatchQueue.main.async { [weak…

Ahmadreza
- 6,950
- 5
- 50
- 69
3
votes
1 answer
Weak reference not getting garbage collected?
I was studying about Weak references. And I understood that all weak references WILL be garbage collected before OutOfMemoryError occurs. I had a simple test something like this (I know catching OOME is not good but just a test) :
Integer weakInt =…

Basith
- 831
- 1
- 7
- 14
3
votes
1 answer
Assign And Weak
I want to look a difference between assign and weak.So I run this code below:
@interface Test : NSObject
@property(nonatomic, strong) NSString *str;
@property(nonatomic, assign) NSString *assignString;
@property(nonatomic, weak) NSString…

chengwen.Y
- 33
- 5
3
votes
3 answers
NSHashTable weakObjectsHashTable – added object is not zeroed
I'm trying to understand how ARC and NSHashTable weakObjectsHashTable works. The objects I add to the hash table should be removed/zeroed or whatever they become once the object has been deallocated. The code example below in its NSLog shows that…

StackUnderflow
- 2,403
- 2
- 21
- 28
3
votes
0 answers
Is it possible to have a weak enum?
I'm using gcc, and I am distributing an object file which may be used in conjunction with some third party code. I have something that looks like this:
void fn3rdParty(int bar) __attribute ((weak));
void fn(void) {
if(fn3rdParty)
…

John
- 3,400
- 3
- 31
- 47
2
votes
1 answer
objective c to swift conversion. (#define and weak)
I am stuck in converting #define and weak from objective c to swift. I tried to use objective c to swift convertor, but I think the result of conversion is not correct.
#define WeakRef(__obj) __weak typeof(self) __obj = self
#define…

Pak Ho Cheung
- 1,382
- 6
- 22
- 52
2
votes
1 answer
Retain Cycle on Class Methods Blocks
As far as I know when we work with blocks we must create a __weak instance of the object running the method with the code, and then a __strong one to keep the weak alive:
__weak __typeof(self) weakSelf = self;
[self setHandler:^{
__strong…

Pablo A.
- 2,042
- 1
- 17
- 27
2
votes
4 answers
boost::shared_ptr cycle break with weak_ptr
I am currently in a situation like:
struct A {
shared_ptr b;
};
struct B {
shared_ptr a;
};
//...
shared_ptr a(new A());
shared_ptr b(new B());
a->b(b);
b->a(a);
I know this won't work, because the references would…

PrettyPrincessKitty FS
- 6,117
- 5
- 36
- 51
2
votes
1 answer
weak property gives me nil when i'm trying to access it swift
i have these classes :
class Song {
var title : String = ""
weak var album : Album?
init() {
self.album = Album()
}
}
and
class Album {
var title : String = ""
var Songs : Array < Song > = []
deinit {
…

Mohammadalijf
- 1,387
- 9
- 19
2
votes
0 answers
GCC / weak symbol function: Why does this segfault?
For unit testing I'd like to replace a function from "outside". Normally, I'm using the wrapping mechanism - but unfortunately this does not work for calls to the function from within the same compilation unit.
My idea was to mark the function as…

trapperjohn
- 49
- 4
2
votes
1 answer
.weak assembler macro for MinGW GCC / C alternative
I have a modular C-Project with some different libraries/objects, which are only linked to the release if some conditions are met. This project is build with GCC 3.4.4 for PowerPC.
So all over my project there are some C-Files which use functions…

Toby
- 3,815
- 14
- 51
- 67
2
votes
3 answers
Can weak symbol be resolved among libraries during linking?
My scenario is about cross-compiling to a Arduino Due (ARM target), but I guess it's a generic C weak symbol problem.
I want to break my firmware into 3 parts:
1. The hardware library (CMSIS, Middleware) -> libHardware.a
2. Realtime OS library ->…

HelloSam
- 2,225
- 1
- 20
- 21
2
votes
1 answer
ios __weak and __autoreleasing
I got some questions after reading the answer to this thread Handling Pointer-to-Pointer Ownership Issues in ARC;
For demo 1, when a reference to a 'strong' attributed variable is passed, a hidden(__autoreleasing) variable is implicitly created and…

Dirk Zhou
- 51
- 3