【已解决】新手求助,我的成员变量不知怎么的就被释放掉了

需要实现一个类似点击屏幕,进行切换精灵图片的功能。

思路是这样的:

HelloWorldScene.h


class HelloWorld : public cocos2d::Layer
{
    protected:
        int m_Index; // 精灵帧下标
        cocos2d::SpriteFrame* m_Frame; // 精灵帧
        cocos2d::Sprite* m_Sprite; // 精灵
    public:
        static cocos2d::Scene* createScene();
        virtual bool init();  
        void menuCloseCallback(cocos2d::Ref* pSender);
        CREATE_FUNC(HelloWorld);
        void onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event  *event);
}

HelloWorldScene.cpp


bool HelloWorld::init()
{
    // 前略前略前略前略前略

    /////////////////////////////
    // 3. add your codes below...

    auto listener = EventListenerTouchAllAtOnce::create();
    listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

    auto image = Director::getInstance()->getTextureCache()->addImage("grossini_dance_atlas.png");

    for(int i = 0; i < 4; i++)
    {
        m_Frame* = SpriteFrame::createWithTexture(image, Rect(85*i, 121, 85, 121));
        m_Frame*->retain(); // 加入此行后解决问题
    }

    m_Index = 0;
    m_Sprite = Sprite::createWithSpriteFrame(m_Frame);
    m_Sprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    m_Sprite->setPosition(visibleSize.width/2, visibleSize.height/2);
    addChild(m_Sprite, 1);

    log("init: m_Frame:%u", m_Frame->getReferenceCount());  //  打印出 1
    return true;
}

void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches, Event  *event)
{
    log("ended: m_Frame:%u", m_Frame->getReferenceCount());  //  打印出 0
    m_Index++;
    if (m_Index == 4)
        m_Index = 0;
    m_Sprite->setSpriteFrame(m_Frame); // 此处终止
}
**

终止在 CCRef.cpp

void Ref::retain()
{
CCASSERT(_referenceCount > 0, “reference count should greater than 0”); // 此处终止,但并不是这个断言报错,而是(见下文)
++_referenceCount;



报错信息
Unhandled exception at 0x00FA7B02 in MyCppGame.exe: 0xC0000005: Access violation reading location 0x18187157.


我想大概是那个自动释放机制把我的精灵帧给释放了,然后就不懂了,感觉这样很不合理啊,求解惑~

感觉jo1983提供的解决方法

*--- Begin quote from ____*

引用第5楼jo1983于2014-07-29 11:21发表的  :
 for(int i = 0; i < 4; i++)
        m_Frame* = SpriteFrame::createWithTexture(image, Rect(85*i, 121, 85, 121));

改成
 for(int i = 0; i < 4; i++)
....... http://www.cocoachina.com/bbs/job.php?action=topost&tid=217236&pid=1015673
![](p_w_picpath/back.gif)
*

*--- End quote*




m_Frame没有reatain, 已经被释放了

是m_Frame里的spriteFrame没有保存

— Begin quote from ____

引用第2楼jo1983于2014-07-29 09:37发表的 :
是m_Frame里的spriteFrame没有保存 http://www.cocoachina.com/bbs/job.php?action=topost&tid=217236&pid=1015494

— End quote

什么意思?这个m_Frame不就是SpriteFrame吗?
没有被保存?但是m_Frame是类HelloWorld的一个成员啊,为什么在HelloWorld的实例还存在着的情况下会被释放呢?我就是奇怪这点。

还是说我在做完
m_Sprite = Sprite::createWithSpriteFrame(m_Frame);
后,还要再
m_Sprite->retain();
吗???应该不用的吧?

需要retain,否则在每一帧之后就会被自动回收池回收,retain了之后记得release

for(int i = 0; i < 4; i++)
m_Frame* = SpriteFrame::createWithTexture(image, Rect(85*i, 121, 85, 121));

改成
for(int i = 0; i < 4; i++)
{
m_Frame* = SpriteFrame::createWithTexture(image, Rect(85i, 121, 85, 121));
m_Frame
->retain();
}

试试,这里需要retain***

我靠,为什么我的【i】都不见了

— Begin quote from ____

引用第5楼jo1983于2014-07-29 11:21发表的 :
for(int i = 0; i < 4; i++)
m_Frame* = SpriteFrame::createWithTexture(image, Rect(85*i, 121, 85, 121));

改成
for(int i = 0; i < 4; i++)
http://www.cocoachina.com/bbs/job.php?action=topost&tid=217236&pid=1015673

*

retain了果然就好了,谢谢你~~~

— End quote