SDL problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL problem

Post by Ginto8 »

ok what I see is that you're trying to dereference a pointer that points to memory location 0xC. 0xC is 12 in decimal. That means that it isn't trying to access NULL, but it also means that somewhere, your pointer gets screwed, because I don't think it's likely that any non-system program will access a memory location so low. Check ALL your pointers. EVERY ONE. Preferably their runtime values, with the debugger. If you can figure out which is causing the issue, you can figure out what chunk of code is the problem, and then you can fix the problem.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

Ginto8 wrote:ok what I see is that you're trying to dereference a pointer that points to memory location 0xC. 0xC is 12 in decimal. That means that it isn't trying to access NULL, but it also means that somewhere, your pointer gets screwed, because I don't think it's likely that any non-system program will access a memory location so low. Check ALL your pointers. EVERY ONE. Preferably their runtime values, with the debugger. If you can figure out which is causing the issue, you can figure out what chunk of code is the problem, and then you can fix the problem.
That's the problem, i tried flowing wich code does the problem, and what i ended up with, is that even the simplest of all codes:

Code: Select all

#include "SDL.h"

SDL_Surface *screen = NULL;

SDL_Event Event;
int main (int argc, char *argv[])
{
	SDL_Init(SDL_INIT_VIDEO);

	screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);

	SDL_Surface *img = SDL_LoadBMP("a.bmp");
	SDL_Surface *ConvertedImg = SDL_ConvertSurface(img,screen->format,SDL_HWSURFACE);

	while (true)
	{
		if (SDL_PollEvent(&Event) == 0)
		{
		}else{
			if (Event.type == SDL_QUIT)
				break;
		}
	}
	
	return 0;
}
does that error! that means that the problem is SDL_ConvertSurface (according to the debugger as well.) so i guess i'v used it incorrectly, maybe the type of image loaded, can't be converted...
Edit: The img isn't the problem. even when i removed it, the error is still there.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: SDL problem

Post by RyanPridgeon »

Code: Select all

#include "SDL.h"

SDL_Surface *screen = NULL;

SDL_Event Event;
int main (int argc, char *argv[])
{
   SDL_Init(SDL_INIT_VIDEO);

   screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);

   SDL_Surface *img = NULL;
   img = SDL_LoadBMP("a.bmp");
   if (img == NULL) return 1;

   SDL_Surface *ConvertedImg = SDL_DisplayFormat(img);

   while (true)
   {
      if (SDL_PollEvent(&Event) == 0)
      {
      }else{
         if (Event.type == SDL_QUIT)
            break;
      }
   }
   
   return 0;
}
See if that works. If it's returning 1 then it's not loading the BMP succesfully. If it runs then you were doing something wrong in the convert format function, which I haven't looked into and have never used, but SDL_DisplayFormat converts it to the same format as the display, which it looks like you were trying to do yourself, so it would be fine like this.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

RyanPridgeon wrote:

Code: Select all

#include "SDL.h"

SDL_Surface *screen = NULL;

SDL_Event Event;
int main (int argc, char *argv[])
{
   SDL_Init(SDL_INIT_VIDEO);

   screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);

   SDL_Surface *img = NULL;
   img = SDL_LoadBMP("a.bmp");
   if (img == NULL) return 1;

   SDL_Surface *ConvertedImg = SDL_DisplayFormat(img);

   while (true)
   {
      if (SDL_PollEvent(&Event) == 0)
      {
      }else{
         if (Event.type == SDL_QUIT)
            break;
      }
   }
   
   return 0;
}
See if that works. If it's returning 1 then it's not loading the BMP succesfully. If it runs then you were doing something wrong in the convert format function, which I haven't looked into and have never used, but SDL_DisplayFormat converts it to the same format as the display, which it looks like you were trying to do yourself, so it would be fine like this.
That's weird, when i run it through the debugger, it runs and then exits, the value returned is 1 (even when the img is in the same directory.) but when running from the actual EXE, it works. does the debugger run the software from a different location and can't find the img?
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: SDL problem

Post by RyanPridgeon »

Avishaiozeri wrote:
RyanPridgeon wrote:

Code: Select all

#include "SDL.h"

SDL_Surface *screen = NULL;

SDL_Event Event;
int main (int argc, char *argv[])
{
   SDL_Init(SDL_INIT_VIDEO);

   screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);

   SDL_Surface *img = NULL;
   img = SDL_LoadBMP("a.bmp");
   if (img == NULL) return 1;

   SDL_Surface *ConvertedImg = SDL_DisplayFormat(img);

   while (true)
   {
      if (SDL_PollEvent(&Event) == 0)
      {
      }else{
         if (Event.type == SDL_QUIT)
            break;
      }
   }
   
   return 0;
}
See if that works. If it's returning 1 then it's not loading the BMP succesfully. If it runs then you were doing something wrong in the convert format function, which I haven't looked into and have never used, but SDL_DisplayFormat converts it to the same format as the display, which it looks like you were trying to do yourself, so it would be fine like this.
That's weird, when i run it through the debugger, it runs and then exits, the value returned is 1 (even when the img is in the same directory.) but when running from the actual EXE, it works. does the debugger run the software from a different location and can't find the img?
Yes, haha. I thought this might be the problem. When you run your project from inside your IDE, it's often a different working directory. The working directory is basically what you described. You can probably find it in your IDE options somewhere, or you can just stick your images in the IDE's working directory.

Note that no matter what the IDE does, when you run that exe OUTSIDE the IDE, the working directory will always be where the EXE is located.

Edit: For Codeblocks and Dev - C++, the default working directory for a project is the directory that your project file is located. For example, if your project is called Foo, then you have to put the images in the folder called Foo. I have limited experience for Visual Studio so I cannot recall where it is if you're using that, but I imagine it's similar if not the same.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

RyanPridgeon wrote:
Avishaiozeri wrote:
RyanPridgeon wrote:

Code: Select all

#include "SDL.h"

SDL_Surface *screen = NULL;

SDL_Event Event;
int main (int argc, char *argv[])
{
   SDL_Init(SDL_INIT_VIDEO);

   screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);

   SDL_Surface *img = NULL;
   img = SDL_LoadBMP("a.bmp");
   if (img == NULL) return 1;

   SDL_Surface *ConvertedImg = SDL_DisplayFormat(img);

   while (true)
   {
      if (SDL_PollEvent(&Event) == 0)
      {
      }else{
         if (Event.type == SDL_QUIT)
            break;
      }
   }
   
   return 0;
}
See if that works. If it's returning 1 then it's not loading the BMP succesfully. If it runs then you were doing something wrong in the convert format function, which I haven't looked into and have never used, but SDL_DisplayFormat converts it to the same format as the display, which it looks like you were trying to do yourself, so it would be fine like this.
That's weird, when i run it through the debugger, it runs and then exits, the value returned is 1 (even when the img is in the same directory.) but when running from the actual EXE, it works. does the debugger run the software from a different location and can't find the img?
Yes, haha. I thought this might be the problem. When you run your project from inside your IDE, it's often a different working directory. The working directory is basically what you described. You can probably find it in your IDE options somewhere, or you can just stick your images in the IDE's working directory.

Note that no matter what the IDE does, when you run that exe OUTSIDE the IDE, the working directory will always be where the EXE is located.
But how does it successful locate the SDL.dll when i put it where the EXE is (and the img too) (or maybe the transition of the location is after the initial setting of the program?)
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: SDL problem

Post by RyanPridgeon »

No idea, just remember to put the DLLs with the EXE.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

RyanPridgeon wrote:No idea, just remember to put the DLLs with the EXE.
Thanks man, so my problem probably was that because the different location the debugger looks for the image in, the LoadBMP returned null... so i tried to convert null...
Edit:
Confirmed it! i passed null to the SDL_ConvertSurface function, and got the error! from now on, i'll check for errors like these more frequently... Thank you all!
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: SDL problem

Post by X Abstract X »

Haha I thought you were checking it? =p
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

X Abstract X wrote:Haha I thought you were checking it? =p
lol, i am a beginner :oops: :lol:
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL problem

Post by xiphirx »

How about, when you get the error, what line is VS telling you to look at?
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

xiphirx wrote:How about, when you get the error, what line is VS telling you to look at?
Thanks man, but we already solved the problem... The problem was that i forgot to check for a case that the image wasn't successfully loaded by SDL_LoadBMP(), and because when i run it through the debugger it doesn't find the image, because it looks in the wrong place, then it returns NULL, and then the SDL_ConvertSurface function gets a null instead of a pointer to a surface as a parameter, so that caused the memory error...
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL problem

Post by xiphirx »

Avishaiozeri wrote:
xiphirx wrote:How about, when you get the error, what line is VS telling you to look at?
Thanks man, but we already solved the problem... The problem was that i forgot to check for a case that the image wasn't successfully loaded by SDL_LoadBMP(), and because when i run it through the debugger it doesn't find the image, because it looks in the wrong place, then it returns NULL, and then the SDL_ConvertSurface function gets a null instead of a pointer to a surface as a parameter, so that caused the memory error...
Yeah, im a dumbass, I didn't notice that there were 2 pages and posted based off of the first page X|
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
Avishaiozeri
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 85
Joined: Wed Mar 17, 2010 4:32 pm

Re: SDL problem

Post by Avishaiozeri »

xiphirx wrote:
Avishaiozeri wrote:
xiphirx wrote:How about, when you get the error, what line is VS telling you to look at?
Thanks man, but we already solved the problem... The problem was that i forgot to check for a case that the image wasn't successfully loaded by SDL_LoadBMP(), and because when i run it through the debugger it doesn't find the image, because it looks in the wrong place, then it returns NULL, and then the SDL_ConvertSurface function gets a null instead of a pointer to a surface as a parameter, so that caused the memory error...
Yeah, im a dumbass, I didn't notice that there were 2 pages and posted based off of the first page X|
A man that helps others out of kindness isn't a dumbass. :bow:
Live-Dimension
Chaos Rift Junior
Chaos Rift Junior
Posts: 345
Joined: Tue Jan 12, 2010 7:23 pm
Favorite Gaming Platforms: PC - Windows 7
Programming Language of Choice: c++;haxe
Contact:

Re: SDL problem

Post by Live-Dimension »

RyanPridgeon wrote:
Avishaiozeri wrote:
RyanPridgeon wrote:

Code: Select all

#include "SDL.h"

SDL_Surface *screen = NULL;

SDL_Event Event;
int main (int argc, char *argv[])
{
   SDL_Init(SDL_INIT_VIDEO);

   screen = SDL_SetVideoMode(640,480,0,SDL_ANYFORMAT);

   SDL_Surface *img = NULL;
   img = SDL_LoadBMP("a.bmp");
   if (img == NULL) return 1;

   SDL_Surface *ConvertedImg = SDL_DisplayFormat(img);

   while (true)
   {
      if (SDL_PollEvent(&Event) == 0)
      {
      }else{
         if (Event.type == SDL_QUIT)
            break;
      }
   }
   
   return 0;
}
See if that works. If it's returning 1 then it's not loading the BMP succesfully. If it runs then you were doing something wrong in the convert format function, which I haven't looked into and have never used, but SDL_DisplayFormat converts it to the same format as the display, which it looks like you were trying to do yourself, so it would be fine like this.
That's weird, when i run it through the debugger, it runs and then exits, the value returned is 1 (even when the img is in the same directory.) but when running from the actual EXE, it works. does the debugger run the software from a different location and can't find the img?
Yes, haha. I thought this might be the problem. When you run your project from inside your IDE, it's often a different working directory. The working directory is basically what you described. You can probably find it in your IDE options somewhere, or you can just stick your images in the IDE's working directory.

Note that no matter what the IDE does, when you run that exe OUTSIDE the IDE, the working directory will always be where the EXE is located.

Edit: For Codeblocks and Dev - C++, the default working directory for a project is the directory that your project file is located. For example, if your project is called Foo, then you have to put the images in the folder called Foo. I have limited experience for Visual Studio so I cannot recall where it is if you're using that, but I imagine it's similar if not the same.
In visual studio you can change the working directory.
Project properties -> config properties -> debugging -> working directory. set it to $(TargetDir)
Image
Post Reply