PDA

View Full Version : Segmentation error



8Observer8
12th January 2015, 21:00
Hello

I have the Segmentation error on the line: m_textures.push_back( new QOpenGLTexture( frame ) );

When I press "the Space key":


void ExplosionOfProjectile::genTextures()
{
QImage image( ":/Texture/TankSpriteSheet.png" );
image = image.mirrored( false, true );

QImage frame;

frame = image.copy( 257, 112, 15, 15 );
m_textures.push_back( new QOpenGLTexture( frame ) );

frame = image.copy( 272, 112, 16, 16 );
m_textures.push_back( new QOpenGLTexture( frame ) );

frame = image.copy( 287, 111, 18, 18 );
m_textures.push_back( new QOpenGLTexture( frame ) );
}


Source: https://github.com/8Observer8/Tanks

8Observer8
13th January 2015, 09:31
I think this is because I need to set a context. I to set context?

anda_skoa
13th January 2015, 09:31
Is "this" a vald instance?

Cheers,
_

8Observer8
13th January 2015, 10:12
What do you mean?

ExplosionOfProjectile.h

class ExplosionOfProjectile : public QObject, public Plane
{
//...
private:
std::vector<QOpenGLTexture*> m_textures;
}

ExplosionOfProjectile.cpp

void ExplosionOfProjectile::genTextures()
{
QImage image( ":/Textures/TankSpriteSheet.png" );
image = image.mirrored( false, true );

QImage frame;

frame = image.copy( 257, 112, 15, 15 );
m_textures.push_back( new QOpenGLTexture( frame ) );

frame = image.copy( 272, 112, 16, 16 );
m_textures.push_back( new QOpenGLTexture( frame ) );

frame = image.copy( 287, 111, 18, 18 );
m_textures.push_back( new QOpenGLTexture( frame ) );
}

The error on the line: m_textures.push_back( new QOpenGLTexture( frame ) );

Added after 34 minutes:

It was bacause I had mistake. I replace "Texture" on "Textures". But now there is a new crash

I set breakpoing on "update()" method but after "continue" I have crash:


void Scene::addProjectileExplosion( float x0, float y0 )
{
ExplosionOfProjectile *explosion = new ExplosionOfProjectile( &m_program, m_vertexAttr, m_textureAttr, m_textureUniform );
explosion->setX0( x0 );
explosion->setY0( y0 );
connect( explosion, SIGNAL( signalShowProjectileExplosion( int, bool ) ),
this, SLOT( slotShowProjectileExplosion( int, bool ) ) );
m_projectileExplosions[explosion->id()] = explosion;
explosion->start();

update();
}

https://github.com/8Observer8/Tanks

8Observer8
13th January 2015, 15:11
I solved the problem: https://github.com/8Observer8/Tanks

The Error:

for ( auto iterOfProjectile = m_projectiles.begin(); iterOfProjectile != m_projectiles.end(); ++iterOfProjectile )
{
....
m_projectiles.erase( iterOfProjectile );
addProjectileExplosion( x0, y0 );

The Solution:


auto iterOfProjectile = m_projectiles.begin();

// for ( auto iterOfProjectile = m_projectiles.begin(); iterOfProjectile != m_projectiles.end(); ++iterOfProjectile )
while (iterOfProjectile != m_projectiles.end())
{
Projectile *projectile = iterOfProjectile->second;

float x0 = projectile->x0();
float y0 = projectile->y0();

bool doDel = false;

switch ( projectile->direction() ) {
case Projectile::Up:
projectile->setY0( projectile->y0() - step );
doDel = projectile->y0() <= 0.0f;
break;
case Projectile::Left:
projectile->setX0( projectile->x0() - step );
doDel = projectile->x0() < 0.0f;
break;
case Projectile::Down:
projectile->setY0( projectile->y0() + step );
doDel = projectile->y0() >= ( m_canvasHeight - projectile->height() );
break;
case Projectile::Right:
projectile->setX0( projectile->x0() + step );
doDel = projectile->x0() > ( m_canvasWidth - projectile->width() );
break;
}

if (doDel )
{
delete projectile;
m_projectiles.erase( iterOfProjectile++ );
addProjectileExplosion( x0, y0 );
}
else
iterOfProjectile++;
}

I am helped here (http://www.prog.org.ru/index.php?topic=28254.msg206472#msg206472)

anda_skoa
13th January 2015, 15:51
Correct would be to use the return value of erase() as the new iterOfProjectile.

Cheers,
_