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

Author Topic: Heap Corruption?  (Read 2692 times)

0 Members and 1 Guest are viewing this topic.

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« on: June 23, 2011, 05:39:47 pm »
I always had difficulties with pointers. I'm trying to duplicate the code here to draw pixels on the screen. I think the issue is with my pointer to Display.

Here's my code:
Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

#define _WIDTH_ 32
#define _HEIGHT_ 64
#define _ZOOM_ 20

int main()
{
    sf::RenderWindow App(sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_), "Test");
sf::View View(sf::FloatRect(0, 0, _WIDTH_, _HEIGHT_));
View.Zoom(_ZOOM_);
sf::Image Image(_WIDTH_, _HEIGHT_, sf::Color(0, 0, 0));
sf::Sprite Sprite;
sf::Uint8 *Display = new sf::Uint8[_WIDTH_ + _HEIGHT_ * 4];

while(App.IsOpened())
{
sf::Event e;
while(App.GetEvent(e))
{
if(e.Type == sf::Event::Closed)
App.Close();

for(int i = 0; i < _WIDTH_; i++)
for(int j = 0; j < _HEIGHT_; j++)
{
Display[(j+i*_WIDTH_)*4] = i*j % 255; // R
Display[(j+i*_WIDTH_+1)*4] = i*j % 255; // G
Display[(j+i*_WIDTH_+2)*4] = i*j % 255; // B
Display[(j+i*_WIDTH_+3)*4] = 255; // A
}

App.Clear();
Image.LoadFromPixels(_WIDTH_, _HEIGHT_, Display);
Sprite.SetImage(Image);
App.Draw(Sprite);
App.SetView(View);
App.Display();
}
}

delete [] Display;
    return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Heap Corruption?
« Reply #1 on: June 23, 2011, 06:08:56 pm »
Quote
Code: [Select]
new sf::Uint8[_WIDTH_ + _HEIGHT_ * 4]

Should be _WIDTH_ * _HEIGHT_ * 4 ;)
Laurent Gomila - SFML developer

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« Reply #2 on: June 23, 2011, 06:27:41 pm »
FML. It's working now, but the window isn't showing. When I put my cursor over the icon on the taskbar, I can preview it, but it's not popping up/displaying.

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« Reply #3 on: June 23, 2011, 06:29:06 pm »
Now I actually put my rendering code in the right place, but it's still not displaying.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Heap Corruption?
« Reply #4 on: June 23, 2011, 06:38:27 pm »
ATI graphics card?
Laurent Gomila - SFML developer

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« Reply #5 on: June 23, 2011, 06:40:18 pm »
No, I have 2 GTX260Ms in SLi.

And here's my current code:
Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>

#define _WIDTH_ 32
#define _HEIGHT_ 64
#define _ZOOM_ 20

int main()
{
    sf::RenderWindow App(sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_), "Test");
sf::View View(sf::FloatRect(0, 0, _WIDTH_, _HEIGHT_));
View.Zoom(_ZOOM_);
sf::Image Image(_WIDTH_, _HEIGHT_, sf::Color(0, 0, 0));
sf::Sprite Sprite;
sf::Uint8 *Display = new sf::Uint8[_WIDTH_ * _HEIGHT_ * 4];

while(App.IsOpened())
{
sf::Event e;
while(App.GetEvent(e))
{
if(e.Type == sf::Event::Closed)
App.Close();
}

for(int i = 0; i < _WIDTH_; i++)
for(int j = 0; j < _HEIGHT_; j++)
{
Display[(j+i*_WIDTH_)*4] = i*j % 255; // R
Display[(j+i*_WIDTH_)*4+1] = i*j % 255; // G
Display[(j+i*_WIDTH_)*4+2] = i*j % 255; // B
Display[(j+i*_WIDTH_)*4+3] = 255; // A
}

App.Clear();
Image.LoadFromPixels(_WIDTH_, _HEIGHT_, Display);
Sprite.SetImage(Image);
App.Draw(Sprite);
App.SetView(View);
App.Display();
}

delete [] Display;
    return 0;
}

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« Reply #6 on: June 23, 2011, 07:20:07 pm »
Wow, I had to replace
sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_) with sf::VideoMode(_WIDTH_ * _ZOOM_, _HEIGHT_ * _ZOOM_)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Heap Corruption?
« Reply #7 on: June 23, 2011, 07:41:40 pm »
By the way:

Code: [Select]
#define _WIDTH_ 32Identifiers beginning with underlines are reserved for the compiler and the standard library implementation, don't use them.

Anyway, a constant is much better than a macro:
Code: [Select]
const unsigned int WIDTH = 32;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« Reply #8 on: June 23, 2011, 09:18:10 pm »
Can you try this? When I try replacing the defines with const ints, the window refuses to display again.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Heap Corruption?
« Reply #9 on: June 23, 2011, 09:37:22 pm »
Ah, I haven't seen before that you copy the whole array every frame. Do that once before the main loop. As soon as the main loop begins, don't access the image anymore.

And seriously, if modifications like inserting spaces into an expression or replacing a #define with const int have visible effects, then your compiler must be really broken.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NullEntity

  • Newbie
  • *
  • Posts: 9
    • View Profile
Heap Corruption?
« Reply #10 on: June 24, 2011, 12:08:25 am »
So of course I rewrite it and it starts working fine.