Got lots of ideas, you've already been offered several by more than one person.
Should I ignore the error message and use a TRY CATCH function? |
I refer you back to the first reply:
Peter87 wrote: |
---|
Use a debugger to see which line of code where the access violation is happening. |
Never ignore error messages, either compile or run-time. Your code is doing something rather dangerous, even if only intermittently. System access violations (reading/writing memory your program doesn't own) is always a bad sign.
You should write code that is as warning free as possible as well. More often than not warnings can develop into errors that become hard to get rid of.
Even with code that seemingly works without errors using a debugger to step through your code is never a bad idea. Get used to debugging code, issues can show up before they become major errors.
FYI, nothing has really changed with your problem, you just keep moving things around to fix a problem that should be profiled and properly debugged.
Your memory problems might be in an entirely different section of your code, waiting to strike like a poisonous snake at a later time.
The program seems to write the png files properly... despite the error |
"Seems to work" is the operative phrase. The files may be subtly corrupted, one or a few bits are not correct, and you'll never know it until *BOOM!* Your mod screws up the game files and the game ceases to work, or some other nasty and unwanted action.
I am trying to make a mod for a game.... |
A goal that I can understand and applaud. :)
...so I am writing code from scratch. |
Been there, done that, and usually have fallen flat on my (digital) face more times than I care to mention or like.
My suggestion of looking at a 3rd party PNG library was for guidance. The authors of the libraries more than likely went through the same trial and error process and figured out how to do the work of PNG grunt work without having errors.
You don't have to
use the library, study the code for pointers. The library code is likely doing things in a way you never even considered.
I don't know what your OS or your compiler is. The MS Visual Studio IDE has a good debugger built into the IDE. Running code in debug mode is always useful, especially with code that has errors. Setting checkpoints to halt execution and then step through you code execution allows you to inspect variables so you can determine if they are correct at the time.
Learning to use a debugger, and use it well, is never really emphasized in programming classes AFAIK or in books or online resources.
Would it help if I link to the code. Its quite long and inefficient. |
Two points you should look at:
http://www.gregcons.com/KateBlog/HowToAskForCCodingHelp.aspx
http://www.sscce.org/ (Short, Self Contained, Correct (Compilable), Example)
Both of those links should be required reading for people looking for help.
What you consider long might be code I consider rather short and lean. Same with inefficient.
Expecting people to look at code that is more than a few dozen lines, and debug it for your for free, is asking a lot.
And the level of competence you get from others might be less than helpful. I am at the core a self-taught programming hobbyist with large gaps in understanding C++. There are times even with code I write I can't understand what it's doing after time has passed. I'm not even an "expert beginner".
https://daedtech.com/how-developers-stop-learning-rise-of-the-expert-beginner/
With all that blathering above I've said all I should. I can only make a further mess of it if I don't stop.