Peter4n31 0 Newbie Poster

Hello. I am debugging a larger project and a tool called Crash Validator keeps pointing line 1 in my code extract:"inline bool SortKnownFiles2 (etc.)" in DirLoader.cpp
Error message in Crash Validator is “Cannot create a file when that file already exists.” (in c++ it runs fine, except a few random crashes I am just trying to find)
Do you have any ideas where could be the problems, because I do not see there any. Any help is much appreciated. Thank you.

Code extract from DirLoader.cpp

inline bool SortKnownFiles2(const CDirLoader::TKnownFile *i1, const CDirLoader::TKnownFile *i2) 
{
	if (i1->name == i2->name) 
	{
		if (i1->grf && !i2->grf) return true;
		if (!i1->grf && i2->grf) return false;		
		if (i1->extensionType == i2->extensionType && i1->extensionType!=0) return i1->path < i2->path;		
		return i1->ext < i2->ext;
	}
	return i1->name < i2->name;
}

void CDirLoader::PrepareDirLoaderForSearch()
{
	files.clear();
	for (DWORD i=0;i<fileStack.size();i++) files.push_back(&fileStack[i]);
	sort(files.begin(), files.end(), SortKnownFiles2); // This launches SortKnownFiles2
isSorted=true;
// ... other code

Second problem is a use of deleted pointer in Line 16.
Code extract from the same DirLoader.cpp

void CDirLoader::AddGroupFile(const std::string& _fname)
{
 // a lot of code...
 FILE *f = fopen(fname.c_str(), "rb");
 struct grfHeader
	{
		DWORD grfLen;
		DWORD fnum;  
		DWORD fheadLen; 
		DWORD encodingType;
		DWORD userDataLen;
	} header;

 	fread(&header, sizeof(grfHeader), 1, f);

        TKnownGRF *grf = new TKnownGRF; // This should be the problem
	grf->filePath = fname;
	grf->encodingType = header.encodingType;
	grf->userDataLen = header.userDataLen;
	grf->userData = NULL;
	if (grf->userDataLen)
	{
		grf->userData = new char[header.userDataLen];
		fread(grf->userData, grf->userDataLen, 1, f);
		globalLog->WriteLog("userdata: %i bytes\n", grf->userDataLen);
	}
	grfs.push_back(grf);

Code extract from DirLoader.h

class CDirLoader  
{
public:
	struct TKnownGRF
	{
		std::string filePath;
		DWORD encodingType;
		DWORD userDataLen;
		char *userData;
	};

	struct TKnownFile
	{
		TKnownFile():grfOffset(0),fileLength(0),extensionType(0),zoom(0),grf(0),fsIndex(0){}
		std::string filePath; 
		std::string path; 
		std::string name; 
		std::string ext;  

		TKnownGRF *grf;
		int grfOffset;		
		int fileLength;    		
		char extensionType; 
		char zoom;
		int fsIndex; 	
	};

	CDirLoader();
	virtual ~CDirLoader();
     // Other code
	std::vector<TKnownGRF*> grfs;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.