0

This should be a C++ specific.

I have a property m9ReloadAnim in the header file, I can access it from the constructor, but when I try to access it from an other function I get an error like: EXC_BAD_ACCESS or something like: "The address does not contain an object ".

I have a header class like this:

#ifndef __SWAT__Weapon__


#define __SWAT__Weapon__

#include "cocos2d.h"

class Weapon : public cocos2d::CCSprite
{
private:
    cocos2d::CCAnimation *m9ReloadAnim = cocos2d::CCAnimation::create();
public:
    Weapon();
    ~Weapon();
    void reloadM9();
};

#endif 

And a cpp file like this:

enter code here
#include "Weapon.h"
#include "cocos2d.h"


Weapon::Weapon(){ 
 m9ReloadAnim->setDelayPerUnit(1.1f);
}

Weapon::~Weapon(){
}

void Weapon::reloadM9(){
    m9ReloadAnim->setDelayPerUnit(1.1f);

}
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45

2 Answers2

3

You could not initialize variable like this:

cocos2d::CCAnimation *m9ReloadAnim = cocos2d::CCAnimation::create();

Only static const int could be init in class declaration.

Move this init to your ctor:

Weapon::Weapon()
  : m9ReloadAnim(cocos2d::CCAnimation::create())
{
    m9ReloadAnim->setDelayPerUnit(1.1f);
}

or

Weapon::Weapon()
{
    m9ReloadAnim = cocos2d::CCAnimation::create();
    m9ReloadAnim->setDelayPerUnit(1.1f);
}
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
  • I don't think this should be a big problem, anyways I've tried it, still no luck – Ferenc Dajka Mar 29 '13 at 14:52
  • @FerencDajka Where does the error message point to? cpp or header file? Might this post be of some help: http://stackoverflow.com/questions/327082/exc-bad-access-signal-received – gongzhitaao Mar 29 '13 at 15:07
  • exactly to the line where i try to acces the property, so in the cpp – Ferenc Dajka Mar 29 '13 at 15:09
  • @FerencDajka I've updated a link in my response, see if it might be of some help. – gongzhitaao Mar 29 '13 at 15:11
  • @FerencDajka Hi, this is all I could do, hope it's of some help: http://stackoverflow.com/questions/9026225/cocos2d-exc-bad-access. – gongzhitaao Mar 29 '13 at 15:24
  • it's something like it is not in the memory when I try to acces, bewcouse the GC deleted it, but why, if I have it in the header class? It should be removed, when the class is able to collected by the gc. – Ferenc Dajka Mar 29 '13 at 15:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27190/discussion-between-gongzhitaao-and-ferenc-dajka) – gongzhitaao Mar 29 '13 at 15:35
0

Sometimes the becomes corrupted so you can't tell where errors originate. I would suggest to put a breakpoint at the entry point of each method, and step the code line by line to make sure that it's triggering in the reloadM9 method. Check to see the m9ReloadAnim is NULL or if it points to the object created at initialization. Additionally you need to check if you are using the library properly.

Lefteris E
  • 2,806
  • 1
  • 24
  • 23