Muhammad Umar
Muhammad Umar

Reputation: 11782

Calling an object into memory at same time though different functions

Suppose I have a MutableArray myArray with 10 Objects.

Each Object is a View with 10 differnt subViews.

Now suppose I have two NSTimers set or I am receiving some notifications from server. Each function have a code or a loop that calls objects of myArray.

Now suppose at an instant of time xxxxx an object myTField is being called by both functions and both try to setText on it. Keep in mind that the time instant is same, Will it cause memory leak issues or how will it effect the object, also if not which value will be set accordingly.

Upvotes: 1

Views: 53

Answers (2)

justin
justin

Reputation: 104708

UIView, as others have pointed out, is generally not intended to be used from multiple threads. It is a bad type to use for this example. Let's fix that, and instead suppose that the array contains NSMutableStrings. Let's also suppose that your intent was to say that the objects are modified by separate threads at the same time.

That said; Unless the object is declared to be thread safe for this purpose, you must assume it is a race condition and Undefined Behavior.


Noting some obvious things:

  • Reference Count operations are atomic (not data races)
  • Accesses to atomic properties are not data races

Upvotes: 2

Wain
Wain

Reputation: 119031

As you can only (or certainly should only) update the text field on the main thread, whichever is run second will set the text. There should be no leak issues.

If your code isn't running on the main thread when it modifies the text field, change it so it is.

Upvotes: 0

Related Questions