Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-01-2011, 01:26 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 2
SiriusBlack is on a distinguished road
Default Draw/Paint Line with Collision (Memory Leak - you are annoying yet justified)

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.
SiriusBlack is offline   Reply With Quote
Reply

Bookmarks

Tags
collision, draw, line, memory leak, paint

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 338
5 members and 333 guests
Dnnake, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:17 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0