Jump to content

C++ SDL problems

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Ninjaboi

Ninjaboi

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
Language: C++
Library: SDL
Problem: Exception error
Compiler: Visual Studio 2008 compiler
Environment: Visual Studio 2008 Professional

Not quite sure how to title this problem, but I'll just spell it out. I've made a program using SDL, and upon running, I get an exception handle that requests a break or continue to the application. Upon breaking, it then shows me the debugging information.

Quote

- file 0x00372f98 {flags=0 format=0x00372ff0 w=640 ...} SDL_Surface *
flags 0 unsigned int
- format 0x00372ff0 {palette=0x00000000 BitsPerPixel='' BytesPerPixel='' ...} SDL_PixelFormat *
- palette 0x00000000 {ncolors=??? colors=??? } SDL_Palette *
ncolors CXX0030: Error: expression cannot be evaluated
colors CXX0017: Error: symbol "" not found
BitsPerPixel 24 '' unsigned char
BytesPerPixel 3 '' unsigned char
Rloss 0 unsigned char
Gloss 0 unsigned char
Bloss 0 unsigned char
Aloss 8 '' unsigned char
Rshift 0 unsigned char
Gshift 8 '' unsigned char
Bshift 16 '' unsigned char
Ashift 0 unsigned char
Rmask 255 unsigned int
Gmask 65280 unsigned int
Bmask 16711680 unsigned int
Amask 0 unsigned int
colorkey 0 unsigned int
alpha 255 'ÿ' unsigned char
w 640 int
h 480 int
pitch 1920 unsigned short
pixels 0x00980020 void *
offset 0 int
hwdata 0x00000000 private_hwdata *
- clip_rect {x=0 y=0 w=640 ...} SDL_Rect
x 0 short
y 0 short
w 640 unsigned short
h 480 unsigned short
unused1 0 unsigned int
locked 0 unsigned int
map 0x0037a030 SDL_BlitMap *
format_version 1 unsigned int
refcount 1 int
- fileoptimized 0x00000000 {flags=??? format=??? w=??? ...} SDL_Surface *
flags CXX0030: Error: expression cannot be evaluated
format CXX0030: Error: expression cannot be evaluated
w CXX0030: Error: expression cannot be evaluated
h CXX0030: Error: expression cannot be evaluated
pitch CXX0030: Error: expression cannot be evaluated
pixels CXX0030: Error: expression cannot be evaluated
offset CXX0030: Error: expression cannot be evaluated
hwdata CXX0076: Error:
- clip_rect {x=??? y=??? w=??? ...} SDL_Rect
x CXX0030: Error: expression cannot be evaluated
y CXX0030: Error: expression cannot be evaluated
w CXX0030: Error: expression cannot be evaluated
h CXX0030: Error: expression cannot be evaluated
unused1 CXX0030: Error: expression cannot be evaluated
locked CXX0030: Error: expression cannot be evaluated
map CXX0076: Error:
format_version CXX0030: Error: expression cannot be evaluated
refcount CXX0030: Error: expression cannot be evaluated


That is the information it provides. It's so strange, I really can't figure it out. I'm not great at debugging with the debugger, I usually only get the simple problems and fix it. The main section of code it's complaining about is a header file I made for loading images.

load_image.h:


// Header guard, which ensures no multiple delcarations

// or calls to this header file.

#ifndef LOAD_IMAGE

#define LOAD_IMAGE


// 'Load_Image()' when called loads an image specified.

SDL_Surface* Load_Image( const char *filename, bool colorkey = false )

{


	// Image file's temporary holder.

	// Loads the image specified in the parameters into

	// temporary holder.

	SDL_Surface* file = NULL;

	

	file = IMG_Load( filename );


	SDL_Surface* fileoptimized = NULL;


	[B]fileoptimized = SDL_DisplayFormat( file );[/B]


	Uint32 colorkeyi = SDL_MapRGB( fileoptimized->format, 0xFF, 0xFF, 0xFF );


	SDL_SetColorKey( fileoptimized, SDL_SRCCOLORKEY, colorkeyi );


	if( colorkey == true )

	{


		SDL_FreeSurface( file );


		return fileoptimized;

	

	}


	// Returns the loaded image.

	return file;


}


#endif

// End of header file guard.




It particularly whines about the line I've bolded in the code above. I'm sure it's not the direct problem, but you never know. I know it's a bit sloppy, and it's not truly well commented, but I've been at this one problem for a day or so now and haven't slept, so you can be aware that I just want it to work before I will waste time commenting more code that might not work.

Any help would be greatly appreciated :cool:.

P.S. If you need any extra code/info, please ask. I'm happy to provide.

EDIT: Sorry, I forgot to add the exception error I receive upon running the application.

Quote

Unhandled exception at 0x68129d00 in program.exe: 0xC0000005: Access violation reading location 0x0000013c.

It's really just memory violations, but you can be the judge of that.

#2
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
I haven't used SDL in like 3+ years so I'm not really sure. But maybe I can give some ideas, Is SDL_Init() being executed beforehand? Is the correct file type being passed to those functions that ask for a file? I would suggest taking out the optimizedfile portion of the code and see how it works with just the IMG_Load() in there to see if it is a problem with the entire function or just that part. Are your source and destination surfaces correct somewhere else in the program? Also maybe use strings for filename parameter like str.c_str().

Edited by Zer033, 15 April 2011 - 11:39 PM.


#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You might wanna take a look here, it's a great SDL tutorial.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

Ninjaboi said:

so you can be aware that I just want it to work before I will waste time commenting more code that might not work.
This is completely the wrong way to go about writing code comments, at least in my opinion. I write code comments while I'm writing the code, usually before even writing it, so that I know what the code was intended to do and possibly how you intend on it doing that. The point of the comments is to clarify parts of code that may not have a distinct purpose from the names and usage alone.

As far as the problem goes, this looks like you missed calling SDL_Init(), which if you had, will result in Access Violations. Check and make sure you've called SDL_Init() before the SDL_DisplayFormat() function.

Also, you never SDL_FreeSurface() the fileoptimized Surface before returning the normal one if colorkey != true. And despite only returning the standard surface, this method will create two SDL Surfaces even if colorkey == false, which it shouldn't.
Wow I changed my sig!

#5
Ninjaboi

Ninjaboi

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
@Zer033:

Taking out the fileoptimized part of the code, I did manage to get the program up and running with some problems with the images loaded ( at least it worked...sorta ). At least that reminded me of some debugging techniques to try out next time ( thanks! ).

@Flying Dutchman:

Thanks for the link, but I already had that bookmarked lol. I've been through all the tutorials on that site, as well as a few others. I also got the book "Focus on SDL" and have read through that too. It wasn't necessarily I'm terrible at using the SDL graphics library itself ( though I would say I'm not great at it ), but I am terrible at debugging things beyond the basics. If it's a simple compile error, I can get that no problem. It's the errors that aren't from compiling that can get me lol.

@ZekeDragon:

SDL_Init() was called before anything else, and all the subsystems were initiated with it too. I put it in a header named "init.h" along with anything else needed before running inside a boolean function called "Init()". In "main.cpp", I put Init() at the top of the main function. So that isn't the problem, especially since I've managed to have this working all the way up to this point ( and I had the images on the screen just fine before ). I'm not sure what I changed to do something like this, but I usually do a compile after a few lines I'm not 100% on ( like the ones I've shown ). I removed the SDL_FreeSurface() and am only freeing surfaces that are permanent ( ones that call Load_Image() ). Still however, I need to be able to call SDL_DisplayFormat() so that I can convert the image into a format where I can use colorkeys.


Thanks for all the help guys! More is appreciated, and I'll be posting here if I ever fix the issue.

EDIT: I'm now sure it's SDL_DisplayFormat() being called that's the problem. I've managed to get not access violations until I put that line in, and I've tried it a few different ways. Perhaps re-downloading the library section that has that function to see if it's the library itself that's the problem?

EDIT2: Fixed it! It was the library that was the issue. I also checked to see what the problem was after I replaced the old lib file with the new one, and realized the old one had some strange values set to ncolors and colors ( the issues pointed out in the debugging information I provided ).

Thanks for all the help guys, it was just a faulty function!

Marking thread 'Solved'.

:lol::lol::lol::lol::lol:
:cool::cool::cool::cool::cool:

EDIT3: Sorry for all the edits, but I can't figure out how to mark the thread solved lol. If anyone can do it for me or tell me how, I'd like to do so.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users