Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement CHCircularBuffer in iOS project?

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?

like image 968
UpCat Avatar asked Jun 21 '11 11:06

UpCat


People also ask

How would you implement a circular buffer?

Circular Buffers can be implemented in two ways, using an array or a linked list. An empty object array along with its capacity is initialized inside the constructor as the type of elements added is unknown. Two pointers namely head and tail are maintained for insertion and deletion of elements.

How does a circular buffer work?

A circular buffer is a utility used to transfer successive data values from a producer thread to a consumer thread, who retrieves the data in FIFO (first in first out) order. This kind of data structure will be used when pipelining threads, a subject discussed in detail in Chapter 15.

What is circular buffering in OS?

An area of memory or a dedicated hardware circuit that is used to store incoming data. When the buffer is filled, new data are written starting at the beginning of the buffer. Circular buffers are typically used to hold data written by one process and read by another.


1 Answers

The snippets of code you are showing are correct. I implemented a small project to test CHCircularBuffer the way you specify and it works correctly. So, the problem must be somewhere else.

The only way around this is, IMHO, put a breakpoint on the line that fails and step into the addObject function to see where exactly it fails. The array could be reallocated in there, so may be this is failing and giving the bad access. The same for allObjects.

Anyway, I have to say that I could execute my test without any issue, adding objects, removing them from head and tail, and getting the array of all objects with no issues.

If you post more code, we can maybe help a bit more.

like image 76
sergio Avatar answered Sep 24 '22 12:09

sergio