Pretty sure this counts as C++, Pointers losing their Type?

This is a discussion on Pretty sure this counts as C++, Pointers losing their Type? within the C++ Programming forums, part of the General Programming Boards category; Here is a link to a post I made elsewhere, no one there knows what to say to my problem, ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Pretty sure this counts as C++, Pointers losing their Type?

    Here is a link to a post I made elsewhere, no one there knows what to say to my problem, maybe on of you guys can help

    http://www.gamedev.net/community/for...opic_id=364947
    Attached Files Attached Files
    Last edited by Shamino; 12-19-2005 at 10:43 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,804
    First off. I don't see any checks for obj == NULL in your code. Got to make sure you Obj isn't NULL.
    Woop?

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Not it, Obj definately has something in it the second time around. I put an if (Obj != NULL) around the whole function.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The error is unlikely to be in the code you have posted. The error is likely to be where you have filled out the ship information and memory allocation. Memory problems (overruns, allocation, deallocation) often don't show up immediately.

    Your code contains a lot of pointers. Can I suggest you minimise your use of pointers and use methods such as vectors (or deques) and references? new/delete can usually be avoided.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    The only place I allocate and de-allocate memory is where I load the MS3D Model into memory, I'm pretty sure that isnt broken because I got that directly from NeHe, a couple variable names were changed but only for increased readability, I never had a problem with it until now.

    I'll post my whole project if need be :\.....

    Question, when creating a pointer to an object, do we have to use the new operator? When I create my two ship objects I use the new operator, I havn't really gotten around to de-allocating them yet...

    I attached all files that I can find any relevancy to my problem in.

    All global variables are for testing purposes, Ship1 and Ship2 are global variables. As well as Obj.
    Last edited by Shamino; 12-19-2005 at 10:43 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    struct Models
    {
    	MS3DModel* Model1;
    };
    
    class Object
    {
    public:
    	virtual ~Object() {}
    	float locx, locy, locz;
    	bool MarkedForDeletion;
    	Models mModels;
    	virtual MS3DModel* GetRenderInformation() = 0;
    	virtual void GetPhysicsInformation() = 0;
    	virtual void GetSoundInformation() = 0;
    	virtual void Update() = 0;
    };
    
    class Ship : public Object
    {
    public:
    
    	Ship(float x, float y, float z)
    	{
    		x = locx;
    		y = locy;
    		z = locz;
    	}
    
    
    	MS3DModel* GetRenderInformation()
    	{
    		return this->mModels.Model1;
    	}
    Is memory for Model1 allocated somewhere?

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Anonytmouse, you hit the nail on the head, yes it is, but not in a good way...


    Let me paste a few bits of code.

    Code:
    	Ship1->mModels.Model1 = new MS3DModel();
    
    	if ( Ship1->mModels.Model1->LoadModelData( "data/model2.ms3d" ) == false )// Loads The Model And Checks For Errors
    	{
    		MessageBox( NULL, "Couldn't load the model data\\model.ms3d", "Error", MB_OK | MB_ICONERROR );
    		return 0;													// If Model Didn't Load Quit
    	}
    Apparently, I'm only loading Ship1's instance of Model1....

    Problem is, I don't want to do one for Ship2's instance as well, I want them to be able to use a single model pointer from memory...

    This is why Ship2 is broken, it doesn't have anything loaded..

    What do I do to make it so that any Object can use a single pointer to the Memory allocated for this model?

    Do I have to copy a single pointer in the constructor of each of my ship objects?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    If all the objects are of the same class (or inherit from the same base class, which appears to be the case), you could have a static member pointer in that class that holds the model. Just initialize it properly in the constructor:
    Code:
    if (NULL == model){
      //initialize model for the class
    }
    Oh - and at the declaration point of the static member pointer, set it to NULL, so the first object that gets instantiated will initialize the model. After that, all will reference the same model.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  4. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21