gpt4 book ai didi

c++ - 当包含它的对象不改变时,这个成员变量的地址如何在 objective-c 中改变?

转载 作者:可可西里 更新时间:2023-11-01 06:09:49 25 4
gpt4 key购买 nike

一段时间以来,我一直在努力解决这个问题,但无法弄清楚为什么会这样。它似乎只发生在“存档”应用程序并在设备上运行时,而不是调试应用程序时。

我有两个类:

@interface AppController : NSObject <UIApplicationDelegate>
{
EAGLView * glView; // A view for OpenGL ES rendering
}

@interface EAGLView : UIView
{
@public
GLuint framebuffer;
}

- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)fformat depthFormat:(GLuint)depth stencilFormat:(GLuint)stencil preserveBackbuffer:(bool)retained scale:(float)fscale msaaMaxSamples:(GLuint)maxSamples;

我正在初始化一个对象:

glView = [ EAGLView alloc ];
glView = [ glView initWithFrame:rect pixelFormat:strColourFormat depthFormat:iDepthFormat stencilFormat:iStencilFormat preserveBackbuffer:NO scale:scale msaaMaxSamples:iMSAA ];
NSLog(@"%s:%d &glView %p\n", __FILE__, __LINE__, glView );
NSLog(@"%s:%d &glView->framebuffer %p\n", __FILE__, __LINE__, &glView->framebuffer );

initWithFrame 看起来像:

- (id) initWithFrame:(CGRect)frame
/* ... */
{
if( ( self = [super initWithFrame:frame] ) )
{
/* ... */
}
NSLog(@"%s:%d &self %p\n", __FILE__, __LINE__, self );
NSLog(@"%s:%d &framebuffer %p\n", __FILE__, __LINE__, &framebuffer );

return self;
}

日志显示:

EAGLView.mm:399 self 0x134503e90
EAGLView.mm:401 &framebuffer 0x134503f68
AppController.mm:277 glView 0x134503e90
AppController.mm:281 &glView->framebuffer 0x134503f10

当包含它的对象不改变时,这个成员变量的地址怎么会改变?

最佳答案

为什么不使用指针呢?您保证地址将是相同的。将 EAGLView 修改成这样

@interface EAGLView : UIView 
{
@public
GLuint *framebuffer;
}

打印出 framebuffer 的地址:

NSLog(@"%s:%d &glView->framebuffer %p\n", __FILE__, __LINE__, glView->framebuffer );

initWithFrame 中做类似的事情:

- (id) initWithFrame:(CGRect)frame
{
unsigned int fbo= opengl_get_framebuffer();
framebuffer = &fbo;
NSLog(@"%s:%d &framebuffer %p\n", __FILE__, __LINE__, framebuffer );
}

现在帧缓冲区的地址应该相同!

关于c++ - 当包含它的对象不改变时,这个成员变量的地址如何在 objective-c 中改变?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19710756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com