OK, I’m hitting a wall here.
I don’t know why my Xcode 4.5.2 is saying I am using 4.16 GB of memory before crashing:
ExampleEngine(11672,0xac70f2c0) malloc: *** mmap(size=4160753664) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
I ran my OpenGL ES application using Instruments – Leaks & Allocations. It shows that the total amount of allocation memory I used was 1.46 MB Live Bytes.
I am trying to follow an OpenGL ES tutorial by Ian Terrel:
http://games.ianterrell.com/how-to-draw-2d-shapes-with-glkit-part-2/
I am stuck of the last part the EERegularPolygon class (all the previous shapes like Triangle, Rectangle, Elipse worked fine). My code is like this (slightly different to his because I am using Xcode 4.5.2’s auto @synthesize):
// EERegularPolygon.h file
#import "EEShape.h"
@interface EERegularPolygon : EEShape
@property (readonly) int numSides;
@property (nonatomic) float radius;
-(id)initWithNumSides:(int)numSides;
@end
// EERegularPolygon.m file
#import "EERegularPolygon.h"
#define M_TAU (2 * M_PI)
@implementation EERegularPolygon
-(id)initWithNumSides:(int)numSides
{
self = [super init];
if(self)
{
_numSides = numSides;
}
return self;
}
-(void)updateVertices
{
for(int i = 0; i < self.numSides; i++)
{
float theta = ((float) i) / self.numSides * M_TAU;
self.vertices[i] = GLKVector2Make(cos(theta) * self.radius, sin(theta) * self.radius);
}
}
-(void)setRadius:(float)radius
{
_radius = radius;
[self updateVertices];
}
@end
// HexagonScene.h file
#import "EEScene.h"
#import "EERegularPolygon.h"
@interface HexagonScene : EEScene
{
EERegularPolygon *polygon;
}
@end
// HexagonScene.m file
#import "HexagonScene.h"
@implementation HexagonScene
-(id)init
{
self = [super init];
if(self)
{
polygon = [[EERegularPolygon alloc] initWithNumSides:6];
polygon.radius = 1;
}
return self;
}
-(void)render
{
[super render];
[polygon render];
}
@end
// AppDelegate DidFinishLaunchingWithOptions: method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:context];
GLKView *view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:context];
view.delegate = self;
GLKViewController *controller = [[GLKViewController alloc] init];
controller.delegate = self;
controller.view = view;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
scene = [[HexagonScene alloc] init];
scene.left = -3;
scene.right = 3;
scene.bottom = -2;
scene.top = 2;
scene.clearColor = GLKVector4Make(0.25, 0.25, 0.25, 1.0);
return YES;
}
Anyone got any ideas why Xcode is reporting 4.16 GB of memory was trying to be allocated ?
I tried manually writing the get and set method for the EERegularPolygon class too but that also didn’t work.
OK, I have magically stumbled upon the solution after trying to debug the problem.
First and foremost, the two changes:
1) I forgot to override the following method:
2) The one single line that stopped the app from crashing and show the hexagon shape was this NSLog() line:
There must be something I don’t understand about #define preprocessor directive not defined until it’s actually called in the above line of code.
Can any C or Objective C guru please explain ?
I’ll dig Google too to see if I can get an understanding why a #define fails to work until a NSLog is called in my above code.
Unless it’s a bug with the compiler or Xcode, otherwise it seems absurd.
EDIT
OK, further discovery, it’s not that the #define was broken and that I needed to call the M_TAU constant like so in the init:
It seems I need to have a line of NSLog inside my init function for some odd reason, anything.