glib detected: malloc() memory curruption

I am using libxml2 library for XMl parsing and libxml++ is C++ wrapper over that. So I am using API of libxml++. I am creating my class and composing instance xmlpp::Node *pNode in that. my class also have funciton prepareXPathQuery() which creates query string and have other fucntion fireXpathQuery which internallly calls pNode->find() function which returns sets of node which matches with fired query but when I fire query it gives error glib detected: malloc() memory curruption and prints backtrace.

/*implemntation of class */
#include "ConfigFileParser.hpp"
include "defines.hpp"
#include <libxml++/libxml++.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class ConfigFileParser
{
std::string filePath;
xmlpp::DomParser parser;
xmlpp::Node *pNode;
char *query;
public:
/***************************************************
* Function Name : ConfigDataStructure
***********************************************//**
* @brief
* @param
* @return none
* @see
***************************************************/
ConfigFileParser();
/***************************************************
* Function Name : ~ConfigFileParser
***********************************************//**
* @brief destructor for class ConfigFileParser
* @param none
* @return none
* @see
***************************************************/
~ConfigFileParser();
/***************************************************
* Function Name : prepareXpathQuery
***********************************************//**
* @brief prepere XPATH query to fire on XML file
* @param char *
* @param char *
* @param char *
* @param int depth=0
* @return void
* @see
***************************************************/
void prepareXpathQuery(char *, char *, char *, int depth=0);
/***************************************************
* Function Name : fireXpathQuery
***********************************************//**
* @brief It fires XPATH query on node
* @param node it's node on which query have to fire
* @return NodeSet it's the set of nodes foungd by query
* @see
***************************************************/
xmlpp::NodeSet fireXpathQuery(xmlpp::Node *node = NULL);
};
ConfigFileParser::ConfigFileParser()
{
filePath = XML_FILE_PATH;//XML file path is defined in defines.hpp
query = new char(MAX_QUERY_LENGTH);
parser.set_substitute_entities();
parser.parse_file(filePath);
pNode = parser.get_document()->get_root_node();
}
ConfigFileParser::~ConfigFileParser()
{
delete(pNode);
}
void ConfigFileParser::prepareXpathQuery(char *nodeName, char *attType, char *attName, int depth)
{
if(depth == 0)
{
sprintf(query,"%s%s%s%s%s%s%s","./",nodeName,"[@",attType,"='",attName,"']");
}
else
sprintf(query,"%s%s%s%s%s%s%s","//",nodeName,"[@",attType,"='",attName,"']");
}
xmlpp::NodeSet ConfigFileParser::fireXpathQuery(xmlpp::Node *node)
{
xmlpp::NodeSet set;
if(node != NULL)
set = node->find(query);
else
set = pNode->find(query);
return set;
}
 
int main()
{
int elementID;
int address;
char *text=NULL;
ConfigFileParser fileParser;
fileParser.prepareXpathQuery((char *)"Element", (char *)"type", (char *)"ModuleVoiceElem", 1);
xmlpp::NodeSet set = fileParser.fireXpathQuery();
xmlpp::Element *nodeElement = dynamic_cast<xmlpp::Element *>(*set.begin());
getBasicAttributes(elementID, address, text,nodeElement);
fillAmpDataStructure();
}

when it run this program it gives error glib detected: malloc() memory curruption at highlighted line. Please help me. I dont know why this error comes.

Thanks in advance,
Sharad Wagh.

Not using code tags has destroyed all your indentation and filled your code with smilies. Please see the video tutorial on how to do so.

---------- Post updated at 09:26 AM ---------- Previous update was at 09:15 AM ----------

Instead of dynamic_cast, why not use the actual type set.begin() returns? After all, it might not be valid in all circumstances, and casting a 'no results' return into a pointer of the kind you wanted would probably crash.

even i faced the same problem in linux.adding -lmcheck while linking in my make file helped me in finding what the exact problem is and rectifying the problem.