Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RenderWindow::getSize() access violation  (Read 5469 times)

0 Members and 1 Guest are viewing this topic.

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
RenderWindow::getSize() access violation
« 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;
}

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: RenderWindow::getSize() access violation
« Reply #1 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()
« Last Edit: July 27, 2014, 04:22:04 pm by Jesper Juhl »

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: RenderWindow::getSize() access violation
« Reply #2 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?

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: RenderWindow::getSize() access violation
« Reply #3 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;
}

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: RenderWindow::getSize() access violation
« Reply #4 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?
« Last Edit: July 27, 2014, 09:15:51 pm by conorjh »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: RenderWindow::getSize() access violation
« Reply #5 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 or use the deprecated auto_ptr 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;
}
« Last Edit: July 27, 2014, 11:22:35 pm by Strelok »

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: RenderWindow::getSize() access violation
« Reply #6 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: RenderWindow::getSize() access violation
« Reply #7 on: July 28, 2014, 12:13:00 pm »
See also: Why RAII rocks :)

And if you use [code=cpp] [/code] tags, you will get syntax highlighting ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: RenderWindow::getSize() access violation
« Reply #8 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: RenderWindow::getSize() access violation
« Reply #9 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.
Laurent Gomila - SFML developer

conorjh

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: RenderWindow::getSize() access violation
« Reply #10 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;

« Last Edit: July 29, 2014, 04:58:51 pm by conorjh »