Monday 15 September 2014

c - how to tell which libxml2 functions returning objects that need to be free? -


Is there a reference on whether the object returned by a particular function should be freed? Currently, I just try ... free () tasks like "new", "copy", "make", come back from the works in their names, and if the program does not crash, then I think the call correct.

This is a very bad idea if you are working with the Library API that points to the Object Objects , especially if allocated objects are of an "opaque" type, where you do not directly access them, but only other functions, the library will almost certainly give these items Stop / release / destroy / free your own car There are several reasons why you should not use only free :

  1. The object created by the library received by malloc There is no possibility of single allocation . In this perhaps there are indications for other allocation, and if you simply free , you will lose the ability to free all those allotations and poor memory leaks will occur.

  2. The object returned from the returned indicator malloc can not be started by the library, but rather than some points. In this case, it invites free to undefined behavior, and may work, or crash or corrupt memory, so that you can only see the effect of corruption later on.

  3. The library can keep the internal index of the object that has allocated it (in standard code stdio would be a great example of this) if You will open objects behind it, it will use free memory later and apply undefined behavior. The most important rule of using

    free is that you never

    unless the indicator (or "as of") was not received by malloc . Some libraries can document that they return the indicator received from malloc , and you should send it to free , when you do not need it, but unless You do not see this contract free . For libxml2 , here's the manual:

    It should be aware that libxml2 to allocate objects to free.

No comments:

Post a Comment