1

我启用了自动引用计数和僵尸...

我一直在代码中的不同点获得 EXC BAD ACCESS,大多数时候没有来自僵尸的进一步信息。

目标是在屏幕上绘制一个矩形,上面带有从图像加载的纹理。它确实有效!但通常它已损坏(图像和矢量),然后我经常收到 exc bad access 警告。

我有这种结构...

应用委托类声明:

GWBackgroundScene* background;
EAGLContext *context;
GLKView *view;
GLKViewController *controller;
UIWindow *window;

然后将window变成一个属性并合成。

使用选项完成启动:

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 在类中调用一个名为“render”的函数,然后调用 [background render];

我的 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 通过其委托(App 委托)调用)使用:

  1. 质地:

    GLKBaseEffect *effect = [[GLKBaseEffect alloc] init];
    effect.texture2d0.envMode = GLKTextureEnvModeReplace;
    effect.texture2d0.target = GLKTextureTarget2D;
    effect.texture2d0.name = texture.name; 
    
  2. 自我顶点:

    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
    
  3. self.textureCordinates

    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates);
    

我在做什么有什么明显的记忆问题?

非常感谢

4

1 回答 1

1

您创建了一个大小为 4 个字节的 NSMutableData 对象,但数据类型 GLKVector2 大于 1 个字节。如果您希望在其中存储一些对象,则应该这样做

vertexData = [NSMutableData dataWithLength:4 * sizeof(GLKVector2)];

对于纹理坐标数据也是如此。

于 2011-11-27T20:48:29.917 回答