tags:

views:

75

answers:

3

In the follwoing code, using libxml libraries :

key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
                if (flag == 1)
                {
                        image2 = key;
                        printf("the image 2 is %s \n", image2);
                        flag = 2;
                }
                if(flag == 0)
                {
                        image1 = key;
                        printf("the image 1 is %s \n", image1);
                        flag = 1;
                }
                    //printf("SRC of the file is: %s\n", key);

                xmlFree(key);
            printf("the image 1 is %s \n", image1);

the two printf are giving me different outputs.

The output is :

the image 1 is 1.png 
the image 1 is 0p�  g 
the image 2 is 2.png 
the image 1 is 0p�  g
A: 

You haven't mentioned what are image1 and key? I guess pointers (because of the tag and because of the function name + the c-tag). If so, the problem is with calling xmlFree(key); before calling printf - both key and image1 point to the same place of memory (if the are pointers ), so that's the problem. Put xmlFre(key) after the printf.

Also you should add else in front of the second if (:

Kiril Kirov
A: 

As far as I can see it from that short piece of undocumented code, it does not really make sense to call xmlfree(), which converts for example 1.png to 0p� g.

but the real reason why the var image1 is changed is because image1 is a reference to key, which then is update by calling xmlfree(key).

sergej
Yeah thanks got the problem.
abhishekgupta92
+6  A: 

After the line image1 = key, image1 and keypoint to the same memory area.

I suppose xmlFree(key); alter this memory area. If you want the content of this string survive to the xmlFree, you should consider using the function strcpy before deallocationg the pointer.

FabienAndre
Thanks dis worked like a charm.
abhishekgupta92