To be honest I do not know if the title is correct for the issue I am experiencing. The issue is thus. I have a class called Engine, of which there is one instance.
It contains two member variables (among others) called testTexture, an instance of my custom Texture class, and testObject, an instance of my custom object class.
In the Engine function Init their values are set thusly:
testTexture = Texture(0, TEXT("D:\\spriteWallVertical112.png"),
renderer.ReturnDevice());
testObject = Object(0,testTexture.textureID, D3DXVECTOR3(0,0,0),
D3DXVECTOR3(100,100,100), testTexture.texture, &renderer);
This all appears to function as I would want, their values are stored and appear to be maintained fine.
However, inside the Object class constructor there is a call to a function in my Renderer class called AddNewTextureObject:
rendererPointer->AddNewTextureObject(&objectID, &textureID, textureInput,
&origin, &coordinates);
This appears to go fine, but as the program runs the values the pointers appear to get overwritten as the program goes on. They don’t instantly become junk memory, but it seems clear that they are. I can provide code as needed, but I don’t want to just spam this question with code that isn’t relevant to the question, especially if someone else may see something obvious that I am doing wrong.
I will however post the TextureObject class code for now as I think it’s the most relevant here:
#ifndef TEXTUREOBJECT_H
#define TEXTUREOBJECT_H
#ifndef UNICODE
#define UNICODE
#endif
#include <d3dx9.h>
class TextureObject
{
public:
TextureObject();
TextureObject(unsigned int *, int *, LPDIRECT3DTEXTURE9, D3DXVECTOR3 *, D3DXVECTOR3 *);
~TextureObject();
unsigned int *objectID; // The object with the texture. Use this for locating and deleting this instance of TextureObject.
int *textureID;
LPDIRECT3DTEXTURE9 texture; // May not be needed if we can simply select the texture via ID.
const D3DXVECTOR3 *origin; // Needed for drawing rotations....I think.
D3DXVECTOR3 *coordinates;
int maintainMe;
};
#endif
The variable maintainMe does keep its value if I assign to it.
This is the code for the AddNewTextureObject() function:
void Renderer::AddNewTextureObject(unsigned int *objectIDInput, int *textureIDInput, LPDIRECT3DTEXTURE9 textureInput, D3DXVECTOR3 *originInput, D3DXVECTOR3 *coordinatesInput)
{
//testTextureObject = TextureObject(objectID, textureID, textureInput, originInput, coordinatesInput);
testTextureObject.objectID = objectIDInput;
testTextureObject.textureID = textureIDInput;
testTextureObject.texture = textureInput;
testTextureObject.origin = originInput;
testTextureObject.coordinates = coordinatesInput;
testTextureObject.maintainMe = 3067;
Note that either method of assigning values to testTextureObject results in the issue.
Any assistance with this would be greatly appreciated.
EDIT:
Here is the constructor for the Object class:
Object::Object(unsigned int objectIDInput, int textureIDInput, D3DXVECTOR3 originInput, D3DXVECTOR3 coordinatesInput, LPDIRECT3DTEXTURE9 textureInput, Renderer *rendererInput)
{
objectID = objectIDInput;
textureID = textureIDInput;
origin = originInput;
coordinates = coordinatesInput;
rendererPointer = rendererInput;
rendererPointer->AddNewTextureObject(&objectID, &textureID, textureInput, &origin, &coordinates);
}
It is declared in Object.h in the Object class as public like this:
Object(unsigned int, int, D3DXVECTOR3, D3DXVECTOR3, LPDIRECT3DTEXTURE9, Renderer *);
EDIT2: I made a copy constructor and an assignment operator:
Object::Object(const Object &source)
{
objectID = source.objectID;
textureID = source.textureID;
texture = source.texture;
origin = source.origin;
coordinates = source.coordinates;
rendererPointer = source.rendererPointer;
}
Object& Object::operator=(const Object &source)
{
if(this == &source)
{
return *this;
}
objectID = source.objectID;
textureID = source.textureID;
texture = source.texture;
origin = source.origin;
coordinates = source.coordinates;
rendererPointer = source.rendererPointer;
return *this;
}
Do these look correct to you more experienced people? This alone does not appear to fix the issue unfortunately.
Since you define a destructor, and you have pointers in your
TextureObjectclass, you need to follow the rule of 3: define a destructor, copy constructor, and assignment operator. It seems the pointers may have originated fromObject, so you may need to do the same for that class as well.I imagine the problem you are facing is a dangling pointer issue, in that after your initialization of
testObject, the temporary used to initialize it destructs, and frees the pointers that were initialized within it. Thus,testTextureObjectis now holding pointers to freed memory (because those pointers originally came from the temporary).Edit: Based on the constructor for
Object, we see thatrendererPointer->AddNewTextureObjectis being passed pointers from the currentObjectinstance, which would be the temporary one.This line of code creates a temporary instance of
Object, and then uses the assignment operator to initializetestObject. After this line of code, the temporary would be destructed. Nowrendereris holding aTextureObjectthat is initialized to pointers from a temporary that no longer exists.Edit: You seem to have some confusion about the problem that the rule of 3 is trying to help you resolve. You can read the the accepted answer to the question about the rule of 3. But to give you a simple example, just consider the simple problem of a class that allocates memory.
The destructor is required to not leak memory. However, there are problems introduced if the copy constructor or the assignment operator is used.
The problem with
bis that it holds the same pointer asa. So whenbandaare destructed, the pointer will get deleted twice.The problem with
cis that not only is it holding the same pointer asa(which will lead to double deletion), but whatever memory it created in its constructor has now been leaked.The rule of 3 is: If a destructor is needed, then so is a copy constructor and an assignment operator. The purpose of the rule is to make the developer think about the problems that needed to be solved by adding a destructor, and what consequences those would have for copy constructions and assignment, and create a reasonable solution.
In your case,
rendereris holding aTextureObjectcreated by the temporaryObject. You need to think about how to fix the situation, either in the destructor, copy constructor and assignment operator ofObject, or by avoiding the problem with some other solution.