初始化在initWithCoder中的内存在drawView中丢失

时间:2009-08-22 18:57:33

标签: iphone objective-c opengl-es

我有一些在initWithCoder中初始化的内存:我可以验证在initWithCoder的“return self”部分之前,内存是初始化的。

我使用malloc分配内存并使用自定义函数填充它:

// in initWithCoder:
fontTexCoords = (GLfloat *)malloc(10 * 8 * sizeof(GLfloat));
[fontInfo textureCoordinatesToArray:fontTexCoords];
NSLog(@"%f", fontTexCoords[0]);  // correctly outputs 1.0

但是,在我的绘图程序中,似乎该内存的内容已归零:

// in drawView
NSLog(@"%f", fontTexCoords[0]); // incorrectly outputs 0.0

我不会触摸应用程序中任何其他位置的内存。

问题:为什么所有数据都丢失了? initWithCoder和drawView之间的进程是什么进行的,并且对我的malloc内存做了一些奇怪的事情?

修改

initWithCoder中前8个浮点数的输出使用NSLog(@“initWithCoder:%@%X%f”,self,fontTexCoords,fontTexCoords [0 到8 ]);

2009-08-22 21:25:15.220 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.221 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 1.000000
2009-08-22 21:25:15.222 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.223 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.500000
2009-08-22 21:25:15.223 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.500000
2009-08-22 21:25:15.224 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.500000
2009-08-22 21:25:15.224 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.500000
2009-08-22 21:25:15.224 Memory[32706:20b] initWithCoder: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 1.000000

使用NSLog从drawView输出(@“drawView:%@%X%f”,self,fontTexCoords,fontTexCoords [0 到8 ]);

2009-08-22 21:25:15.399 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.399 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.400 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.401 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.403 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.404 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.404 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000
2009-08-22 21:25:15.405 Memory[32706:20b] drawView: <EAGLView: 0xd19060; frame = (0 0; 320 480); autoresize = W+H; layer = <CAEAGLLayer: 0xd1b300>> D53110 0.000000

由于

2 个答案:

答案 0 :(得分:1)

您可以更改NSLog调用以在输出中包含self吗?然后确认它与两个地方使用的视图相同:

NSLog(@"%@ %f", self, fontTexCoords[0]);

答案 1 :(得分:0)

发现问题。这是我的愚蠢。我用错误的方法释放了内存。不知怎的,我把free(fontTexCoords)放在layoutView而不是dealloc中。

傻,傻傻的我:)。