Thread: Vector of pointers and scope

  1. #1
    Lode Runner
    Join Date
    May 2004
    Posts
    53

    Vector of pointers and scope

    I have two classes that represent 3D objects :
    1) Mesh, which contains a single simple Mesh
    2) Model, which is basically a vector of meshes

    Here are my headers :
    Code:
    #include "mesh.hpp"
    #include <vector>
    
    using namespace std;
    
    class Model {
    public:
    	void addObject(Mesh *newObject);
    	void draw();
    	void description(char * s);
    private:
    	vector<Mesh*> objects;
    };
    Code:
    class Mesh {
    public:
          /*------- public methods, getters and setters -----*/
    private:
    	unsigned int numVertex;
    	unsigned int numFaces;
    	GLfloat (* vertex)[3];
    	unsigned int (* face)[3];
    	unsigned int numTVertex;
    	GLfloat (* tVertex)[2];
    	unsigned int numTVFaces;
    	unsigned int (* tFace)[3];
    	GLfloat (* faceNormal)[3];
    	GLfloat (* vertexNormal)[3];
    };
    I implement the addObject method like this
    Code:
    void Model::addObject(Mesh *newObject) {
      objects.push_back(newObject);
    }
    I initialize a Model like this :
    Code:
    Mesh *newMesh;
    for(i=0;i<objectCount;i++) {
           /*---- newMesh initializing code here -----*/     
    	model->addObject ( newMesh );
    }
    The Mesh initializing code works very well, I can see that for each i, newMesh is what I want it to be. But I have two problems.

    1) The addObject only works for the first. After that I get insane values for the next objects I add.

    2) the newMesh pointer is valid only in the scope of the initializing function. Once the program that function, all my vector points to NULL.

    If anyone has an idea for any of the two problems, please help. Thanks everyone.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Your code as is looks fine, you didn't post the interesting part.

    You pretty much have to allocate the Mesh objects dynamically on the heap with new before pushing them back onto your model's vector. An object allocated on the stack will be destroyed when it goes out of scope, so even if you store a pointer to that object you will still get garbage after it goes out of scope. If you dynamically allocate your Mesh, then it will not be destroyed until you explicitly call delete to destroy it. Incidentally, in your Model destructor (or some other cleanup method) you will want to add code that loops through your object vector to delete the memory for each pointer in the vector.

    That is just my guess at your problem, but if you already are allocating your Mesh objects with new then something else is wrong.

    > 2) the newMesh pointer is valid only in the scope of the initializing function. Once the program that function, all my vector points to NULL.

    The vector holds whatever you put into it, so if it is holding NULL, then you pushed_back NULL. Did you check the size of your vector to make sure it holds the correct number of pointers? Can you post the code you left out that creates the meshes and adds them inside the for loop?

  3. #3
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    I could post my mesh creating code but it's long and boring. I import from a file, so its lines and lines of fscanf and fgets.

    but it works very well, I see in gdb that my newMesh instances are correct. but, even before I leave the function, the values in the vector are incorrect. I know this doesn't make sense. It actually worked with the same code (as far as i can remember) before.

    But I'll try checking the size to see. If you are really interested, I attached my mesh generating class as a zip. But as I told you, most of it is file reading, and it works very well.

    Thanks for the help

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    More specifically, I assume the line of code in red (or something similar) can be found in that for loop. The pointer has to point to something.
    Code:
    Mesh *newMesh;
    for(i=0;i<objectCount;i++) {
    	newMesh = new Mesh;
           /*---- newMesh initializing code here -----*/     
    	model->addObject ( newMesh );
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The STL containers make extensive use of copy constructors when pushing/inserting objects. For most data types, the default copy constructors generated automatically by the compiler work fine. This will however break when dealing with pointers and dynamically allocated memory. I notice that your Mesh object contains many pointers. Have you also implemented a copy constructor for it? There may be other aspects of the Mesh class that you should also think about implementing, the assignment operator for instance.

    [edit]I guess that the above would be more for a vector<Mesh> rather than a vector<Mesh*>... but it might not hurt to do it anyway[/edit]
    Last edited by hk_mp5kpdw; 11-22-2004 at 12:54 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Lode Runner
    Join Date
    May 2004
    Posts
    53
    I think you're right. It looks like that. Even when my variables are correct, my pointers are wrong (because they point to the same pointer than the original Mesh). I tried to implement my copy constructor but it just gave me more segmentation faults.

    Could you just outline the overloading of the copy constructor please ? It would really help. Sorry for getting down to the basics, this is the first time I use C++.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Sample copy constructor code:

    Code:
    class example
    {
        char * name;
    public:
        example(const example& rhs)  // Copy constructor
        {
            name = new char[strlen(rhs.name)+1];
            strcpy(name,rhs.name);
            name[strlen(rhs.name)] = 0;
        }
    };
    Your copy constructor would probably need to implement something similar for each pointer variable in your class.

    [edit]Found a problem with my example, added the & above.[/edit]
    Last edited by hk_mp5kpdw; 11-23-2004 at 08:54 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of pointers and assignment within function
    By mmfuller in forum C++ Programming
    Replies: 11
    Last Post: 06-06-2008, 08:47 AM
  2. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Auto_ptr = pointers for dummies?
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-03-2003, 09:47 PM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM