ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I am using libxml2 (libxml2-2.7.6-1.fc10.x86_64 )
I get crash when I call xmlFreeTextReader after the XMl file is processed. Following is the sample code that I am using:
Okay the clue here is with the cfree in your error. This is a big giveaway you are trying to free something which has not been allocated. The question is what it is. first of all if you look at the libxml2 api you will find xmlReaderForMemory returns NULL upon error so you need to check for that even if you know all the parameters you are passing in are valid that would necessarily guarantee you a valid pointer. Secondly if you also look at the documentation for xmlTextReaderConstName you will find it free's the resource associated with it. this is something you have to be careful about with libxml2 as some methods do that. so i would say that when you call xmlFreeTextReader you get a double free error
Okay the clue here is with the cfree in your error. This is a big giveaway you are trying to free something which has not been allocated. The question is what it is. first of all if you look at the libxml2 api you will find xmlReaderForMemory returns NULL upon error so you need to check for that even if you know all the parameters you are passing in are valid that would necessarily guarantee you a valid pointer. Secondly if you also look at the documentation for xmlTextReaderConstName you will find it free's the resource associated with it. this is something you have to be careful about with libxml2 as some methods do that. so i would say that when you call xmlFreeTextReader you get a double free error
I checked xmlReaderForMemory return value and it gives a valid pointer. Also it does not crash at xmlTextReaderConstName call. But it crashes when xmlFreeTextReader is called. I am checking the xmlTextPointer before calling xmlFreeTextReader.
~/src/xml/crash$ make crash
gcc -Wall -Wextra -g -O0 -I/usr/local/include/libxml2 -c -o crash.o crash.c
gcc crash.o -lxml2 -lm -o crash
~/src/xml/crash$ ./crash
Name : (null)
Name : something
As you can see, when I tried there was no crash, probably something in the part of the program you didn't post causes the problem.
Quote:
Originally Posted by pgpython
Secondly if you also look at the documentation for xmlTextReaderConstName you will find it free's the resource associated with it. this is something you have to be careful about with libxml2 as some methods do that. so i would say that when you call xmlFreeTextReader you get a double free error
The qualified name of the node, equal to Prefix :LocalName.
reader: the xmlTextReaderPtr used Returns: the local name or NULL if not available, the string is deallocated with the reader.
That just warns against freeing the string returned by xmlTextReaderConstName(), which the code (at least what was posted) doesn't do.
Its really difficult to see what is going on here for several reasons. there isn't enough comments to understand what is going on and secondly you have not provided enough data for the code to be understood and tested. such as what your passing into your function when it happens and the structure of dmxLocal and devServices as well. As a point of reference experience has taught me its a good idea in C to have a few as places as possible which call malloc,calloc,free and similar stuff so it may be an idea to create functionality for that part.
Might I also suggest you use valgrind and gdb. valgrind in particular is very good at detecting memory leaks and double free errors. While testing I would compile it with debugging symbols in the executable. It will make the program but it makes much easier to debug -ggdb in GCC
Thanks a lot for the responses. Your responses really help me. I could solve the crash.
Here is what I did :
As suggested by pgpython I used VALGRIND and found a number of memory issues. I fixed one by one all the memory related issues and in the end this crash vanished.
Might be at some place because of the corruption this memory was held on because of which when this was released it was crashing.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.