vector push_back causes assertion error but list push_back works

maogenc

I'm using Qt5.1 compiled for Visual Studio 2008.

I'm trying to render objects with multiple textures. In my original code, I use a mesh class to manage loading 3D objects, which is based on this tutorial:

http://ogldev.atspace.co.uk/www/tutorial22/tutorial22.html

The mesh class contains a vector that holds textures. Textures load image data from files and keep track of all of the OpenGL state housekeeping necessary to render themselves.

I get a "Debug assertion failed: _BLOCK_TYPE_US_VALID(pHead->nBlockUse)" error when trying to use Qt to add more than one texture to my mesh class vector container. This error occurs when the destructor is called when adding textures to the vector, similar to what is described in this thread:

Destructor called on object when adding it to std::list

I am new to Qt and am wondering what the best way to handle textures is in this case. The tutorial code uses GLEW rather than Qt and I had to inherit from the QOpenGLFunctions to manage the OpenGL state for rendering the textures (my mesh class also inherits from QOpenGLFunctions). It appears that the default copy constructor is doing a shallow copy of the texture object, which deletes memory that is managed by Qt. However, when I switch to using a list container for textures, I don't get the assertion error.

Here is a simplification of my code that illustrates my problem:

#include <QOpenGLFunctions_3_3_Core>
#include <list>
#include <vector>

class Texture : protected QOpenGLFunctions_3_3_Core
{

public:
    Texture() 
    { 
    }

    ~Texture()
    {
        std::cout << "destructor" << std::endl;
    }
};

std::vector<Texture> textureList; //this causes the assertion failure
//std::list<Texture> textureList; //this works
void tester()
{

    for (int i = 0; i < 3; i++)
    {
        Texture Texture;
        textureList.push_back(Texture);
    }
}

int main()
{
    tester();

    //Qt application code
}
angdev

This problem comes from difference of internal implementation between vector and list.

First of all, list is doubly linked list, so list can grow without copies for original elements. But vector is dynamically allocated array. If capacity of vector is full, vector allocate new array with greater size (Array doubling), and copy elements from old array to new array. Below snippet shows why assertion occurs. (Maybe)

#include <iostream>
#include <list>
#include <vector>

class Texture
{

public:
  Texture() 
  {   
    std::cout << "constructor" << std::endl;
  }   
  Texture(const Texture &t) 
  {   
    std::cout << "copy constructor" << std::endl;
  }   

  ~Texture()
  {   
    std::cout << "destructor" << std::endl;
  }   
};

std::vector<Texture> textureList;
//std::list<Texture> textureList;
void tester()
{
  for (int i = 0; i < 3; i++)
  {   
    //textureList.reserve(4);
    std::cout << "Loop\n";
    Texture texture;
    textureList.push_back(texture);
    std::cout << "Capacity: " << textureList.capacity() << "\n";
  }   
  std::cout << "End\n";
}

int main()
{
  std::cout << "main\n";
  tester();
  std::cout << "main end\n";
}

Result

main
Loop
constructor
copy constructor
Capacity: 1
destructor
Loop
constructor
copy constructor
copy constructor
destructor
Capacity: 2
destructor
Loop
constructor
copy constructor
copy constructor
copy constructor
destructor
destructor
Capacity: 4
destructor
End
main end
destructor
destructor
destructor

Texture instance is destroyed whenever vector grows up.

If you prevent from this problem, use vector::reserve (check the above code: textureList.reserve(4)). But I think that another problems will be occurred with this code, so I recommend to use pointer of texture instead of instance of texture to deal with.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Vector of shared_ptr resulting in error: "no instance of overloaded function" when wanting to push_back vector list

From Dev

Vector of shared_ptr resulting in error: "no instance of overloaded function" when wanting to push_back vector list

From Dev

c++ - runtime error: vector push_back()

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

vector push_back zero into an empty vector

From Dev

push_back a vector of vectors into a vector

From Java

Insert or push_back to end of a std::vector?

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

vector::push_back and std::move

From Dev

Why are there two overloads for vector::push_back?

From Dev

string Vector push_back failing in class

From Dev

garbage values for vector push_back

From Dev

push_back new element to vector

From Dev

Implementatnion of std::vector::push_back in MSVC

From Dev

garbage values for vector push_back

From Dev

Vector push_back incredibly slow

From Dev

Vector push_back Array of doubles

From Dev

Push_back variadic function parameters into a vector?

From Dev

c++ push_back() a struct into a vector

From Dev

vector of reference wrapper, push_back failure?

From Dev

push_back a pointer to a vector of pointers

From Dev

Singly linked list - push_back

From Dev

STL list segmentation fault on push_back

From Dev

Error with Push_back function for a LinkedList?

From Dev

std vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

From Dev

Visual Studio Reporting an error with a push_back() function call on a vector of structs

From Dev

std::vector's push_back() causing a strange compile-time error message

Related Related

  1. 1

    Vector of shared_ptr resulting in error: "no instance of overloaded function" when wanting to push_back vector list

  2. 2

    Vector of shared_ptr resulting in error: "no instance of overloaded function" when wanting to push_back vector list

  3. 3

    c++ - runtime error: vector push_back()

  4. 4

    vector push_back in STL?

  5. 5

    Vector and push_back() behavior

  6. 6

    Unable to push_back to vector

  7. 7

    vector push_back zero into an empty vector

  8. 8

    push_back a vector of vectors into a vector

  9. 9

    Insert or push_back to end of a std::vector?

  10. 10

    Implementatnion of std::vector::push_back in MSVC

  11. 11

    vector::push_back and std::move

  12. 12

    Why are there two overloads for vector::push_back?

  13. 13

    string Vector push_back failing in class

  14. 14

    garbage values for vector push_back

  15. 15

    push_back new element to vector

  16. 16

    Implementatnion of std::vector::push_back in MSVC

  17. 17

    garbage values for vector push_back

  18. 18

    Vector push_back incredibly slow

  19. 19

    Vector push_back Array of doubles

  20. 20

    Push_back variadic function parameters into a vector?

  21. 21

    c++ push_back() a struct into a vector

  22. 22

    vector of reference wrapper, push_back failure?

  23. 23

    push_back a pointer to a vector of pointers

  24. 24

    Singly linked list - push_back

  25. 25

    STL list segmentation fault on push_back

  26. 26

    Error with Push_back function for a LinkedList?

  27. 27

    std vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

  28. 28

    Visual Studio Reporting an error with a push_back() function call on a vector of structs

  29. 29

    std::vector's push_back() causing a strange compile-time error message

HotTag

Archive