好吧,我在这里碰壁了。
我不知道为什么我的 Xcode 4.5.2 说我在崩溃前使用了 4.16 GB 内存:
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
我使用 Instruments - Leaks & Allocations 运行了我的 OpenGL ES 应用程序。它显示我使用的分配内存总量为 1.46 MB Live Bytes。
我正在尝试遵循 Ian Terrel 的 OpenGL ES 教程:
http://games.ianterrell.com/how-to-draw-2d-shapes-with-glkit-part-2/
我被EERegularPolygon
课程的最后一部分困住了(所有以前的形状,如三角形、矩形、椭圆都工作得很好)。我的代码是这样的(与他的略有不同,因为我使用的是 Xcode 4.5.2 的 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;
}
任何人都知道为什么 Xcode 报告试图分配 4.16 GB 的内存?
我也尝试为该类手动编写 get 和 set 方法,EERegularPolygon
但这也没有用。