Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7523] iOS: Use blocks and remove "return caches" from code

GitHub Issuen/a
TypeBug
PriorityMedium
StatusClosed
ResolutionFixed
Resolution Date2012-02-14T23:29:01.000+0000
Affected Version/sRelease 1.8.0.1
Fix Version/sSprint 2012-03, Release 2.0.0, Release 1.8.2
ComponentsiOS
Labelsn/a
ReporterStephen Tramer
AssigneeBlain Hamon
Created2012-02-02T13:59:50.000+0000
Updated2012-02-14T23:29:01.000+0000

Description

Throughout the code there are functions which need to be performed on the main thread, but still return a value (mostly in Contacts and Media, although there may be others). Now that we have blocks, we can instead use local variables with the __block specifier and TiThreadPerformOnMainThread to clean this up. Using the return cache leads (and has always led to) overretaining memory. Blain's fix for TIMOB-1542 demonstrates a reasonable pattern for this, but rather than checking [NSThread isMainThread] and then calling back out to the function, we should instead be putting main thread critical sections within the block itself, i.e.
-(id)getPropertyFromMainThread
{
  __block id result = nil;
  TiThreadPerformOnMainThread(^{
    // Critical code to get value for 'result' here...
    [result retain];
  }, YES);
  return [result autorelease];
}
Note the retain/autorelease pattern being necessary, as if the block is performed on the main thread, it sets up its own autorelease pool and as a result may point back to a bad memory block.

Comments

  1. Blain Hamon 2012-02-07

    The pull request for 1542 will include the commits fixing this bug as well.
  2. Thomas Huelbert 2012-02-14

    no way for qe to confirm, closing

JSON Source