C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2007, 09:45 PM   #1
Registered User
 
Join Date: Nov 2005
Posts: 627
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.
Raigne is offline   Reply With Quote
Old 10-10-2007, 09:50 PM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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?
brewbuck is offline   Reply With Quote
Old 10-10-2007, 10:02 PM   #3
Registered User
 
Join Date: Nov 2005
Posts: 627
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.
Raigne is offline   Reply With Quote
Old 10-10-2007, 10:58 PM   #4
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Have you initialized SDL properly?
Have you set the display flags correctly?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 10-10-2007, 11:40 PM   #5
Registered User
 
Join Date: Nov 2005
Posts: 627
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
Raigne is offline   Reply With Quote
Old 10-11-2007, 08:16 AM   #6
Unregistered User
 
Yarin's Avatar
 
Join Date: Jul 2007
Posts: 925
The file your reading is open maybe, have you tried reading in shared mode?
__________________
GCC 4.4.0, Code::Blocks 8.02, Fedora 11, x64
Yarin is offline   Reply With Quote
Old 10-11-2007, 08:25 AM   #7
Registered User
 
Join Date: Jan 2007
Location: Euless, TX
Posts: 135
Are you sure that SDL_DisplayFormat(t_sfc) returns a pointer value? Maybe check to see if it's NULL?
kcpilot is offline   Reply With Quote
Old 10-11-2007, 10:52 AM   #8
Registered User
 
Join Date: Nov 2005
Posts: 627
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 11:11 AM.
Raigne is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:18 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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