[SOLVED]What does getName in cctexture2d actually return?

No one would answer my previous question, so I need to ask this question to get at least where to start from.

the function returns unsigned int, what does this mean?
what does it do, and in what condition it would spit out memory access violation?

The OpenGL handle (GLuint) for the texture on the GPU.
Used with glGenTextures, glBindTexture, etc
Cocos2d-x abstracts all of these GL calls so you usually don’t have to worry about them.

“Bad Access” would mean that the Texture2D* is invalid or null.

1 Like

Thank you for reply steve
is it possible to have problem with it if I use sprite and camera together?

That question isn’t specific enough or it’s too vague. Either post error output, or debugger exception, or the few lines of code surrounding the issue. You’re probably not retaining the memory of the Texture2D instance. If you are getting the texture from a node then that node is probably not being retained.

1 Like

so my class looks like this.
robot class doesn’t have any sprite member, other classes below all has sprite with them.
My robot class creates chassis and turret instances with ‘new’ if I call ‘create’ function I defined, so it is automatic.

void RobotTemplate::create(cocos2d::Node* _this, CONTROLTYPE conType, CHASSISTYPE chaType, MANEUVERTYPE manType, TURRETTYPE turType)
{
	//make child components
	chassis = new ChassisTemplate();
	chassis->create(conType, chaType, manType);
	turret = new TurretTemplate();
	turret->create(turType);
}

//delete in destructor

so in create function I made in chassis, turret and weapon class, they create sprite like this;

void ChassisTemplate::create(CONTROLTYPE conType, CHASSISTYPE chaType, MANEUVERTYPE manType)
{
	const char* sprName;

	//...set other values

	sprName = "Chassis.png";

	image = cocos2d::Sprite::create(sprName);
}

after all these, I made camera in actual play scene like this.

camera = Camera::createOrthographic(visibleSize.width / 2, visibleSize.height/ 2, 0.0f, 100.0f);
camera->setCameraFlag(CameraFlag::USER1);
//the calling order matters, we should first call setPosition3D, then call lookAt.
camera->setPosition3D(Vec3(visibleSize.width / 2, visibleSize.height / 2, -1.0f));
camera->lookAt(Vec3(visibleSize.width / 2, visibleSize.height / 2, 0.0f), Vec3(0.0, 1.0, 0.0));
PlayLayer->addChild(camera);
PlayLayer->setCameraMask((unsigned short)CameraFlag::USER1, true);

I made two layers so I can distinguish hudLayer and playLayer

finally, as I try to run it, this comes out.

First-chance exception at 0x01518D21 (libcocos2d.dll) in CombatBot.exe: 0xC0000005: Access violation reading location 0x0000002C.
Unhandled exception at 0x01518D21 (libcocos2d.dll) in CombatBot.exe: 0xC0000005: Access violation reading location 0x0000002C.

same result every time.

btw I am using cocosd-x 3.10, visual studio community 2013 on Windows 10

EDIT: fixed picture

Do you add image to another node, layer, or scene after its created in ChassisTemplate: image = cocos2d::Sprite::create(sprName);?? If you access image later on image->method_or_field that probably is the cause of error (based on your posted code).

You need to retain all nodes either by using new MyCustomNode() or by adding it with addChild(node).

Where are you using the texture getName() method? Can you see the stack trace in the debugger to determine what line of your code triggered the exception? Step through until the exception and find out.

Access violation usually means a pointer is being dereferenced that has been freed or deleted. This can also occur in cocos2d-x if a node has been fully released as well.

1 Like

Again, thank you very much for your answer steve.

I do add images to correct places, because I see it working WITHOUT camera.
I also has AI class for this robot, everything works fine WITHOUT camera.
only if I add camera, change the position of layer, or use CCFollow, error above is thrown.

I don’t use getName() function myself,
it is in director->mainLoop() in CCApplication-win32.cpp

 while(!glview->windowShouldClose())
    {
        QueryPerformanceCounter(&nNow);
        if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
        {
            nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart);
            
            director->mainLoop(); //here
            glview->pollEvents();
        }
...

So, I tried some stuff.
I only made chassis and turret, it worked.
but with weapon class, it throws error.

this is exact place where getName function is used, in Sprite class.

//...
#endif
{
 _trianglesCommand.init(_globalZOrder, _texture->getName()/*<-HERE*/, getGLProgramState(), _blendFunc, _polyInfo.triangles, transform, flags);
        renderer->addCommand(&_trianglesCommand);
 //...      

You’re not creating the sprite that the weapon class uses correctly, or you aren’t setting an initial texture, or you are not retaining it. Very difficult to debug errors like this on the forums since the faulty code is not at the source of the error.

Actually looking at your initial post, or actually the image of the class diagram my guess is that you’re creating weapons and storing them into a std::vector? If not then ignore this, otherwise look at storing them in a cocos2d::Vector instead because that retains each object on pushBack and releases during eraseObject.

The reason for this is my guess is that you don’t call addChild(weapon) on every weapon, but rather you’re trying to load in all the weapons up front and then addChild(weapon) only for one weapon at a time?

1 Like

Thank you steve
My classes were subclass of Sprite at the beginning, I changed to current structure only after confronting the issue. Back then, my addWeapon() function in turret class looked like this.

void TurretTemplate::addWeapon(WEAPONTYPE type)
{
	WeaponTemplate* _weapon = new WeaponTemplate;
	_weapon->create(type);
	// WeaponTemplate::create(type);
	image->addChild(_weapon);
	weaponSlots.pushBack(_weapon);
}

This worked back then, but addChild code there became problem after changing structure, because now it means I am adding child that is not sprite.
so I changed it to this;

void TurretTemplate::addWeapon(WEAPONTYPE type)
{
	WeaponTemplate* _weapon = new WeaponTemplate;
	_weapon->create(type);
	// WeaponTemplate::create(type);
	image->addChild(_weapon->getImage()); //changed
	weaponSlots.pushBack(_weapon);
}

Now all the sprite is shown and no error thrown.
I guess there was some kind of issue using Sprite subclass, I still have no idea why subclass thing didn’t work, but oh well, whatever.
Anyway, all works now, no error, all sprites shown and working. Thanks again steve!
you are awesome.

I should go figure out what part I screwed up.
I can’t believe I spent full three days trying to solve this problem. XD

1 Like