Thread: Error from vector push_back()

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92

    Error from vector push_back()

    Hi

    I am trying to add an element to a vector with this code:

    Code:
    //newMaterial is initted here a bit above:
    
    tMaterial newMaterial = {0};
    
    //Here is the push_back function
    
    pModel->numOfMaterials++;
    pModel->pMaterials.push_back(newMaterial);
    memset(&(pModel->pMaterials[pModel->numOfMaterials - 1]), 0, sizeof(newMaterial));
    The first element that I add works fine, but the second makes the program crash with this error (in MSVC 7):

    Debug error!

    DAMAGE: after normal block (#53) at 0x003DF6C0

    I have a similar function that looks like this, and as far as I know it doesn't crash after more than one object:

    Code:
    tDobj newObject = {0};
    
    pModel->numOfObjects++;
    pModel->pObject.push_back(newObject);
    memset(&(pModel->pObject[pModel->numOfObjects - 1]), 0, sizeof(newObject));
    Anyone know if there's anything wrong here, or do I need to post more code?

    Thanks in advance

    The Wazaa

    Edit:

    Here is the declaration of those vectors:
    Code:
    	vector<tDobj> pObject;
    	vector<tMaterial> pMaterials;
    Last edited by The Wazaa; 03-11-2006 at 10:29 AM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see anything wrong technically with that code. Why are you keeping a separate count of objects when you can use the vector's size() method? Perhaps that count is off. Why are you using memset? Can you add a clear function to tMaterial that clears its data on its own? What does the definition of tMaterial look like? The crash could also be related to an error accessing other parts of pModel.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    This code is a rewritten version of a tutorial that I read somewhere, the reason that I use a counter is that it is a bit easier to handle it when you mess around with the models.
    Memset was in the tutorial, but only on the pObject push_back so when pMaterials didn't work I tried to add the same there but it didn't work.
    This is the definition of tMaterial:
    Code:
    struct tMaterial
    {
    	char name[255];
    	char fileName[255];
    	unsigned char color[3];
    	int textureId;
    	int transparency;
    };
    
    //This is the model class:
    class model
    {
    public:
    	model();
    	void model::render();
    	void model::render(vector3 pos, bool selected, int name);
    	void model::move(vector3 speed);
    	void model::rotate(vector3 direction, float angle);
    	void model::setPos(vector3 newPos);
    	~model();
    
    	int numOfObjects;
    	int numOfMaterials;
    	vector<tDobj> pObject;
    	vector<tMaterial> pMaterials;
    
    	vector3 position;
    	quaternion rotation;
    	char fileName[255];
    	int modelIndex;
    	
    };
    Thanks for your reply

    The Wazaa

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Why the scope declarations for those methods in the model class when they are in the desired scope?

    Did you initialize numOfMaterials in the constructor?
    Last edited by Dae; 03-11-2006 at 11:40 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    I like to declare it like this, even though its unnecessary :-/

    And, yes I did. The value of numOfMaterials is correct when I check it in the function.

    Thanks for your reply

    The Wazaa

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I can't see anything that would specifically cause the issue.

    Remember that it might be completely unrelated to the push_back or memset, for example, perhaps pModel is an invalid pointer, or some other data inside it got corrupted (like maybe fileName).

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    But if it is corrupted, why does it work the first time? It reads in a lot of data before it crashes so it might be something of that, but I can't see anything. I'll look into it, and maybe post some more code later ^^

    Thanks for your replies

    The Wazaa

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It is undefined behavior which means anything can happen. If it is corrupted, perhaps the first time you overwrite memory that is not important, but the second, you overwrite memory that causes the crash.

Popular pages Recent additions subscribe to a feed