2011-11-27 75 views
1

我已經自動引用計數和殭屍啓用...常數,隨機和莫名的(對我來說,反正)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通過它的委託(應用程序委託)調用它:

  1. 質地:

    GLKBaseEffect *effect = [[GLKBaseEffect alloc] init]; 
    effect.texture2d0.envMode = GLKTextureEnvModeReplace; 
    effect.texture2d0.target = GLKTextureTarget2D; 
    effect.texture2d0.name = texture.name; 
    
  2. self.vertices:

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

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

什麼是我在做什麼顯而易見的內存問題?

非常感謝

+0

你的回溯是什麼意思? –

+0

@CarlNorum - 你通常不會用EXC_BAD_ACCESS獲得回溯。 –

+1

@Hot Licks,你應該可以在*任何*時間回溯,所以我很確定你可以。 –

回答

1

創建與大小4個字節的NSMutableData對象,但是數據類型GLKVector2大於1個字節大。如果你希望存儲在那裏的幾個對象,你應該做的

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

,類似的還有textureCoordinateData。

+0

就是這樣!非常感謝。 – Vazzyb

+0

您能否將此答案標記爲正確? – joerick

+0

我不相信這些糟糕的訪問警告是多麼無益!我什至不能找到一個體面的教程如何調試它們。我想你創建它們的方式也是相關的;像我這樣的非常差的編程可能會更頻繁地拋出它們! – Vazzyb