How to implement CHCircularBuffer in iOS project?
Asked Answered
T

1

7

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?

Thumping answered 21/6, 2011 at 11:52 Comment(1)
FYI, CHDataStructures.framework is originally from: cocoaheads.byu.edu/code/CHDataStructuresArleta
T
3

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.

Telescopium answered 30/6, 2011 at 10:30 Comment(2)
can u zipp it up for me and upload it?Thumping
please, see dl.dropbox.com/u/29366847/provaUtility.zip, check MainViewController. showInfo (click on the info button) adds to the ring, flipsideViewControllerDidFinish (click on the done button) logs allObjects count. The If you have any more questions, feel free...Telescopium

© 2022 - 2024 — McMap. All rights reserved.