Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7606847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:30:42+00:00 2026-05-31T00:30:42+00:00

I have some piece of code: //in the interface file //… NSMutableSet * someSet;

  • 0

I have some piece of code:

//in the interface file
//...
NSMutableSet * someSet;    
//...
//

//in the implementation file
//-(id)init {
   if(self = [super self])
   {
       someSet = [[NSMutableSet alloc] init];
   }
   return self;
}    
-(void)someFunc
{
    NSLog(@"someSet count: %lu", [someSet count]); //always shows some number
    array = ... //getting array from another function
    for(SomeObject * obj in array)
    {
         NSSomeOperation * op = [[NSSomeOperation alloc] initWithValue:[obj someValue]];
         //start operation
         [someSet addObject:[obj someValue]];
    }  
}
//this function is called from another thread (from operation) through notification
// 
-(void)someAnotherFunc
{
    @synchronized(self)
    {
        int size = [someSet count]; //HERE size = 0, but it must be some count
        NSLog(@"someSet: %@", someSet); // it outputs: "someSet: {()}"
        NSLog(@"someSet count: %lu", [someSet count]); // it outputs: "someSet count: (null)"
    } 
}

The problem is, that size = 0 in someAnotherFunc, but it must be some count. And I know, it’s because someAnotherFunc is called from another thread, and in that thread someSet = nil.

I’ve tried performSelectorOnMainThread but it doesn’t help.

UPDATE:

I’ve debugged this, I’ve called twice someFunc, and someSet is valid object during these calls, and have some count of objects. I’ve logged count in someFunc – and outputs valid count of objects.

It only occurs when is ARC enabled, before ARC was enabled – everything was fine.

It seems it is some security from threads or ARC, to prevent to use data from different threads. Maybe I’m not right, but I don’t understand why it occurs.

During execution of this piece of code someSet is not directly assigned to nil. And it is not deallocated.

Does anyone know how to access object someSet from another thread? How to get valid count of objects in someSet?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:30:43+00:00Added an answer on May 31, 2026 at 12:30 am

    There is nothing thread related wrong with that code (other than that you are going to run into a slew of unrelated problems by trying to do threading in this fashion).

    Somewhere, someSet is being set to nil before the other thread’s code is run. You say this only happens with ARC enabled? Then that is most likely because you don’t have a strong reference to whatever object encapsulates that code; i.e. the object is being deallocated because the compiler has determined it is no longer being used.

    This is most likely because your code that handles the notification is not set up to retain the observer — to keep a reference to the observer — around. Stick a reference to the observer in a global variable, directly or indirectly.

    Note that the @synchronize() in that code is likely nonsense or, if you really do have multiple threads that might be firing the notification, indicative of an architectural issue.

    Note also that NSLog(@"someSet count: %@", [someSet count]); // it outputs: "someSet count: (null)" will crash if someSet were ever properly set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If have a piece of code that gets some data from a sql database
I have a piece of code here that i really could use some help
I have got a piece of code, that should countdown some number (in this
I have written some piece of code for my program in Matlab 7.10.0 which
I have some piece of code which goes like this: pipe(fd); child_map[0] = fd[0];
I have a piece of dissassembled code which didn't work quite well around some
I'm writing some code to interface with a piece of hardware. The hardware connects
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some C# / asp.net code I inherited which has a textbox which
I have some code like this in a winforms app I was writing to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.