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

前端之家收集整理的这篇文章主要介绍了c – 当包含它的对象不在时,该成员变量的地址如何在objective-c中改变?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图解决这个问题一段时间,但无法弄清楚它为什么会发生.它似乎只发生在“存档”应用程序并在设备上运行时,而不是在调试应用程序时.

我有两节课:

@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",&glView->framebuffer );

使用initWithFrame看起来像:

- (id) initWithFrame:(CGRect)frame
    /* ... */
{
    if( ( self = [super initWithFrame:frame] ) )
    {
        /* ... */
    }
    NSLog(@"%s:%d &self %p\n",self );
    NSLog(@"%s:%d &framebuffer %p\n",&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",glView->framebuffer );

在initWithFrame里面做类似的事情:

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

现在帧缓冲的地址应该是一样的!

猜你在找的Xcode相关文章