Eventual Heap Corruption

I really don't know how to go on... Sorry to bother again with a problem. I'm trying to write an XML-Reader. It's supposed to read in the complete document and save the nodes in an array, the attributes and texts as well.
But as I read in the second element windows determinds a breakpoint which may be due to a corruption of the heap as Windows "sais"...

Here is the class and the structures:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <string>
#define __STRING_INCLUDED

/* Structs */
struct documentElement {
	std::string		 name;
};

struct xmlAttribute {
	std::string		 name;
	std::string		 value;
};

struct xmlElement {
	std::string		 name;
	xmlAttribute	*attributes;
	std::string		 text;

	xmlElement		*parent;
	xmlElement		*subelements;
};


/* The xmlIO-Class */
class xmlIO
{
private:
	// Variables
	int				 count;
	std::string		 path;
	documentElement  document;
	xmlAttribute	*attributes;
	xmlElement		*elements;

	struct error {
		int			 id;
		char		*name;
		char		*message;
	} err;

	// Methods
	xmlAttribute	*getAttributes(		char*, xmlElement* );
	xmlElement		 getElements(		char* );
	char			*getText(			void );
	

public:
	xmlIO(  void );
	xmlIO(  char* );
	~xmlIO( void );
	bool getError( void );
};


And now to the functions I think of they might be important (I always try to hold my questions as brief as possible...):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// In this function the error occures
xmlElement xmlIO::getElements( char *szLine ) {
	xmlElement currElem;
	currElem.name.assign( (const char*)substr( (const char*)szLine, strsearch( (const char*)szLine, "<" ) + 1, strsearch( (const char*)szLine, ">" ) - 1 ) ); // here the error occures as I try to read in the second element
	currElem.attributes = getAttributes( szLine, &currElem ); // <<< under Construction

	return currElem;
}

// The class constructor used to read in the XML-File
xmlIO::xmlIO( char *xmlFileIn ) {
	// Init Variables and open the file
	count = 0;
	FILE *xmlDocument;

	// Init the arrays
	elements = new xmlElement;
	attributes = new xmlAttribute;


	// Variables when succeeded
	char *szBuffer = new char, *szBuff = new char;
	int nSearchResult1 = 0, nSearchResult2 = 0;

	// Pass the <?xml version="1.0"?> line and read the document element
	fscanf( xmlDocument, "%[^\n]s", szBuffer );
	if( strsearch( (const char*)szBuffer, "<?" ) != -1 ) {
		fgets( szBuffer, strlen( (const char*)szBuffer ), xmlDocument );
		fscanf( xmlDocument, "%[^\n]s", szBuffer ); // Read the next line
	}

	// Search for "<" and ">"
	nSearchResult1 = strsearch( (const char*)szBuffer, "<" );
	nSearchResult2 = strsearch( (const char*)szBuffer, ">" );

	// Don't know why I actually wrote the following... ^^'
	// A bit useless security routine as the following "elements" may still be in a non-xml-format...
	if( nSearchResult1 == -1 || nSearchResult2 == -1 ) {
		err.id = 101;
		err.name = "Invalid XML-Format";
		err.message = "Die angegebene Datei ist nicht in einem gültigen XML-Format!";
		return;
	} else {
		// Get the document's root xmlElement
		document.name = substr( (const char*)szBuffer, nSearchResult1, nSearchResult2 );

		// Get all sub elements
		while( !feof( xmlDocument ) ) {
			fgets( szBuffer, strlen( (const char*)szBuffer ), xmlDocument );
			fscanf( xmlDocument, "%[^\n]s", szBuffer );
			elements[ count++ ] = getElements( szBuffer );
		}
	}

	// Delete Pointers and close docs
	fclose( xmlDocument );
	if( logging ) fclose( log );
	delete xmlDocument;
	delete log;
	delete[] szBuffer;
	delete[] szBuff;

	return;
}


Sorry for this bunch of code. Hope you can help me, I've commented it so I can remember what it's actually supposed to do when I make a break on it. :)
I always structure it too, so I guess it is not a big deal to get straight with the code... At least I hope so :)

DarkDragon1993
The most common causes of heap corruption are supernumerary deallocations, buffer overflows, and dereferences of invalid or deleted pointers. Check you memory management and your array indices.

Also, I see you're using C file I/O functions. I'd suggest switching to C++ functions for added safety, or verify the parameters being passed.
Okay, I will do so. Thank you for your answer, I was more than clueless.
Topic archived. No new replies allowed.