Skip to content

Commit

Permalink
xml: Cleanup libxml2 parser when exiting library
Browse files Browse the repository at this point in the history
When libiio is unloaded (when the client program exits), cleanup libxml2
so that memory analyzer tools like Valgrind won't detect a memory leak.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Apr 27, 2021
1 parent ebf3c7e commit 6d49eec
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,47 @@ struct iio_context * xml_create_context_mem(const char *xml, size_t len)
xmlFreeDoc(doc);
return ctx;
}


static void cleanup_libxml2_stuff(void)
{
/*
* This function will be called only when the libiio library is
* unloaded (e.g. when the program exits).
*
* Cleanup libxml2 so that memory analyzer tools like Valgrind won't
* detect a memory leak.
*/
xmlCleanupParser();
xmlMemoryDump();
}

#if defined(_MSC_BUILD)
#pragma section(".CRT$XCU", read)
#define __CONSTRUCTOR(f, p) \
static void f(void); \
__declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
__pragma(comment(linker,"/include:" p #f "_")) \
static void f(void)
#ifdef _WIN64
#define _CONSTRUCTOR(f) __CONSTRUCTOR(f, "")
#else
#define _CONSTRUCTOR(f) __CONSTRUCTOR(f, "_")
#endif
#elif defined(__GNUC__)
#define _CONSTRUCTOR(f) static void __attribute__((constructor)) f(void)
#else
#define _CONSTRUCTOR(f) static void f(void)
#endif

_CONSTRUCTOR(initialize)
{
/*
* When the library loads, register our destructor.
* Do it here and not in the context creation function,
* as it could otherwise end up registering the destructor
* many times.
*/
atexit(cleanup_libxml2_stuff);
}
#undef _CONSTRUCTOR

0 comments on commit 6d49eec

Please sign in to comment.