for my game iOS project I need a ring buffer. It should work similar to a queue where elements go out and go in but the total amount of elements in the buffer should stay the same.
I implemented the ring buffer successfully using java but I am not so familar with objective-c. I found a ring buffer implementation on the web called CHCircularBuffer: https://bitbucket.org/devartum/chdatastructures/src/4d6d7194ee94/source/CHCircularBuffer.m However I failed implementing it correctly.
The circular buffer is a property of a class called TerrainManager which does all the mathematical terrain generation.
@interface TerrainManager : NSObject{
int terrainParts;
CHCircularBuffer* circularTerrainBuffer;
}
@property(nonatomic, retain) CHCircularBuffer *circularTerrainBuffer;
@end
This how the ring buffer is initialized in the implementation of TerrainManager
circularTerrainBuffer = [[CHCircularBuffer alloc] initWithCapacity:parts];
This creates an instance of the buffer and sets the size property to parts. Now I add objects to the buffer using addObject method:
[circularTerrainBuffer addObject:[NSNumber numberWithDouble:0.2]];
Sometimes this line receives an error "exec_bad_access". E.g. when I init the buffer with capacity of 15 everything is fine, with 20 I get the error.
I now try to access the buffer from the terrain class where the drawing takes place. But whenever I try to access the objects I get an "bad_access" error.
NSArray *arr = [terrainManager.circularTerrainBuffer allObjects];
E.g. this line would create the error.
So there is something wrong with my code. Maybe I dont understand the buffer and add objects the wrong way. I dont know. Any ideas or suggestions?