simple pointers question

Nov 17, 2011 at 3:58pm
Hello,
I have a class with some member fields that are pointers. I'm trying to set the pointer member fields with mutators (setters) but I'm getting memory access errors when I try to do this. This is how it's looking at the moment.

Class header containing member fields

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// include headers

class Model
{
    public:
    // Constructors and other Methods
    
    // Mutators
    void SetTexture(const BYTE *texture);
    void SetPalette(const Gdiplus::Color *palette);

    private:
    BYTE* _texture;
    Gdiplus::Color* _palette;
}


And here's the implementation of the mutator's in the model.cpp file

1
2
3
4
5
6
7
8
9
void Model3D::SetTexture(const BYTE *texture)
{
    *_texture = *texture;
}

void Model3D::SetPalette(const Gdiplus::Color *palette)
{
    *_palette = *palette;
}


The mutator's take a pointer as a parameter as you can see except when they are called I'm getting this memory access violation. I'm pretty sure the mutator's are wrong, how should I be doing this?

:)
Nov 17, 2011 at 4:02pm
Oh btw I missed 3D from the end of Model in the first code sample.
Nov 17, 2011 at 4:16pm
@codejunky99
Before use "*_texture = *texture;" & "*_palette = *palette;",
did you use "_texture = new BYTE;" & "_palette = new Gdiplus::Color"?
Nov 17, 2011 at 4:17pm
Oh no I didn't i think you've just hit the nail on the head. Thanks :)
Nov 17, 2011 at 4:45pm
_texture and _palette are probably uninitialized
Nov 17, 2011 at 5:03pm
They're definitely not uninitialized, I did actually noticed I'd set them to NULL rather than what cdlang said to change them to in the constructor, but that was giving me the error whereas I'm not getting the error when I initialize them with the new keyword and var type.

I also forgot to delete them in the destructor but that wasn't related to the error I was getting. Strangely the guide I'm following does tell me to initialize them to NULL.
Topic archived. No new replies allowed.