Saving a static array leads to leakage of an unhealthy project?

I got the test code below.
(In this example, I came across the fact that the interface cannot be statically distributed without saving.)

Thanks to this code block, I realized what was really saved.
I want to be sure if this leads to a leak, and I have to let it go elsewhere. I just don’t want to reinitialize the array every time. and made it static (lack of memory, but speed advantage)

Should I free this stored static array somewhere? is it safe code or am i completely removing static and saving words and just initializing the arrayObjects method classically? so what can you prefer for me?

-(NSUInteger)getCoordYByX:(int)ax { NSUInteger ret_=-1; static NSArray *coordsX=nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ coordsX=[[NSArray arrayWithObjects: [NSNumber numberWithInt:50], [NSNumber numberWithInt:170], [NSNumber numberWithInt:190], [NSNumber numberWithInt:210], [NSNumber numberWithInt:350], nil]retain]; /*it is more longer. cropped for test purposes*/ }); ret_=[[coordsX objectAtIndex:ax] unsignedIntegerValue]; return ret_; 

}


Finally:
why does static not support the initial values ​​of the array? and
What if I want to use persistence, a memory leak may occur or not?
+4
source share
2 answers

This is one way to declare a global, immutable array of objects.

This is not a leak in the sense that distribution is not inaccessible memory. To take this example further (read: stupidity), your program could -retain all of these objects, and you still would not have a leak by the definition of unreachable memory. The array is initialized exactly once and never reassigned, so the original array will always be available to your method, and its elements are always reachable through the array.

However, this is memory that cannot be restored until the program terminates (provided that the method body is entered). So this is really not a design that I would recommend (in fact, I avoid this design).

A better alternative would look more:

 + (NSUInteger)coordYByX:(int)ax { enum { NCoordsX = 5 }; const NSUInteger CoordsX[NCoordsX] = { 50,170,190,210,350 }; assert(ax < NCoordsX && "invalid index"); // handle error as you handle errors return CoordsX[ax]; } 

This requires zero heap allocations, zero static storage, and zero locks;)

+1
source

static just turns your variable into state until your application exits. static declaration will not leak. I think there is a chance here to get a leak in your code. So,

 [[NSArray arrayWithObjects: [NSNumber numberWithInt:50], [NSNumber numberWithInt:170], [NSNumber numberWithInt:190], [NSNumber numberWithInt:210], [NSNumber numberWithInt:350], nil]retain]; 

equally

 [[[[NSArray alloc] initWithObjects: [NSNumber numberWithInt:50], [NSNumber numberWithInt:170], [NSNumber numberWithInt:190], [NSNumber numberWithInt:210], [NSNumber numberWithInt:350], nil] autorelease] retain]; 

Thus, your save message will create a leak.

+1
source

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


All Articles