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

Author Topic: Exception Thrown when trying to run program  (Read 2767 times)

0 Members and 1 Guest are viewing this topic.

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Exception Thrown when trying to run program
« on: April 11, 2016, 04:15:31 pm »
Code: [Select]
#include "SFML\Graphics.hpp"
#include <random>
#include <ctime>

int main() {
   srand(time(NULL));
   sf::RenderWindow TestingWindow(sf::VideoMode(100, 100), "Testing Window");
   sf::Image img;
   sf::Texture txt;
   sf::Sprite spr;
   int x = 1, y = 1, z;
   while (y < 100){
      while (x < 100) {
         z = rand() % 2 + 1;
         if (z == 1) {
            img.setPixel(x, y, sf::Color::Black);
         }
         else {                                 
            img.setPixel(x, y, sf::Color::Black);
         }
         x++;
      }
      x = 1; y++;
   }
   txt.loadFromImage(img);
   spr.setTexture(txt);
   while (TestingWindow.isOpen())
   {
      sf::Event event;
      while (TestingWindow.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            TestingWindow.close();
      }

      TestingWindow.clear();
      TestingWindow.draw(spr);
      TestingWindow.display();
   }
}


So.... I'm trying to run this program, and it should be creating a sort of fuzz you see on a faulty television, but everytime I run the program, this exception is thrown.

Unhandled exception at 0x6B8C5C98 (sfml-graphics-2.dll) in SFML 2.0 APPLICATION.exe: 0xC0000005: Access violation writing location 0x00000004.

Is there something I can do to fix this?


(And yes, I know there is a lack of comments. I'm trying to put more in my code, but I keep forgetting because I'm the only one who reads it)
« Last Edit: April 11, 2016, 04:18:54 pm by TheGoldenFyre »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Exception Thrown when trying to run program
« Reply #1 on: April 11, 2016, 04:18:56 pm »
You didn't set the size of image.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10834
    • View Profile
    • development blog
    • Email
AW: Exception Thrown when trying to run program
« Reply #2 on: April 11, 2016, 04:19:20 pm »
You don't create the image, yet you use setPixel on it. That's undefined behavior as documented in the docs. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Exception Thrown when trying to run program
« Reply #3 on: April 11, 2016, 04:21:27 pm »
Thanks!


TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Exception Thrown when trying to run program
« Reply #5 on: April 11, 2016, 04:30:21 pm »
One more thing... It is running now, but I only get to see a black screen. Any ideas?

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Exception Thrown when trying to run program
« Reply #6 on: April 11, 2016, 04:31:26 pm »
Your code gives an black image as result, see:
if (z == 1) {
            //for black pixel
            img.setPixel(x, y, sf::Color::Black);
         }
         else {  
            //again black pixel                              
            img.setPixel(x, y, sf::Color::Black);
         }
//conclusion: in any case you get black pixel
 
« Last Edit: April 11, 2016, 04:35:42 pm by Mr_Blame »

nicox11

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Re: Exception Thrown when trying to run program
« Reply #7 on: April 11, 2016, 04:31:45 pm »
 if (z == 1) {
    img.setPixel(x, y, sf::Color::Black);
}
else {                                
    img.setPixel(x, y, sf::Color::Black);
}

what's the difference here ? You either way define black as the pixel color.

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Exception Thrown when trying to run program
« Reply #8 on: April 11, 2016, 04:33:45 pm »
I'm stupid as sh*t... I meant the second one to be white. Thanks for the help guys!


Edit:

Everything's working now. Thanks again!
« Last Edit: April 11, 2016, 04:35:30 pm by TheGoldenFyre »