Why does CString break when inside this loop?

Status
Not open for further replies.

Catfish

Ars Tribunus Angusticlavius
9,954
Heya...
Here's a snippet of code:
code:<HR>
Code:
	<P>ifstream in;<P>int renderFrame()
{
	static bool firstTime=TRUE;
	if (firstTime)
	{
		in.open(filename, ios::in | ios::binary);
		firstTime = FALSE;
	};<P>	// Check that the file loaded OK
	if (!in)
		return 0;<P>	CString imagename = "c://black betty";
	imagename.AppendFormat(".%d.raw", frameCount);<P>	if (!in.eof())
	{
		in.read((char*)&flockWeights, sizeof(flockWeights)*2);
		in.read((char*)&boidModifier, sizeof(boidModifier)*2);
		in.read((char*)&normalisedVolume, sizeof(normalisedVolume)*2);
		in.read((char*)&filteredSpectrum, sizeof(filteredSpectrum)*630);
		
		app.takeScreenShot(imagename);
	}
}

Any assignments to a CString inside that if (!in.eof()) loop, including inside function calls like app.takeScreenShot(imagename);, result in a long stream of
First-chance exception at 0x00000000 in Flocking.exe: 0xC0000005: Access violation reading location 0x00000000.

errors.

Further testing has shown it's not just limited to CString - it will also crash on this trivial assignment:
code:<HR>
Code:
		FILE *out;
		out = fopen(FileName, "a");


Uhhh...any suggestions?
Catfish
 

Catfish

Ars Tribunus Angusticlavius
9,954
Is there something missing from that code? I don't see any loops
Sorry - I have a habit of grouping 'if' statements under the term 'loops'.

One thing I'd like to point out is your filename should have backslashes rather than forward slashes
It does the same if I assign something trivial like imagename = "x"


Are you sure you can cast a CString to (char *)? That may be killing you...
:confused: I'm not casting CString to a char - those are some unrelated structures.
 
in.read((char*)&flockWeights, sizeof(flockWeights)*2);
in.read((char*)&boidModifier, sizeof(boidModifier)*2);
in.read((char*)&normalisedVolume, sizeof(normalisedVolume)*2);
in.read((char*)&filteredSpectrum, sizeof(filteredSpectrum)*630);
I think these are overflowing your stack... for the number of bytes to be read, either use:

1) sizeof(type) * arraySize
2) sizeof(arrayVariable)

What you seem to be doing is sizeof(arrayVariable) * arraySize which causes too much stuff to be read.
code:<HR>
Code:
char x[10];
char* x2 = x;<P>sizeof(x) is 10
sizeof(x2) is 4 (size of the pointer)
sizeof(char) is obviously 1

Edit: So in your case, for flockWeights, you'd use something like sizeof(double) * 2 (assuming they're doubles).
 
euzeka yuo are teh winnar! :)
Glad I could help :D
Thanks for pointing that out - I didn't realise that sizeof functioned like that for variable arrays.
Well it's not exactly obvious after all. sizeof has a few subtleties... I was reading a while back on www.cuj.com how they used sizeof with templates to determine if a type could be converted into another type (really weird code).
 
Status
Not open for further replies.