Hi there,
I am a kinda new to iPhone development in a sense and not (hard to explain).

Anyway, something puzzles me, I have been trying for days to solve this - and I can't seem to do so, so I am checking if any of you guys can spin this around your brains and see if you can assist in anyway - as you may be better at this sort of thing.
What's the problem? Memory Management (for chipmunk - I believe this is a general memory management problem though - not specific to only chipmunk). - It's all the more annoying because I thought I was pretty good at memory management - darn!
So yes, if you don't know Chipmunk, it is a game physics engine written in C. So that's about it, all the same memory management standards apply etc, unfortunately this one confuses me. (even if you don't know chipmunk, you may know how I'd get out of this conundrum).
I must deserve this memory leak through poor implementation of my code (usually the case in regards to memory leaks) - though I'd be ecstatic to know how to get around it.
So here are the relevant parts of my code:
Code:
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Began");
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [ touches anyObject];
CGPoint new_location = [touch locationInView: [touch view]];
new_location = [[CCDirector sharedDirector] convertToGL:new_location];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
//begin drawing to the render texture target
[targetRender begin];
//draw smoothly from the last position and vary the sprite's scale/rotation/offset
float distance = ccpDistance(new_location, oldTouchLocation);
if(brushFlag) {
if (distance > 1)
{
int d = (int)distance;
for (int i = 0; i < d; i++)
{
float difx = oldTouchLocation.x - new_location.x;
float dify = oldTouchLocation.y - new_location.y;
float delta = (float)i / distance;
[brush setPosition:ccp(new_location.x + (difx * delta), new_location.y + (dify * delta))];
[brush setRotation:rand()%360];
float r = ((float)(rand()%50)/100.f); //float r = ((float)(rand()%50)/50.f)+ 0.25f;
[brush setScale:r];
// Call visit to draw the brush
[brush visit];
}
}
}
// finish drawing and return context back to the screen
[targetRender end];
staticBody = cpBodyNew(INFINITY, INFINITY);
shape = cpSegmentShapeNew(staticBody, new_location, oldTouchLocation, 0.0f);
shape->e = 1.0f; shape->u = 0.01f;
cpSpaceAddStaticShape(space, shape);
and in the header file:
Code:
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// Importing Chipmunk headers
#import "chipmunk.h"
// HelloWorld Layer
@interface HelloWorld : CCLayer
{
float totalDistanceOfPoints, totalIter;
CCSprite *floor, *ball, *ball2, *brush;
cpSpace *space;
float *coords;
cpShape *shape;
cpBody *staticBody;
CCProgressTimer *timey;
CCMenu *menu;
CCMenuItem *Back;
CCMenuItem *Reset;
CCMenuItemFont *Play;
CCRenderTexture *targetRender;
bool brushFlag;
bool shapeFlag;
}
-(void)tick:(ccTime*)timer;
void updateShape(void *ptr, void *unused);
-(void)setupChipmunk;
-(void)flush;
-(void)startSim;
-(void)resetGame;
-(void)back:(id)sender;
//-(void) registerWithTouchDispatcher;
@end
I continually get a memory leak from the TouchesMoved clearly. It would appear the 'NEW' objects I am creating are the memory leaks I am creating, as they are being called again and again and again. (with no release).
Yet, how else am I supposed to be able to have a collidable line?
Or more importantly when am I supposed to release these objects?
As, Chipmunk has functions to use to release memory, such as cpShapeFree/cpBodyFree etc. But I can't call them right after I create these in the touches moved as the collision obstacle becomes no more (the objects upon the shape simply fall through and no collision shape is created).
I can provide more information if necessary, I am having stupendous amounts of issues with this.
Any help would most definately be exceedingly appreciated for this horrendous problem that I am unable to think through for some reason.
Thank you so much in advanced.
P.S
I can't use - @property (nonatomic,retain) - to assist with memory management on these Chipmunk structs either as they can't be retained or copied.
I just seem to have no idea when and where I should release these objects effectively without the leaks and still utilise the collision the drawn line creates.