写了如下一段代码,输出VOCellNode的引用计数和一个字符串内容,然后加入了一个计划任务每次都输出引用计数和字符串,结果发现第三次之后VOCellNode的引用计数不对了,字符串内容也不对了,而且 析构函数也会被调用,然后输出析构函数的内容,是不是出现了野指针?
#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "MapEnum.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
void HelloWorld::test(float t)
{
CCLOG("references:%u %s", vo->getReferenceCount(), vo->str.c_str());
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
vo = VOCellNode::create();
vo->str = "VOCellNode";
CCLOG("references:%u %s", vo->getReferenceCount(), vo->str.c_str());
this->schedule(schedule_selector(HelloWorld::test));
return true;
}
```
//
// VOCellNode.cpp
// TurnTableRPG
//
// Created by hanxianming on 15/4/7.
//
//
#include "VOCellNode.h"
bool VOCellNode::init()
{
return true;
}
VOCellNode::~VOCellNode()
{
CCLOG("~VOCellNode");
}
```
//
// VOCellNode.h
// TurnTableRPG
//
// Created by hanxianming on 15/4/7.
//
//
#ifndef __TurnTableRPG__VOCellNode__
#define __TurnTableRPG__VOCellNode__
class VOCellNode : public cocos2d::Ref
{
public:
cocos2d::Color3B color;
std::string str;
~VOCellNode();
virtual bool init();
CREATE_FUNC(VOCellNode);
private:
};
#endif /* defined(__TurnTableRPG__VOCellNode__) */
```