Do the Access Violations every end?

This is a discussion on Do the Access Violations every end? within the C++ Programming forums, part of the General Programming Boards category; I've been working on a game engine for the past 2 weeks, I've run into some wierd errors throughout my ...

  1. #1
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215

    Do the Access Violations every end?

    I've been working on a game engine for the past 2 weeks, I've run into some wierd errors throughout my time, but this one takes the pie. I've debugged it from start to finish, watching the addresses carefully. I noticed that the function that causes the error SETS an argument to 0x00000000 or NULL and that seems to be causing the access violation. I watched that argument all the way down to the function and it's definitely not NULL. Here's some code:

    Error Function
    Code:
    BOOL ArkButton::Init(ArkCore *Ark, LPCTSTR TextureFilename, int MaxButtons)
    {
    	if( Ark == NULL || TextureFilename == NULL || MaxButtons == NULL )
    		return FALSE;
    	if( Ark->GetDevice() == NULL )
    		return FALSE;
    
    	*m_Button = new sButton[MaxButtons];
    
    	if(FAILED(D3DXCreateTextureFromFile(Ark->GetDevice(), TextureFilename, &m_Texture)))  // ACCESS VIOLATION, ARK IS THE SUPPOSED NULL OBJECT(NOTE THE CHECK FOR IT)
    		return FALSE;
    And the call:
    Code:
    /* PRE INSTANTIATED OBJECTS **********
    ArkCore* AC;
    ArkButton* AB;
    **********************************/
    
    AB->Init(AC, "C:\\UI\\Buttons.jpg", 2); // The call
    I'm stumped, any ideas?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I suspect you're setting a pointer to NULL, rather than setting whatever the pointer points-to to NULL.

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Well what does Ark->GetDevice() return? I would assume a pointer to an IDirect3DDevice9.
    Also why are you checking an int to be NULL?
    I don't know much about D3D but are you sure you shouldn't be checking something like
    Code:
    if(Ark->GetDevice() == INVALID_DEVICE)
    {
      //oh crap
    }
    My logic is based on this code
    Code:
    #include <iostream>
    
    class Ark
    {
      public:
        Ark()
        {
          device = new int;
        }
        int *GetDevice()
        {
          return device;
        }
      private:
        int *device;
    };
    
    int main()
    {
      Ark *ark = new Ark;
      if(ark->GetDevice() == NULL)
      {
        std::cout<<"It's null"<<std::endl;
      }
      else
      {
        std::cout<<"It's ok"<<std::endl;
      }
      std::cin.get();
    }
    Even though it passes the NULL check it doesn't mean it is good data.
    Last edited by prog-bman; 10-12-2005 at 01:05 PM.
    Woop?

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Arg, I was allocating memory for the dereferenced double pointer, but I wasn't allocating memory for the actual double pointer. So on dereference of the double pointer, it was actually pointing to NULL.

    Solution:
    m_Button = new sButton();
    *m_Button = new sButton[];
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Cool glad I was no help to you
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

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