I use vc++ 2010 express and currently working on a project but when I try to use SDL_CreateRenderer function I get this error:
First-chance exception at 0x6c8037be in Oyun Projem.exe: 0xC0000005: Access violation reading location 0x00000010.
Unhandled exception at 0x6c8037be in Oyun Projem.exe: 0xC0000005: Access violation reading location 0x00000010.
The program '[320] Oyun Projem.exe: Native' has exited with code -1073741819 (0xc0000005).
at this line:
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
here is the code
#include "stdafx.h"
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window;
SDL_Renderer *renderer;
window = NULL;
window = SDL_CreateWindow("My first RPG!", 100, 100, 100, 100, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
return 0;
}
Here is what I see when debugging:
SDL_CreateWindow returns NULL if there is a failure. Your code needs to check for this.
// Your code:
window = SDL_CreateWindow("My first RPG!", 100, 100, 100, 100, SDL_WINDOW_SHOWN);
// Add the following:
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
Related
With this minimal example I have a BadRequest error:
#include <SDL.h>
int main( int argc, char* args[] )
{
if(SDL_Init(SDL_INIT_VIDEO) < 0) abort();
SDL_Window *window = SDL_CreateWindow(
"An SDL2 window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);
if(window == NULL) abort();
SDL_Renderer* renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if(renderer == NULL) abort();
SDL_Delay(3000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
The error is the following
$ g++ $(sdl2-config --cflags) foo.cpp $(sdl2-config --libs)
$ ./a.out
X Error of failed request: BadRequest (invalid request code or no such operation)
Major opcode of failed request: 149 (GLX)
Minor opcode of failed request: 16 (X_GLXVendorPrivate)
Serial number of failed request: 197
Current serial number in output stream: 198
It seems the issue appears at the SDL_CreateRenderer function. Creating the window works which confirms my X server works too.
I'm just trying to create a simple SDL2 window with a simple renderer. When I debug it, it runs normally, the memory still normal. But when I minimize it, press the minimize button then suddenly the memory just keep going up and only when i reopened that window then the memory goes back normal.
Memory spike
The code:
#include <SDL.h>
#include <SDL_image.h>
int main(int argc, char* args[]) {
SDL_Window* gWindow;
SDL_Renderer* gRenderer;
SDL_Texture* gTexture;
SDL_Surface* temp;
//Init
SDL_Init(SDL_INIT_VIDEO);
gWindow = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1366, 768, SDL_WINDOW_SHOWN);
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
IMG_Init(IMG_INIT_PNG);
//loading medias
temp = IMG_Load("assets/sprites/700.png");
gTexture = SDL_CreateTextureFromSurface(gRenderer, temp);
SDL_FreeSurface(temp);
//The main loop
bool quit = false;
SDL_Event e;
while (!quit)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
quit = true;
}
}
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderPresent(gRenderer);
}
//Destroy things
SDL_DestroyTexture(gTexture);
gTexture = NULL;
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);
gWindow = NULL;
gRenderer = NULL;
IMG_Quit();
SDL_Quit();
return 0;
}
switch (m_event.window.event)
{
case SDL_WINDOWEVENT_MINIMIZED:
while (SDL_WaitEvent(&m_event))
{
if (m_event.window.event == SDL_WINDOWEVENT_RESTORED)
{
break;
}
}
break;
...
So when you minimize the Window, SDL keeps waiting for events. No further code could memory leak, since it stays in the lazy polling loop until the window is restored.
I traced the leak back to SDL_RenderClear, which shouldn't be supposed to run while minimized.
I just ran your code and compiled it. Seems fine. However you did mention when you minimize the program your ram meter increases, how long are you able to perform this repetition before it crashes? and does it even crash? It could be normal behavior. If your program crashes, its a memory leak which could indicate forgetting to de-allocate some other pointer to a memory object/variable.
I have a problem with SDL lib. I'm using VS2012 Ultimate and i was actually using this tutorial: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php to set everything and i did it step by step few times, but I still have problems this is my code, very simple:
#include <iostream>
#include <SDL.h>
SDL_Surface * ekran = NULL;
int main (int argc, char *args [] )
{
SDL_Init( SDL_INIT_EVERYTHING );
ekran = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
SDL_Flip( ekran );
SDL_Delay( 2000 );
SDL_Quit();
return 0;
}
and im having this errors:
error C3861: 'SDL_SetVideoMode': identifier not found
error C3861: 'SDL_Flip': identifier not found
Here below is an example how to replace SDL_SetVideoMode() in SDL2. The old way to init SDL is commented and left along with the new way for comparison purposes. Basically, SDL2 creates a window with a title, then a surface attached to it, while SDL1 creates a surface alone and then calls the window manager to give it a name.
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL video init failed: %s\n", SDL_GetError());
return 1;
}
// SDL_Surface *screenSurface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
window = SDL_CreateWindow("Sphere Rendering",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Window could not be created: %s\n", SDL_GetError());
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
if (!screenSurface) {
fprintf(stderr, "Screen surface could not be created: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// SDL_WM_SetCaption("Sphere Rendering", NULL);
Take a look at that tutorial page again. Your code does not match it (e.g. SDL_SetVideoMode() no longer exists). Your code uses SDL 1.2 and the (updated) tutorial uses SDL 2.0. Are you using an old cached version of that page?
I have the following piece of code where among with lot of other stuff (which i didn't include in this topic), i'm trying to start up sdl, create a render and load some sprites.
Everything compiles just fine but when i run my application a break is caused saying: Unhandled exception at 0x681252D5 (SDL.dll) in Carribean World SDL.exe: 0xC0000005: Access violation reading location 0x16161804
The break occurs and the point where i use the SDL_ConvertSurface() function
Can anyone help me out, i can't see what's wrong
Declerations:
SDL_Texture* background = NULL;
SDL_Surface* tmp = NULL;
SDL_Surface* surface = NULL;
SDL_Window *window = SDL_CreateWindow("Carribean World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
1360, 768,
SDL_WINDOW_RESIZABLE);
SDL_Surface* screen = SDL_GetWindowSurface(window);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);
SDL_PixelFormat* fmt = screen->format;
IN MAIN:
Initialize all SDL subsystems
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 0;
}
Load images to surfaces
if ((tmp = IMG_Load("images/water.jpg")) == NULL)
{
cout << "SDL_SetVideoMode() Failed: " << SDL_GetError() << endl;
return 0;
}
Right here a break is caused
if ((surface = SDL_ConvertSurface(tmp, fmt, 0)) == NULL)
{
cout << "SDL_ConvertSurface() Failed: " << SDL_GetError() << endl;
}
background = SDL_CreateTextureFromSurface(renderer, tmp);
You haven't checked return value of SDL_GetWindowSurface. But anyway, SDL documentation for this function says 'You may not combine this with 3D or the rendering API on this window.'. So either you exclusively use SDL_Renderer API, or using SDL_BlitSurface and alike and after that calling SDL_UpdateWindowSurface, but you can't use both.
The problem occurs when I try to play with one of the Rastertek DirectX 11 tutorials.
I changed the const bool fullscreen value to false so the program runs under window mode
I changed the window style from CLIPPINGWINDOW to OVERLAPPEDWINDOW
It works fine except that the program throws an exception when the window is destroyed:
Unhandled exception at 0x779715ee in FrustumCulling.exe: 0xC0000005: Access violation reading location 0xfeeeff5e.
it's tutorial 16 at http://www.rastertek.com/dx11tut16.html
The only modifications I made are:
In graphicsclass.h
const bool FULL_SCREEN = true //false;
and
SystemClass::InitializeWindows
{
...
m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_applicationName, m_applicationName,
WS_OVERLAPPEDWINDOW,
posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL);
...
}
Is it possible you're still setting up your swapchain in fullscreen mode?
When I was running into this issue, adding this to my DxClass' destructor solved it
if(_swapChain != nullptr)
{
_swapChain->SetFullscreenState(false, NULL);
_swapChain->Release();
_swapChain = nullptr;
}
As per one of the Rastertek comments in one of the tutorials, the swapchain has to have the fullscreen set to false prior to being released.
When you set up your DXGI_SWAP_CHAIN_DESC assure you are using swapChainDesc.Windowed = !FULL_SCREEN and not a literal bool value, and make sure you set FULL_SCREEN to false, FULL_SCREEN = true //false; wouldn't do that and shouldn't compile.