C++ WTF

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

C++ WTF

Post by WedNESday » Tue Jan 01, 2013 2:48 pm

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?

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: C++ WTF

Post by thefox » Tue Jan 01, 2013 3:05 pm

"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.)
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Re: C++ WTF

Post by WedNESday » Tue Jan 01, 2013 4:05 pm

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.

natt
Posts: 76
Joined: Fri Oct 26, 2012 5:27 pm

Re: C++ WTF

Post by natt » Tue Jan 01, 2013 5:54 pm

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.

User avatar
Dwedit
Posts: 4236
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: C++ WTF

Post by Dwedit » Tue Jan 01, 2013 5:55 pm

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?
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

Zelex
Posts: 267
Joined: Fri Apr 29, 2011 9:44 pm

Re: C++ WTF

Post by Zelex » Tue Jan 01, 2013 11:07 pm

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.

User avatar
rainwarrior
Posts: 7675
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: C++ WTF

Post by rainwarrior » Tue Jan 01, 2013 11:16 pm

Why not put a data breakpoint on the variable in question?

User avatar
cpow
NESICIDE developer
Posts: 1089
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Re: C++ WTF

Post by cpow » Wed Jan 02, 2013 7:14 am

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].

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Re: C++ WTF

Post by WedNESday » Wed Jan 02, 2013 10:33 am

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.

Jsolo
Posts: 27
Joined: Mon Jun 27, 2011 4:14 am
Location: Lurker Cave

Re: C++ WTF

Post by Jsolo » Wed Jan 02, 2013 11:15 am

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<>. :)

mic_
Posts: 917
Joined: Thu Oct 05, 2006 6:29 am

Re: C++ WTF

Post by mic_ » Thu Jan 03, 2013 11:48 pm

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.

tepples
Posts: 21751
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: C++ WTF

Post by tepples » Fri Jan 04, 2013 8:29 am

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.

User avatar
koitsu
Posts: 4216
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: C++ WTF

Post by koitsu » Fri Jan 04, 2013 10:26 am

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.

Jsolo
Posts: 27
Joined: Mon Jun 27, 2011 4:14 am
Location: Lurker Cave

Re: C++ WTF

Post by Jsolo » Fri Jan 04, 2013 10:44 am

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.

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Re: C++ WTF

Post by WedNESday » Sat Jan 05, 2013 2:41 pm

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.

Post Reply