SFML community forums

Help => Window => Topic started by: conorjh on July 27, 2014, 04:07:16 pm

Title: RenderWindow::getSize() access violation
Post by: conorjh on July 27, 2014, 04:07:16 pm
I get an access violation when trying to get the size of a window. Oddly, I can do most of the other members I tried, like getPosition() or display(). Can provide more code if needed. Can anyone notice anything on first browse?

Code: [Select]
kRenderContext* kit::gfx::create(int _x, int _y, int _z)
{
//attempt to create a window with the given resolution
klog.print("Attempting to create window...",ll_debug);
kRenderContext* out = new kRenderContext();
out->window = new sf::RenderWindow();
out->window->create(sf::VideoMode(_x,_y,_z), kitAppTitle, sf::Style::Default);

if(!out->window->isOpen())
{
//failed
klog.print("Failed to create window",ll_debug);
return NULL;
}

//add to our list of contexts
out->x = out->window->getSize().x; <----------------------------error here
out->y = out->window->getSize().y;
out->z = _z;

out->id = util::randomNumU();
contextBank.insert( std::make_pair( out->id,out ) );

klog.print("Window created successfully",ll_debug);
return out;
}
Title: Re: RenderWindow::getSize() access violation
Post by: Jesper Juhl on July 27, 2014, 04:20:27 pm
A few guesses:
 - are you mixing up debug and release versions of the library and your app?
 - are you perhaps not using the exact same compiler (including version) to build your app that was used to build SFML?

A few random things I noticed:
 - you don't seem to use smart pointers (unique_ptr, make_unique and friends); why not?
 - prefer "nullptr" over NULL.
 - don't use names with leading underscore "_", most such names are reserved for the implementation.
 - you don't check the result of .insert()
Title: Re: RenderWindow::getSize() access violation
Post by: conorjh on July 27, 2014, 05:00:56 pm
All projects are part of the same solution, so I assume all projects share the same Debug/Release setting? As for the SFML libs, they were built in the same version just yesterday, fresh out the compiler. Does it matter that the pointer resides in a static lib?
Title: Re: RenderWindow::getSize() access violation
Post by: conorjh on July 27, 2014, 05:53:27 pm
Also tried doing the same, from within the method that creates the window and get the same.

Code: [Select]

bool kit::gfx::startup()
{
if(!(kit::gfx::mainRC = create(kitDefaultWindowW,kitDefaultWindowH,kitDefaultWindowD)))
{
//error making main window
return false;
}
kit::gfx::mainRC->window->getSize().x;    <--------------errors here
return true;
}
Title: Re: RenderWindow::getSize() access violation
Post by: conorjh on July 27, 2014, 08:29:43 pm
Ok, so I remade the most basic example I could, and dont get the error if I set the project to Multi Threaded Debug DLL, but do if its straight Multi Threaded Debug...

I thought Id got rid of this by getting the original project to compile but obviously not. Just for simplicity's sake, are there any VS 2010 static libs for 2.1 around? The ones I used from http://www.sfml-dev.org/download/sfml/2.1/ are what gave me the issues in the original post, so I went with Cmake and managed to get rid of them.

Im fairly sure that all my project settings are correct, and that I built SFML correctly via Cmake and VS2010, but these results suggest otherwise.

Code: [Select]
#include <SFML\Graphics.hpp>

int main()
{
sf::RenderWindow* test = new sf::RenderWindow();
test->create(sf::VideoMode(800,600,32), "Test", sf::Style::Default);
test->getSize().x;

return 0;
}

Edit:
Also, using the libs from the aforementioned link, this example only builds with Multi Threaded (Debug) DLL despite linking "-s-d" versions of libs and having SFML_STATIC as a preprocessor value. Is there anything Im missing? It cant be something wrong with SFML surely?
Title: Re: RenderWindow::getSize() access violation
Post by: Strelok on July 27, 2014, 11:06:35 pm
I know it doesn't relate to your problem but
#include <SFML\Graphics.hpp>

int main()
{
        sf::RenderWindow* test = new sf::RenderWindow();
        test->create(sf::VideoMode(800,600,32), "Test", sf::Style::Default);
        test->getSize().x;
        return 0; //where is test deleted?
}
#include <SFML\Graphics.hpp>

int main()
{
        sf::RenderWindow test;
        test.create(sf::VideoMode(800, 600, 32), "Test", sf::Style::Default);
        test.getSize().x;
        //test is destroyed, no leaks
        return 0;
}
If you really need to use pointers, you can either switch to a more recent version of VS and use smart pointers (http://msdn.microsoft.com/en-us/library/hh279674.aspx) or use the deprecated auto_ptr (http://www.gotw.ca/publications/using_auto_ptr_effectively.htm) like this:
#include <SFML\Graphics.hpp>

int main()
{
        std::auto_ptr<sf::RenderWindow> test(new sf::RenderWindow());
        test->create(sf::VideoMode(800, 600, 32), "Test", sf::Style::Default);
        test->getSize().x;
        //test is destroyed, no leaks
        return 0;
}
Title: Re: RenderWindow::getSize() access violation
Post by: conorjh on July 28, 2014, 10:26:24 am
That really was just an example, but Ill take the raw pointers advice for when I can actually get this project working properly
Title: Re: RenderWindow::getSize() access violation
Post by: Nexus on July 28, 2014, 12:13:00 pm
See also: Why RAII rocks (http://www.bromeon.ch/articles/raii.html) :)

And if you use [code=cpp] [/code] tags, you will get syntax highlighting ;)
Title: Re: RenderWindow::getSize() access violation
Post by: conorjh on July 29, 2014, 04:25:35 pm
Have rebuilt a project from scratch, using cMake, and can get the following example to run fine

Code: [Select]
sf::RenderWindow* w = new sf::RenderWindow();
w->create(sf::VideoMode(800,600,32), "Test", sf::Style::Default);
w->getSize().x;     //errors here

Stick that same code into my Project, and I get an access violation on line 3. The two projects are using the exact same libraries, Im assuming it can only be a project setting?

What are the possible settings that would cause this? Iv remade this project that many times now I cant see what Im doing wrong.
Title: Re: RenderWindow::getSize() access violation
Post by: Laurent on July 29, 2014, 04:35:28 pm
I didn't read everything so sorry if I'm off-topic, but this:

Quote
Ok, so I remade the most basic example I could, and dont get the error if I set the project to Multi Threaded Debug DLL, but do if its straight Multi Threaded Debug...

SFML needs to be compiled with the same CRT settings o compile SFML with "Multi Threaded Debug", you must set the CMake variable SFML_USE_STATIC_STD_LIBS to TRUE when you configure & build SFML.
Title: Re: RenderWindow::getSize() access violation
Post by: conorjh on July 29, 2014, 04:49:31 pm
Sorry, that problem was sorted, missed the option the first time around. The libraries Im using are building and running fine with the std libs linked statically, its now this error with RenderWindows. Iv made a skeleton project and the code runs fine, and even though theres more to the other project in which it doesnt run, all Iv done is include extra libs. Have you seen anything like this before?

Code: [Select]
//works
sf::RenderWindow w;
w.create(sf::VideoMode(800,600,32), "Test", sf::Style::Default);
w.getSize().x;

//doesnt work
sf::RenderWindow* w=new sf::RenderWindow();
w->create(sf::VideoMode(800,600,32), "Test", sf::Style::Default);
w->getSize().x;