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

Author Topic: Access Violation  (Read 4384 times)

0 Members and 1 Guest are viewing this topic.

NameUser

  • Newbie
  • *
  • Posts: 30
    • View Profile
Access Violation
« on: July 09, 2012, 12:51:29 am »
I've been working on getting better with SFML. After a few simple projects which went well, I wanted to learn a vital skill. Dividing my code into seperate header files so that I don't just have a giant wall of code. I decided to try and remake Zelda II's Sword system, which I always felt was very fun to fight with.

However, I was soon stumped by this.

Unhandled exception at 0x76f415de in Zelda II Swordfight.exe: 0xC0000005: Access violation reading location 0xc5fed298.

I managed to pinpoint it down to this line.

In Graphics Handling/func_drawscreen.h/window->draw(*sprite_title_p);

If I move the initialization of the texture and conversion of it to a sprite into the same file, all works fine.

http://www.sendspace.com/file/fyiff7

There's my project. I'm using Microsoft Visual C++ 2012. I'm hoping this is just some dumb mistake on my part. If anyone could help me it would be greatly appreciated.
« Last Edit: July 09, 2012, 01:27:57 am by NameUser »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Access Violation
« Reply #1 on: July 09, 2012, 01:59:32 am »
I've no idea where you got that strange idea you're using for splitting things up into diffrent header files, it's not the way anyone (should) programs in C++.

I can only advice to get a C++ book and read it from front to back.
For example you're using new and never delete, this is not Java requires everything to be initialized with new.
Also using global variables and functions isn't such a good idea, unless it's really needed and you know what you're doing, because it can have some strange side effects.

I managed to pinpoint it down to this line.
This sounds like you had quite a bit of work to do when doing this. Using the build in debuger of Visual Studio it can be done within a few seconds...

Now for your possible problem, look at func_graphicsinit.h. You're creating a local sprite and then try to assign it's adress to the global sprite. This may work, but as soon as you leave the function the local sprite will get destroyed and the adress the global sprite points at is invalid, trying to access it ends up in access violation.

To me it seems you've no idea what you're doing with those pointers and objects and 'header' files, thus I really can only push you to read a book about C++!
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Access Violation
« Reply #2 on: July 09, 2012, 02:04:57 am »
Yup, you need to learn C++. Buy a book or some books because at the moment you're doing a lot of things plain wrong. Not subjectively wrong, but empirically, measurably and undeniably wrong. Such as here:

void graphicsinit() {
sf::Texture tex_title;                                                                   
tex_title.loadFromFile("C:/title.png");                                  
sf::Sprite sprite_title(tex_title);            
sprite_title_p = &sprite_title;
};
 

You point it towards sprite_title, but sprite_title goes out of scope at the end of that function so you're just pointing to essentially a random block in the memory and things will go wrong when you try and access that section of memory through the pointer later (this may be the cause of the Access Violation).
« Last Edit: July 09, 2012, 02:06:41 am by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

NameUser

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Access Violation
« Reply #3 on: July 09, 2012, 02:06:33 am »
I have read a book. It did not give much coverage of splitting the code between multiple files. I had originally been using CPPs but someone told me I wanted to be using Headers instead. If you'd like to enlighten me on the right way to do it, I'd be happy to hear. I was aware of the delete issue, just hadn't fixed it yet.

Also thanks. That should have been completely obvious, but I have a tendency to overlook such things.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Access Violation
« Reply #4 on: July 09, 2012, 11:08:59 am »
Unfortunately, there are many bad C++ books. I'd also suggest a new one like the C++ Primer and maybe, to teach good programming techniques, Effective C++.

It won't help if we just fix single issues, you really have to understand C++ as a whole. Even if this requires some time now, it will avoid much trouble (and thus wasted time) in the future. You are also more motivated if you can implement things fast and if your games are robust.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

NameUser

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Access Violation
« Reply #5 on: July 09, 2012, 11:27:00 pm »
The book I read seemed quite good, and it covered all of the vitals. I think the bigger issue is that you can't really get many books on programming applied to game development, which has some vital differences. Are you honestly trying to tell me you all started being perfect programmers doing everything right and never needing help after reading a book?

I'm of the opinion that trying, failing, and recieving help is a far more efficent way to learn than reading another book.

Regardless, I didn't make this topic to debate my knowledge or learning styles. I'd appreciate it if I could just receive help with my issue. ;)
« Last Edit: July 09, 2012, 11:58:03 pm by NameUser »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: Access Violation
« Reply #6 on: July 10, 2012, 12:04:37 am »
If I'm not mistaken your question was already answered in the first reply.

Simply out of curiosity, what's the name of the book you're/you've been using?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Access Violation
« Reply #7 on: July 10, 2012, 12:12:22 am »
If you're so assured that you're basic knowledge is sufficient, than take a look at "Effective C++" and be ready to be blown away. This book convers so much knowledge in a few pages that you won't find in 90% (or even higher) of the C++ books. But it requires quite a good understanding of classes and OOP which you're obviously lacking.

Trial & error is always a possible path, but it's not very efficient and next to that part, you're not only using up your time, but you're also stealing time from people that are willing help. So it seems to me quite unfair to steal their time just because you didn't felt like studying and reading more on your own. ;)
« Last Edit: July 10, 2012, 12:13:54 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NameUser

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Access Violation
« Reply #8 on: July 10, 2012, 12:32:21 am »
You're right Perde. My bad. I had been going to reuse this topic for another issue I was having but I decided to try again and work it out myself.

eXpl0it3r: As I said in the IRC, I understand how to create and control classes, but not efficent use. Unfamiliar with OOP though.

As for taking the time of the helper, you happen to be particularly helpful (which I appreciate), but you're not obligated to help me. Any person with experience can assist me. I also try my hardest to work things out myself before asking. I solve many problems myself. I only ask when I'm truly stumped.

As for it being 'less efficient', bear in mind that everyone has a different learning style. I have difficulty with rote memorization without real application.


MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Access Violation
« Reply #9 on: July 10, 2012, 09:17:15 pm »
Also I must recommend, after that, Exceptional C++ by Herb Sutter and Modern C++ Design by Andrei Alexandrescu.

Exceptional C++ shows a lot of "gotchas" in the language and programming in general, places where subtle problems can appear. Modern C++ is all about template metaprogramming a fascinating read.
« Last Edit: July 10, 2012, 09:21:38 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Access Violation
« Reply #10 on: July 10, 2012, 09:47:45 pm »
I think the bigger issue is that you can't really get many books on programming applied to game development, which has some vital differences.
I have made exactly the other experience: There are many C++ books that also cover parts beyond the standard library, be it GUI, Networks, or game programming. Unfortunately, these are almost always the books that are written using "C with classes", and as a result of their focus on the "beyond" part, C++ and its standard library often go short.

Books that teach modern and idiomatic C++ style are quite rare to find, even more about C++11. With that, I mean that topics such as the STL, RAII, smart pointers, std::string should primarily be recommended (while arrays, new/delete, C strings are mentioned, but more as a side note).

In fact, game programming doesn't fundamentally differ from writing other C++ applications - although the application logic is different, the abstraction mechanisms and the used language/library features remain the same. Moreover, it's those parts who render an application robust and maintainable. If one doesn't know how to apply them efficiently, he will always be in trouble. And C++ is a language where trial and error is an extremely bad approach, already because of the language complexity. Even if you find a possibility to achieve something, how do you know this cannot be done in a better way? C++ offers loads of situations where something appears to be "working", but actually ends up to be inherently error-prone (such as the "side note" topics mentioned above).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: