Results 1 to 6 of 6

Thread: Segmentation error

  1. #1
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Segmentation error

    Hello

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

    When I press "the Space key":

    Qt Code:
    1. void ExplosionOfProjectile::genTextures()
    2. {
    3. QImage image( ":/Texture/TankSpriteSheet.png" );
    4. image = image.mirrored( false, true );
    5.  
    6. QImage frame;
    7.  
    8. frame = image.copy( 257, 112, 15, 15 );
    9. m_textures.push_back( new QOpenGLTexture( frame ) );
    10.  
    11. frame = image.copy( 272, 112, 16, 16 );
    12. m_textures.push_back( new QOpenGLTexture( frame ) );
    13.  
    14. frame = image.copy( 287, 111, 18, 18 );
    15. m_textures.push_back( new QOpenGLTexture( frame ) );
    16. }
    To copy to clipboard, switch view to plain text mode 

    Source: https://github.com/8Observer8/Tanks
    Last edited by 8Observer8; 12th January 2015 at 20:21.

  2. #2
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Segmentation error

    I think this is because I need to set a context. I to set context?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error

    Is "this" a vald instance?

    Cheers,
    _

  4. #4
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Segmentation error

    What do you mean?

    ExplosionOfProjectile.h
    Qt Code:
    1. class ExplosionOfProjectile : public QObject, public Plane
    2. {
    3. //...
    4. private:
    5. std::vector<QOpenGLTexture*> m_textures;
    6. }
    To copy to clipboard, switch view to plain text mode 

    ExplosionOfProjectile.cpp
    Qt Code:
    1. void ExplosionOfProjectile::genTextures()
    2. {
    3. QImage image( ":/Textures/TankSpriteSheet.png" );
    4. image = image.mirrored( false, true );
    5.  
    6. QImage frame;
    7.  
    8. frame = image.copy( 257, 112, 15, 15 );
    9. m_textures.push_back( new QOpenGLTexture( frame ) );
    10.  
    11. frame = image.copy( 272, 112, 16, 16 );
    12. m_textures.push_back( new QOpenGLTexture( frame ) );
    13.  
    14. frame = image.copy( 287, 111, 18, 18 );
    15. m_textures.push_back( new QOpenGLTexture( frame ) );
    16. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. void Scene::addProjectileExplosion( float x0, float y0 )
    2. {
    3. ExplosionOfProjectile *explosion = new ExplosionOfProjectile( &m_program, m_vertexAttr, m_textureAttr, m_textureUniform );
    4. explosion->setX0( x0 );
    5. explosion->setY0( y0 );
    6. connect( explosion, SIGNAL( signalShowProjectileExplosion( int, bool ) ),
    7. this, SLOT( slotShowProjectileExplosion( int, bool ) ) );
    8. m_projectileExplosions[explosion->id()] = explosion;
    9. explosion->start();
    10.  
    11. update();
    12. }
    To copy to clipboard, switch view to plain text mode 

    https://github.com/8Observer8/Tanks
    Last edited by 8Observer8; 13th January 2015 at 09:12.

  5. #5
    Join Date
    Nov 2012
    Posts
    232
    Thanks
    118
    Thanked 18 Times in 10 Posts
    Platforms
    Windows Android

    Default Re: Segmentation error

    I solved the problem: https://github.com/8Observer8/Tanks

    The Error:
    Qt Code:
    1. for ( auto iterOfProjectile = m_projectiles.begin(); iterOfProjectile != m_projectiles.end(); ++iterOfProjectile )
    2. {
    3. ....
    4. m_projectiles.erase( iterOfProjectile );
    5. addProjectileExplosion( x0, y0 );
    To copy to clipboard, switch view to plain text mode 

    The Solution:
    Qt Code:
    1. auto iterOfProjectile = m_projectiles.begin();
    2.  
    3. // for ( auto iterOfProjectile = m_projectiles.begin(); iterOfProjectile != m_projectiles.end(); ++iterOfProjectile )
    4. while (iterOfProjectile != m_projectiles.end())
    5. {
    6. Projectile *projectile = iterOfProjectile->second;
    7.  
    8. float x0 = projectile->x0();
    9. float y0 = projectile->y0();
    10.  
    11. bool doDel = false;
    12.  
    13. switch ( projectile->direction() ) {
    14. case Projectile::Up:
    15. projectile->setY0( projectile->y0() - step );
    16. doDel = projectile->y0() <= 0.0f;
    17. break;
    18. case Projectile::Left:
    19. projectile->setX0( projectile->x0() - step );
    20. doDel = projectile->x0() < 0.0f;
    21. break;
    22. case Projectile::Down:
    23. projectile->setY0( projectile->y0() + step );
    24. doDel = projectile->y0() >= ( m_canvasHeight - projectile->height() );
    25. break;
    26. case Projectile::Right:
    27. projectile->setX0( projectile->x0() + step );
    28. doDel = projectile->x0() > ( m_canvasWidth - projectile->width() );
    29. break;
    30. }
    31.  
    32. if (doDel )
    33. {
    34. delete projectile;
    35. m_projectiles.erase( iterOfProjectile++ );
    36. addProjectileExplosion( x0, y0 );
    37. }
    38. else
    39. iterOfProjectile++;
    40. }
    To copy to clipboard, switch view to plain text mode 

    I am helped here

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Segmentation error

    Correct would be to use the return value of erase() as the new iterOfProjectile.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    8Observer8 (13th January 2015)

Similar Threads

  1. Segmentation fault error
    By Niamita in forum Qt Programming
    Replies: 5
    Last Post: 23rd April 2012, 17:00
  2. Segmentation error
    By dacron.goku in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2012, 13:07
  3. Segmentation error! Please help!!!
    By phapha in forum Newbie
    Replies: 4
    Last Post: 26th October 2011, 17:01
  4. Segmentation error in run
    By kurrachow in forum Newbie
    Replies: 1
    Last Post: 21st April 2011, 13:13
  5. QT 4.6.0 with threads - UIC segmentation error
    By edmondo1984 in forum Installation and Deployment
    Replies: 5
    Last Post: 15th November 2010, 18:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.