Page 1 of 3

C++ WTF

Posted: Tue Jan 01, 2013 2:48 pm
by WedNESday
On creation of a mapper 3 game...

Code: Select all

case 3:
	VideoRAM = 0
	break;
While fetching either VRAM or VRAM data...

Code: Select all

if (VideoRAM)
	Data = VRAM[....];
else
	Data = VROM[....];
After the working Arkanoid title screen (which means VROM has been used so far) just as you go to play my emulator crashes with an error. Upon debugging it says that it tried to access VRAM. Nowhere in the program is VideoRAM being set to 1 by mistake and if I try to manually trap the illegal access with...

Code: Select all

if (VideoRAM)
{
	MessageBox(NULL, "WARNING", "", 0);
	Data = VRAM[....];
}
else
	Data = VROM[....];
...nothing happens because VideoRAM never equals 1 but it still goes on to crash straight away on the next line.

I have deleted my PCH once already and that helped but it came back after a few tries. Whats more it only seems to happen about 80% of the time. Just WTF is going on?

Re: C++ WTF

Posted: Tue Jan 01, 2013 3:05 pm
by thefox
"Happens 80% of the time" usually means some kind of concurrency problem. It's impossible to say based on the information that you gave. Because you talked about PCH files, I assume you use Visual Studio, so why don't you debug from the point where the crash happened? (Look at the disassembly if you have to.)

Re: C++ WTF

Posted: Tue Jan 01, 2013 4:05 pm
by WedNESday
I can't debug any further from the point where it crashes as the Ignore button is greyed out.

It has nothing to do with the PCH as regardless to whether I delete that or not the problem still persists.

Re: C++ WTF

Posted: Tue Jan 01, 2013 5:54 pm
by natt
WedNESday wrote: I have deleted my PCH once already and that helped but it came back after a few tries.
I highly doubt it has anything to do with PCH files, but if you think it does, just turn them off.

Re: C++ WTF

Posted: Tue Jan 01, 2013 5:55 pm
by Dwedit
Sounds like an access violation? I've seen some programs put in SEH handlers (structured exception handlers) to allow execution to continue despite an access violation.

But anyway, are you sure it's not just uninitialized or corrupted memory?

Re: C++ WTF

Posted: Tue Jan 01, 2013 11:07 pm
by Zelex
thefox wrote:"Happens 80% of the time" usually means some kind of concurrency problem. It's impossible to say based on the information that you gave. Because you talked about PCH files, I assume you use Visual Studio, so why don't you debug from the point where the crash happened? (Look at the disassembly if you have to.)
That or also could be memory corruption.

Re: C++ WTF

Posted: Tue Jan 01, 2013 11:16 pm
by rainwarrior
Why not put a data breakpoint on the variable in question?

Re: C++ WTF

Posted: Wed Jan 02, 2013 7:14 am
by cpow
WedNESday wrote:

Code: Select all

case 3:
	VideoRAM = 0
	break;
While fetching either VRAM or VRAM data...

Code: Select all

if (VideoRAM)
	Data = VRAM[....];
else
	Data = VROM[....];
There's no C++ here. :?: It's things like this that give C++ a bad name. :roll:
WedNESday wrote: I have deleted my PCH once already and that helped but it came back after a few tries. Whats more it only seems to happen about 80% of the time. Just WTF is going on?
Check your CHR bank switching code. Make sure you're never selecting VROM banks other than 0 or 1. I'm assuming you mean Arkanoid (U).nes. When I run that it is CNROM and has 2 CHR banks. Make sure that no matter what is written to $8000 - $FFFF you only use the number of bits necessary to select between the actual number of banks present in the image.

I looked at my CNROM implementation and I don't mask any bits so that implies that the correct values are written to $8000 - $FFFF at least by Arkanoid (U).nes [which works for me].

Re: C++ WTF

Posted: Wed Jan 02, 2013 10:33 am
by WedNESday
1. What's wrong with my C++? :o

2. The problem seems to have fixed itself now since I have mended the PPU renderer. Although I must admit that this is not the first time that I have had code self modify.

Re: C++ WTF

Posted: Wed Jan 02, 2013 11:15 am
by Jsolo
Code does not modify itself unless explicitly told to. You are probably a victim of the common buffer overflow (bufferus overflowis).
C++ provides many abstractions to prevent buffer overflows, for instance std::vector<> or std::array<>. :)

Re: C++ WTF

Posted: Thu Jan 03, 2013 11:48 pm
by mic_
For issues like these you might want to make use of utilities like Valgrind or Electric-Fence in order to find potential error sources faster.
Upon debugging it says that it tried to access VRAM.
So it actually halted execution and entered the VS debugger? In that case you should be able to see what value VideoRAM has when execution halts. Does it look valid (you seem to be writing only 0 and 1 to it, so then it should never contain anything else)? If it doesn't you might be having problems with buffer overflows as Jsolo suggested.
Another potential error source would be concurrency, though I have no idea how you've structured your emulator, and it's impossible to say from the snippets of code pasted here. If you've got dependencies to a global variable in more than one thread and at least one of those threads is modifying the variable you'll typically want to make those pieces of code mutually exclusive from one another.
Nowhere in the program is VideoRAM being set to 1 by mistake
At which points do you set it to 1 on purpose though? Might that have happened sometime earlier during execution, and now you're looking at an old value of VideoRAM while VRAM has been freed?
are you sure it's not just uninitialized ... memory?
Only local variables (i.e. those allocated on the stack) are truly uninitialized in C++. A global or local static variable should contain a zero value if not explictly initialized.

Re: C++ WTF

Posted: Fri Jan 04, 2013 8:29 am
by tepples
I was under the impression that the use of global and static variables was "considered harmful" nowadays. Global variables make it harder to turn an object into one that supports multiple instances (such as the parallel emulators that power nemulator's Wii Menu-style game chooser). Global variables also interfere with multithreading, especially now that computers have multiple cores, and cores in low-cost or low-power CPUs such as Atom and PPE have simultaneous multithreading to hide the negative effects of in-order execution.

Re: C++ WTF

Posted: Fri Jan 04, 2013 10:26 am
by koitsu
tepples wrote:I was under the impression that the use of global and static variables was "considered harmful" nowadays. ... Global variables also interfere with multithreading, especially now that computers have multiple cores, and cores in low-cost or low-power CPUs such as Atom and PPE have simultaneous multithreading to hide the negative effects of in-order execution.
Utter nonsense.

Re: C++ WTF

Posted: Fri Jan 04, 2013 10:44 am
by Jsolo
tepples wrote: I was under the impression that the use of global and static variables was "considered harmful" nowadays.
Global variables have their place, as do macros and all other language features. There are situations where using global variables is perfectly fine.
tepples wrote:Global variables also interfere with multithreading.
C++11 fixed this by introducing thread_local.

Re: C++ WTF

Posted: Sat Jan 05, 2013 2:41 pm
by WedNESday
Just a quick update for you guys. As I have fixed elements of broken mapping/mirroring/timing etc. the whole thing has gotten a lot better. I think that maybe before the PPU was getting into a loop that it couldn't get out of or something.