S
S
SVolkov2013-11-21 20:42:23
C++ / C#
SVolkov, 2013-11-21 20:42:23

C++ memory error?

Hello, I am far from the C ++ Zen, and I read little on it, which I sin on .. and for me, to be honest, this is an unusual and mysterious mistake.

#ifndef MMUSICANTLAYER_H
#define MMUSICANTLAYER_H

#include "cocos-ext.h"
#include "../Common.h"
USING_NS_CC;

class MMusicantLayer : public Layer
{
public:
    CREATE_FUNC(MMusicantLayer);
    ~MMusicantLayer();
    bool onTouchBegan(Touch *touch, Event *event);
    void onTouchMoved(Touch *touch, Event *event);
    void onTouchEnded(Touch *touch, Event *event);
private:
    bool init();
    MMusicantLayer();
    Point* m_beginPoint;
};

#endif // MMUSICANTLAYER_H

#include "MMusicantLayer.h"

MMusicantLayer::MMusicantLayer()
{
    m_beginPoint = new Point(0,0);
}

MMusicantLayer::~MMusicantLayer()
{
    CC_SAFE_DELETE(m_beginPoint);

}

bool MMusicantLayer::init()
{
    if(!Layer::init())  {
        return false;
    }

    Object* it;
    CCARRAY_FOREACH(MGameState::getInstance()->getActiveTeam(), it) {
        MMusicant* musicant = static_cast<MMusicant*>(it);
        addChild(musicant);
    }

    CCARRAY_FOREACH(MGameState::getInstance()->getPassiveTeam(), it) {
        MMusicant* musicant = static_cast<MMusicant*>(it);
        addChild(musicant);
    }

    setTouchEnabled(true);

    return true;
}


bool MMusicantLayer::onTouchBegan(Touch *touch, Event *event)
{
    Point newLoc = touch->getLocation();
    m_beginPoint->setPoint(newLoc.x, newLoc.y);

    return true;
}


void MMusicantLayer::onTouchMoved(Touch *touch, Event *event)
{
    CCLOG("%lf %lf", m_beginPoint->x, m_beginPoint->y);
}


void MMusicantLayer::onTouchEnded(Touch *touch, Event *event)
{

}

#define CC_SAFE_DELETE(p)           do { delete (p); (p) = nullptr; } while(0)

valgrind shows:
Invalid write of size 4 in MMusicantLayer::MMusicantLayer() in path/..../MMusicantLayer.cpp:5
As a result, the program throws an error stack on exit or if NULL is written to m_beginPoint (although this also happens on exit ). Similar behavior was manifested earlier, in other classes, but also disappeared incomprehensibly.
What could be the problem?
P.S.
Perhaps important: OS Ubuntu 13.0, x32, 8GB RAM, gcc version 4.7.3

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2013-11-22
@dmtrrr

See what happens in the constructor of the Point class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question