Unable to access pointer
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Unable to access pointer

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Unable to access pointer

    Hi all,

    I seem to be unable to access member pointers to structs. Here are the struct declarations in the class:

    Code:
    SpriteStruct* first;
    SpriteStruct* last;
    Any time I try to access first or last, I get an access violation at 0x00000000. Examples:

    Code:
    first = NULL;
    if (first==NULL);
    Thank you in advance for any information regarding this problem!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Unable to access pointer

    Quote Originally Posted by hockeyb View Post
    Hi all,

    I seem to be unable to access member pointers to structs. Here are the struct declarations in the class:
    Your program must be much longer than 4 lines.

    Please post the actual code that duplicates the error. Setting a pointer to NULL and comparing it to NULL does not cause errors by itself -- your code is doing much more to cause the problem than what you've shown.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2009
    Posts
    3

    Re: Unable to access pointer

    That's what my team and I also thought at first, but this is the isolated portion of the code that causes the error. I'm sorry I wasn't clear, but the two statements in the bottom code section are in separate methods. I took out the first line after I saw that was where the access violation break point was and the second line also caused the same error at the same location.

    Here's the class:

    Code:
    #ifndef DIRECT2D_SOFTWARE_DEVICE
    #define DIRECT2D_SOFTWARE_DEVICE
    
    #include "windows.h"
    #include "windowsx.h"
    #include "d3d9.h"
    #include "d3dx9.h"
    #include "d3dx9math.h"
    #include "TerraEngine.h"
    #include <vector>
    
    using namespace std;
    
    #pragma comment (lib,"d3d9.lib")
    #pragma comment (lib,"d3dx9.lib")
    
    class Direct2DSoftwareDevice 
    {
    public:
    	void Start();
    	void SpriteToDrawBuffer(LPDIRECT3DTEXTURE9 inSprite,
    					   D3DXVECTOR3 inPosition,
    					   D3DXVECTOR3 inCenter,
    					   D3DCOLOR inColor,
    					   RECT* inSrcRect,
    					   LPDIRECT3DDEVICE9 D3DDevice);
    	void RenderDrawBuffer();
    	void Clean();
    private:
    	//Keeps information about a sprite to be drawn
    	//and implements a linkedlist structure
    	struct SpriteStruct
    	{
    		LPD3DXSPRITE D3DSprite;
    		RECT* pSrcRect;
    		D3DXVECTOR3 Position;
    		D3DXVECTOR3 Center;
    		D3DCOLOR Color;
    		LPDIRECT3DTEXTURE9 SpriteTexture;
    		SpriteStruct* next;
    	};
    
    	//Sprite draw buffer
    	SpriteStruct* first;
    	SpriteStruct* last;
    };
    #endif
    And the first method that was causing the error:
    Code:
    void Direct2DSoftwareDevice::Start()
    {
           //!!!!!!!!!!
           //DIES HERE
           //!!!!!!!!!!
    	first = NULL;
    	last = NULL;
    }
    And the second method that was causing the error:
    Code:
    void Direct2DSoftwareDevice::SpriteToDrawBuffer(LPDIRECT3DTEXTURE9 inSprite,
    					   D3DXVECTOR3 inPosition,
    					   D3DXVECTOR3 inCenter,
    					   D3DCOLOR inColor,
    					   RECT* inSrcRect,
    					   LPDIRECT3DDEVICE9 D3DDevice)
    {
    	LPD3DXSPRITE d3dspt;
    	D3DXCreateSprite(D3DDevice, &d3dspt);
    	SpriteStruct ss;
    	ss.D3DSprite = d3dspt;
    	ss.Center = inCenter;
    	ss.Color = inColor;
    	ss.Position = inPosition;
    	ss.pSrcRect = inSrcRect;
    	ss.SpriteTexture = inSprite;
    	ss.next = NULL;
    
           //!!!!!!!!!!
           //DIES HERE
           //!!!!!!!!!!
    	if (first==NULL)
    	{
    		first = &ss;
    		last = &ss;
    	} else
    	{
    		SpriteStruct* oldLast = last;
    		last = &ss;
    		oldLast->next = last;
    	}
    	SpriteStruct st;
    }

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Unable to access pointer

    This probably has nothing to do with the fact that the members you're accessing are pointers. Notice that in both cases, the code dies the first time any member of the Direct2DSoftwareDevice class is touched.

    This probably means that you're trying to call these methods on something which isn't a valid Direct2DSoftwareDevice object. Usually this can happen if you're trying to access the object via an invalid Direct2DSoftwareDevice*.

  5. #5
    Join Date
    Nov 2006
    Posts
    1,611

    Re: Unable to access pointer

    Set a breakpoint at two positions, such that the debugger stops at, say:

    Code:
    void Direct2DSoftwareDevice::Start()
    {
           //!!!!!!!!!!
           //DIES HERE
           //!!!!!!!!!!
    
    
    	first = NULL;  //<< so debugger stops here
    	last = NULL;
    }
    Then, tell us what the value of "this" is.
    Then, look at the call stack and find the code where "Start" is called
    ..from there, look for the object upon whose instance Start is called,
    and look further backwards until you find where that object is created.

    Tell us the code you have there.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  6. #6
    Join Date
    Jul 2009
    Posts
    3

    Re: Unable to access pointer

    Thank you Lindley! You were spot on. A pointer to an instance of the class wasn't properly initialized. Doh. Thank you JVene for your help as well. I'll try to remember to make the instance before I use its non-static methods next time. =)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Unable to access pointer

    Quote Originally Posted by hockeyb View Post
    Thank you Lindley! You were spot on. A pointer to an instance of the class wasn't properly initialized
    That is why you should have written a constructor for that struct, making sure that when you create one, all members are initialized:
    Code:
    struct SpriteStruct
    	{
    		LPD3DXSPRITE D3DSprite;
    		RECT* pSrcRect;
    		D3DXVECTOR3 Position;
    		D3DXVECTOR3 Center;
    		D3DCOLOR Color;
    		LPDIRECT3DTEXTURE9 SpriteTexture;
    		SpriteStruct* next;
                    
                    SpriteStruct() : D3DSprite(0), pSrcRect(0),
                                             Position(0), Center(0), Color(0),
                                            SpriteTexture(0), next(0) { }
              };
    Out of curiousity, what is the "next" parameter supposed to denote? Is this a linked list? If so, maybe that should just be a std::list<SpriteStruct*> or something similar to that, and forget about the "next" member variable.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center