Access violation... can't figure it out...

This is a discussion on Access violation... can't figure it out... within the C++ Programming forums, part of the General Programming Boards category; I keep getting an access violation with my code in this function.. Code: #include "stdafx.h" #include "CSprite.h" bool CSprite::LoadImage(const std::string ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Access violation... can't figure it out...

    I keep getting an access violation with my code in this function..
    Code:
    #include "stdafx.h"
    #include "CSprite.h"
    
    bool CSprite::LoadImage(const std::string &imgfile)
    {
        SDL_Surface* t_sfc = NULL;
        t_sfc = IMG_Load(imgfile.c_str());
        if ( t_sfc == NULL )
            return false;
        spriteSheet = SDL_DisplayFormat(t_sfc); //Access violation here
        SDL_FreeSurface(t_sfc);
        if ( spriteSheet == NULL )
            return false;
        return true;
    }
    this is the class definition...
    Code:
    #ifndef CSPRITE_H
    #define CSPRITE_H
    
    /* CSprite class definition */
    class CSprite
    {
    private:
        int x; int y;
        int direction;
        int xVel; int yVel;
    
        enum
        {
            UP = 0,
            DOWN = 1,
            LEFT = 2,
            RIGHT = 3
        };
        SDL_Surface* spriteSheet;
        std::vector<SDL_Rect> clips;
    public:
        CSprite();
        ~CSprite();
        
        bool LoadImage(const std::string& imgfile);
        void NewRect(int x, int y, int w, int h);
    
        void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
    };
    
    #endif
    I cant find the problem, but like always it will be something simple.
    The exact violation is
    Code:
    Unhandle exception at 0x68129410 in Varia_Engine.exe: 0xC0000005:
    Access violation reading location 0x00000013c.
    I am deeply thankful for any assistance. Hopefully I am clear enough.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Apparently you are trying to invoke CSprite::LoadImage through a null pointer. Check the function which calls CSprite::LoadImage. Is it calling it through a pointer to a CSprite? Is this pointer NULL, possibly?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    I tested it like this...
    Code:
    int main()
    {
      CSprite n;
      n.LoadImage("C:\\BSM.png"); //access violation.
    }
    I initialized spriteSheet to NULL in the constructor, but I thought you are suppose to NULL initialize pointers.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Have you initialized SDL properly?
    Have you set the display flags correctly?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    yea I used SDL_Init(SDL_INIT_EVERYTHING) and it is successful. I can get the image to load into t_sfc, but it will not let me assign the optimized image to spriteSheet. In the debugger spriteSheet just stays NULL

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,690
    The file your reading is open maybe, have you tried reading in shared mode?
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  7. #7
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    Are you sure that SDL_DisplayFormat(t_sfc) returns a pointer value? Maybe check to see if it's NULL?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Ah, yea SDL_DisplayFormat(t_sfc) is NULL, but why? I can't see why it would be.
    [EDIT]
    Thanks for the assistance. It was because I was initializing SDL in hardware memory, and my chipset is not valid for running that way. I had to switch to SWSURFACE. Thank again.
    [/EDIT]
    Last edited by Raigne; 10-11-2007 at 12:11 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 08:31 PM
  2. Istream::Release access violation? [C++]
    By A10 in forum Windows Programming
    Replies: 10
    Last Post: 01-13-2009, 10:56 PM
  3. Access violation when reading a string.
    By Desolation in forum C++ Programming
    Replies: 16
    Last Post: 05-01-2007, 11:25 AM
  4. Access Violation Error
    By Neurofiend in forum C Programming
    Replies: 3
    Last Post: 08-22-2005, 07:19 PM
  5. Help! CListCtrl access violation
    By bonkey in forum Windows Programming
    Replies: 4
    Last Post: 11-18-2003, 02:40 PM

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