-1

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

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 (:

Comments

-1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.