Unexpected EXC_BAD_ACCESS when setting a weak property in the global BG global point

In my ChatMessage class, I have a weak chat class reference

@interface ChatMessage : NSObject

@property (nonatomic, weak) Chat *chat;

I am doing the following initialization in implementing a chat in a global queue

ChatMessage *chatMessage = [[ChatMessage alloc] initWithDictionary:dictionary];
chatMessage.chat = self;

and get a very strange error in the second line

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x4c008be5
Triggered by Thread:  17

Thread 17 Crashed:
0   libobjc.A.dylib                 0x39f535d2 cache_getImp + 18
1   libobjc.A.dylib                 0x39f4e9a8 lookUpImpOrForward + 28
2   libobjc.A.dylib                 0x39f4e942 lookUpImpOrNil + 22
3   libobjc.A.dylib                 0x39f48aca class_getMethodImplementation + 30
4   libobjc.A.dylib                 0x39f5833a weak_register_no_lock + 38
5   libobjc.A.dylib                 0x39f586fa objc_storeWeak + 106
6   MyMessenger                     0x000d366c -[ChatMessage setChat:]
7   MyMessenger                 0x001173bc -[Chat getMessagesForPage:]
8   libdispatch.dylib               0x3a432d50 _dispatch_call_block_and_release + 8
9  libdispatch.dylib                0x3a438684 _dispatch_root_queue_drain + 224
10  libdispatch.dylib               0x3a4388d8 _dispatch_worker_thread2 + 52
11  libsystem_pthread.dylib         0x3a563c14 _pthread_wqthread + 296
12  libsystem_pthread.dylib         0x3a563ad8 start_wqthread + 4

Has anyone encountered such problems when setting weak properties?

+4
source share
1 answer

It seems like the problem is accessing this weak property from different threads simultaneously.

The weakness of the property does not allow to neglect multithreaded appointment.

A safe rule for working with data sources is to synchronously change it within the same selected stream.

+1
source

Source: https://habr.com/ru/post/1542788/


All Articles