持续的、随机的和莫名其妙的(无论如何对我来说)EXC BAD ACCESS in Objective C app
我启用了自动引用计数和僵尸...
我不断收到对代码中不同点的 EXC BAD ACCESS,大多数时候没有来自僵尸的进一步信息。
目标是在屏幕上绘制一个矩形,其上有从图像加载的纹理。它确实有效!但通常它会被损坏(图像和矢量),然后我经常会收到错误的访问警告。
我有这种结构...
应用程序委托类声明:
GWBackgroundScene* background;
EAGLContext *context;
GLKView *view;
GLKViewController *controller;
UIWindow *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 委托)调用)使用:
texture:
GLKBaseEffect *effect = [[GLKBaseEffect alloc] init]; effect.texture2d0.envMode = GLKTextureEnvModeReplace; 效果.texture2d0.target = GLKTextureTarget2D; effect.texture2d0.name = 纹理.name;
self.vertices:
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, self.vertices);
self.textureCordes
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, self.textureCoordinates);
我正在做的事情有哪些明显的内存问题?
非常感谢
I have Automatic Reference Counting and Zombies enabled...
I keep getting EXC BAD ACCESS to different points in the code, most of the time with no further information coming from zombies.
The objective is to draw a rectangle to the screen with a texture on it that is loaded from an image. It does actually work! But often it is corrupted (the image and the vectors) and then often I just get the exc bad access warning.
I have this sort of structure...
App Delegate Class Declaration:
GWBackgroundScene* background;
EAGLContext *context;
GLKView *view;
GLKViewController *controller;
UIWindow *window;
and then window is made into a property and synthesized.
The did Finish Launching With Options:
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"]];
The AppDelegate acts as an OpenGL delegate and OpenGL calls a function called 'render' in the class which then calls [background render];
My GWBackgroundScene class:
@interface GWBackgroundScene : NSObject
{
GLKTextureInfo *texture;
NSMutableData* vertexData;
NSMutableData* textureCoordinateData;
}
@property(readonly) GLKVector2 *vertices;
@property(readonly) GLKVector2 *textureCoordinates;
-(void) render;
-(id) initWithImage: (UIImage*)image;
Initialises with:
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;
and has these two functions for dealing with the vector and texture information
- (GLKVector2 *)vertices {
return [vertexData mutableBytes];
}
- (GLKVector2 *)textureCoordinates {
return [textureCoordinateData mutableBytes];
}
and then the render function (which is called by OpenGL via its delegate (the App delegate) uses:
texture:
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);
What are the obvious memory issues with what im doing?
Thanks very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您创建了一个大小为 4 字节的 NSMutableData 对象,但数据类型 GLKVector2 大于 1 字节。如果您希望在其中存储一些对象,您应该
对textureCooperativeData 执行类似的操作。
You create an NSMutableData object with size 4 bytes, but the data type GLKVector2 is larger than 1 byte. If you're expecting to store a few objects in there you should do
And similarly for textureCoordinateData.