常数,随机和莫名的(对我来说,反正)EXC BAD目标C中的应用程序访问
问题描述:
我已经自动引用计数和僵尸启用...常数,随机和莫名的(对我来说,反正)EXC BAD目标C中的应用程序访问
我不断收到代码EXC错误访问到不同的点,最的时间没有进一步的信息来自僵尸。
目标是在屏幕上绘制一个矩形,其中包含从图像加载的纹理。它确实有效!但通常它被破坏了(图像和矢量),然后我经常得到不良的访问警告。
我有这种结构...
应用程序委托类声明:
GWBackgroundScene* background;
EAGLContext *context;
GLKView *view;
GLKViewController *controller;
UIWindow *window;
,然后窗被制作成一个属性和合成。
DID的完成启动与选项:
context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
[EAGLContext setCurrentContext:context];
view = [[GLKView alloc] initWithFrame:[[UIScreen mainScreen] bounds] context:context];
view.delegate = self;
controller = [[GLKViewController alloc] init];
[controller shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
controller.delegate = self;
controller.view = view;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
background = [[GWBackgroundScene alloc] initWithImage:[UIImage imageNamed:@"DSC_0059.jpg"]];
的AppDelegate中充当一个OpenGL委托和OpenGL呼叫被叫的类,然后调用[背景呈现]“呈现”功能;
我GWBackgroundScene类:
@interface GWBackgroundScene : NSObject
{
GLKTextureInfo *texture;
NSMutableData* vertexData;
NSMutableData* textureCoordinateData;
}
@property(readonly) GLKVector2 *vertices;
@property(readonly) GLKVector2 *textureCoordinates;
-(void) render;
-(id) initWithImage: (UIImage*)image;
初始化有:
self = [super init];
if(self != nil)
{
texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft] error:&error];
vertexData = [NSMutableData dataWithLength:4];
textureCoordinateData = [NSMutableData dataWithLength:4];
self.vertices[0] = GLKVector2Make(-2.0,3.0);
...
self.textureCoordinates[0] = GLKVector2Make(0,0);
...
}
return self;
,有这两个函数来处理矢量和纹理信息
- (GLKVector2 *)vertices {
return [vertexData mutableBytes];
}
- (GLKVector2 *)textureCoordinates {
return [textureCoordinateData mutableBytes];
}
,然后渲染功能(OpenGL通过它的委托(应用程序委托)调用它:
-
质地:
GLKBaseEffect *effect = [[GLKBaseEffect alloc] init]; effect.texture2d0.envMode = GLKTextureEnvModeReplace; effect.texture2d0.target = GLKTextureTarget2D; effect.texture2d0.name = texture.name;
-
self.vertices:
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
-
self.textureCordinates
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates);
什么是我在做什么显而易见的内存问题?
非常感谢
答
创建与大小4个字节的NSMutableData对象,但是数据类型GLKVector2大于1个字节大。如果你希望存储在那里的几个对象,你应该做的
vertexData = [NSMutableData dataWithLength:4 * sizeof(GLKVector2)];
,类似的还有textureCoordinateData。
你的回溯是什么意思? –
@CarlNorum - 你通常不会用EXC_BAD_ACCESS获得回溯。 –
@Hot Licks,你应该可以在*任何*时间回溯,所以我很确定你可以。 –