EXC_BAD_ACCESS - Holyhoppsan - Aug 10, 2010 01:14 AM
Hi!
I'm trying to combine obj-c with c++ since i'm more used to c++. My code that inits opengl works fine and everything runs smoothly until i try to render an image. somehow i get an EXC_BAD_ACCESS. I know that this is caused by my code when it tries to use memory that has been released before. but after running my code in instruments, i still can't seem to find the cause. the code that causes the EXC_BAD_ACCESS is the following:
Code:
#include "Image.h"
CORE::Image::Image()
{
}
CORE::Image::Image(const Image& aImage)
{
NSLog(@"COPY");
}
CORE::Image::~Image()
{
NSLog(@"IMage Destroy");
}
void CORE::Image::InitImage(std::string aTextureFileName, float aTextureSizeX, float aTextureSizeY)
{
myTextureSizeX = aTextureSizeX;
myTextureSizeY = aTextureSizeY;
glGenTextures(1, &myTexture);
glBindTexture(GL_TEXTURE_2D, myTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
std::string firstPartOfFileName;
std::string fileExtension;
bool hasFoundDot = false;
for(unsigned int i = 0; i < aTextureFileName.length(); i++)
{
if(!hasFoundDot)
{
if(aTextureFileName[i] == '.')
{
hasFoundDot = true;
}
else
{
firstPartOfFileName += aTextureFileName[i];
}
}
else
{
fileExtension += aTextureFileName[i];
}
}
// NSString *firstPart = [NSString stringWithUTF8String: firstPartOfFileName.c_str()];
// NSString *extension = [NSString stringWithUTF8String: fileExtension.c_str()];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Hej" ofType:@"pvrtc"];
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, aTextureSizeX, aTextureSizeY, 0, [texData length], [texData bytes]);
}
void CORE::Image::Render(float aX, float aY)
{
NSLog(@"RENDERAR");
const GLfloat texCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
const GLfloat squareVertices[] = {
aX, aY,
aX + myTextureSizeX, aY,
aX, aY + myTextureSizeY,
aX + myTextureSizeX, aY + myTextureSizeY,
};
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, myTexture);
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Enable Texture_2D
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Now we are done drawing disable blending
glDisable(GL_BLEND);
// Disable as necessary
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glPopMatrix();
}
void CORE::Image::RenderFrame(float aX, float aY, float aFrameStartX, float aFrameStartY, float aWidth, float aHeight)
{
if((aFrameStartX + aWidth) > myTextureSizeX)
{
NSLog(@"TextureSizeX %d", myTextureSizeX);
assert(0);
}
if((aFrameStartY + aHeight) > myTextureSizeY)
{
NSLog(@"TextureSizeY %d", myTextureSizeY);
assert(0);
}
float texStartX = (aFrameStartX) / myTextureSizeX;
float texEndX = (aFrameStartX +aWidth) / myTextureSizeX;
float texStartY = (aFrameStartY) / myTextureSizeY;
float texEndY = (aFrameStartY + aHeight) / myTextureSizeY;
const GLfloat texCoords[] = {
texStartX, texEndY,
texEndX, texEndY,
texStartX, texStartY,
texEndX, texStartY
};
const GLfloat squareVertices[] = {
aX, aY,
aX + aWidth, aY,
aX, aY + aHeight,
aX + aWidth, aY + aHeight,
};
glBindTexture(GL_TEXTURE_2D, myTexture);
glVertexPointer(2, GL_FLOAT, 0, squareVertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
If i run my app without trying to render an image everything works fine so the opengl es is properly initialized.
RE: EXC_BAD_ACCESS - SethWillits - Aug 10, 2010 11:27 AM
(Aug 10, 2010 01:14 AM)Holyhoppsan Wrote: somehow i get an EXC_BAD_ACCESS. I know that this is caused by my code when it tries to use memory that has been released before.
Evidenced by....?
Quote:but after running my code in instruments, i still can't seem to find the cause.
If you run the code with the debugger attached it'll break on the line that caused the exception. If you're getting a standard crash log, it'll tell you the stack trace from when the crash occurred.
Those are your best sources to find the problem.
Btw:
Code:
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, aTextureSizeX, aTextureSizeY, 0, [texData length], [texData bytes]);
Leak.
RE: EXC_BAD_ACCESS - Holyhoppsan - Aug 10, 2010 01:44 PM
(Aug 10, 2010 11:27 AM)FreakSoftware Wrote: (Aug 10, 2010 01:14 AM)Holyhoppsan Wrote: somehow i get an EXC_BAD_ACCESS. I know that this is caused by my code when it tries to use memory that has been released before.
Evidenced by....?
Quote:but after running my code in instruments, i still can't seem to find the cause.
If you run the code with the debugger attached it'll break on the line that caused the exception. If you're getting a standard crash log, it'll tell you the stack trace from when the crash occurred.
Those are your best sources to find the problem.
Btw:
Code:
NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, aTextureSizeX, aTextureSizeY, 0, [texData length], [texData bytes]);
Leak.
Thanks for spoting the leak.
After a day of trial and error i've noticed that the cause of this problem is not in my imageclass. The app gives me a bad access error while calling the following code:
Code:
[EAGLContext setCurrentContext:myContext];
glBindFramebufferOES(GL_FRAMEBUFFER_OES, myViewFramebuffer);
glViewport(0, 0, 320, 480); <-- It crashes here
glClear(GL_COLOR_BUFFER_BIT); // or sometimes here
for(int taskIndex = 0; taskIndex < myRenderList.Count(); taskIndex++)
{
if(myRenderList[taskIndex].type == IMAGE)
{
myResourceHandler.GetSprite(myRenderList[taskIndex].key)->Render(myRenderList[taskIndex].x, myRenderList[taskIndex].y);
}
if(myRenderList[taskIndex].type == FRAME)
{
myResourceHandler.GetSprite(myRenderList[taskIndex].key)->RenderFrame(myRenderList[taskIndex].x, myRenderList[taskIndex].y, myRenderList[taskIndex].frameStartX, myRenderList[taskIndex].frameStartY, myRenderList[taskIndex].width, myRenderList[taskIndex].height);
}
}
glBindRenderbufferOES(GL_RENDERBUFFER_OES, myViewFramebuffer);
[myContext presentRenderbuffer:GL_RENDERBUFFER_OES];
myRenderList.RemoveAll();
|