VS2010 Express crashing when writing buffer to a file

Hi.

I have just moved over to VS2010 Express from C::B and have been trying to move some code I wrote in C::B over to VS. I have a problem when trying to write a buffer to a file. If I run either release or debug with debugging it runs fine but if I run the code as “Start without debugging” it crashes when I try and write the buffer. This is the same when I run the standard exe from the directory in which it was created. The file which I am writing to is always created.

This did run fine in C::B and as I cant use the debugger I’m stumped.

  
  
#include "DataWriter.h"  
  
DataWriter::DataWriter(char *_filename) {  
  
	containers  = new dataContainer[MAX_RECORDING_TIME];  
  
	strncpy(filename, _filename, 256);  
	item_count  = 1;  
	frame       = 0;  
	open        = false;  
}  
  
string DataWriter::startRecording() {  
  
	if(open) {  
		stopRecording();  
	}  
	char _filename[128];  
	sprintf(_filename, "%s-%d.knct", filename, item_count);   
  
	char buf[256];  
	sprintf(buf, "%s%s", ofToDataPath("./", true).c_str(), _filename, item_count);  
  
	char buf2[256];  
	sprintf(buf2, "%s%s",  "", _filename, item_count);  
  
	filenameOut = buf2;  
  
	outFile = fopen(buf, "wb");  
  
	if(outFile != NULL) {  
		startClock = get_stamp_sec();  
		open = true;  
		cout << "DataWriter::startRecording() | file open success. : create file " << buf << " ..." << endl;  
		cout << "DataWriter::startRecording() | start recording..." << endl;  
		string outName = _filename;  
		return outName;  
	} else {  
		cout << "DataWriter::startRecording() | file open error. : failed to open " << buf << " ..." << endl;  
		return NULL;  
	}  
}  
  
bool DataWriter::update(dataContainer *buffer) {  
	if(open) {  
		if(frame < MAX_RECORDING_TIME) {  
			memcpy(&(containers[frame]), buffer, sizeof(dataContainer));  
			containers[frame].stamp = get_stamp_sec() - startClock;  
  
			frame++;  
			return true;  
		} else {  
			stopRecording();  
			return false;  
		}  
	} else {  
		cout << "DataWriter::update() | file: " << filename << " file not open for writing" << endl;  
	}  
	return false;  
}  
  
void DataWriter::stopRecording() {  
	if(open) {  
		int count = fwrite(containers, sizeof(dataContainer), frame, outFile);  
		fclose(outFile);  
		open = false;  
		frame = 0;  
		cout << "DataWriter::stopRecording() | stop recording #" << item_count << " ... " << count << " frame write." << endl;  
		cout << "DataWriter::stopRecording() | " << containers[count - 1].stamp << "sec." << endl;  
		item_count++;  
	} else {  
		cout << "DataWriter::stopRecording() | waiting. press '!' to start recording" << endl;  
	}  
}  
  
  

TIA

  
memcpy(&(containers[frame]), buffer, sizeof(dataContainer));   

This line looks a little suspicious to me. Is there anything in your dataContainer that might be owned by another thread or be unallocated or in some way or another, em, weird?

Thanks Joshua,

I was writing a timestamp in the data container and had to find an alternative to gettimeofday() so used ofGetSystemTimeMicros() which caused the crash.

I used

  
  
  
SYSTEMTIME st;      
GetSystemTime(&st);  
  
  

instead