Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:02:58+00:00 2026-06-07T05:02:58+00:00

To be honest I do not know if the title is correct for the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:03:02+00:00Added an answer on June 7, 2026 at 5:03 am

    Since you define a destructor, and you have pointers in your TextureObject class, you need to follow the rule of 3: define a destructor, copy constructor, and assignment operator. It seems the pointers may have originated from Object, 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, testTextureObject is now holding pointers to freed memory (because those pointers originally came from the temporary).

    Edit: Based on the constructor for Object, we see that rendererPointer->AddNewTextureObject is being passed pointers from the current Object instance, which would be the temporary one.

    testObject = Object(0,testTexture.textureID, D3DXVECTOR3(0,0,0),
                        D3DXVECTOR3(100,100,100), testTexture.texture, &renderer);
    

    This line of code creates a temporary instance of Object, and then uses the assignment operator to initialize testObject. After this line of code, the temporary would be destructed. Now renderer is holding a TextureObject that 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.

    class Foo {
        Bar *bar;
    public:
        Foo () : bar(new Bar) {}
        ~Foo () { delete bar; }
        Bar * get_bar () { return bar; }
    };
    

    The destructor is required to not leak memory. However, there are problems introduced if the copy constructor or the assignment operator is used.

    Foo a;
    Foo b(a); // copy
    

    The problem with b is that it holds the same pointer as a. So when b and a are destructed, the pointer will get deleted twice.

    Foo a;
    Foo c;
    c = a;    // assign
    

    The problem with c is that not only is it holding the same pointer as a (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, renderer is holding a TextureObject created by the temporary Object. You need to think about how to fix the situation, either in the destructor, copy constructor and assignment operator of Object, or by avoiding the problem with some other solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To be honest, I do not really know how to name that problem. I'll
I know there are already some questions about this, but not that specific. I
To be honest, I'm not sure if this is an issue with my code
To be honest I'm not quite sure if I understand the task myself :)
I'll be honest, I am not sure of the terms I used in the
So to be honest the title says what I'm after. I've searched and tried
to be honest, I don't really know what the small green men in my
There's always skepticism from non-programmers when honest developers learn the techniques of black hat
So I have an app that if I'm honest doesn't really need transactional integrity
I'm working with QThread and slots/signals mechanism; I know there's a lot of threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.