0

I'm looking to support rendering SVG images as Bitmaps, I'm using librsvg. So far it works well and draws using Cairo, hooray! However I've run into an issue:

We have a control that is similar to Windows Explorer, e.g. it creates thumbnail-sized images of files in a directory, so the product would be extended to generate thumbnails of SVG's.

It uses background threads to generate the thumbnail images so the UI isn't frozen as it's working. The code has been in production for a long time so no problem as far as concurrency issues go. But it seems when rsvg_handle_new() is called from a thread other than the main thread an Access Violation occurs, when it's not I have no issue working with librsvg.

Looking at the stack trace the crash appears to be occurring in libxml2 (which is a dependency of librsvg) at xmlSetGlobalState(), the problem seems the same as reported here libxml2 crash on second use on Windows which doesn't seem to have a resolution. I would limit my use of the library to the main thread only if I could, but I'm stuck with this existing architecture which produces the thumbnails off the main thread.

I found http://www.xmlsoft.org/threads.html which suggests calling xmlInitParser() in the main thread first which seems to be called by librsvg during the RsvgHandle creation (I think).

So I'm not sure if it's really a libxml2 problem, a problem with the way librsvg is using libxml2, or a problem with the way I'm trying to use librsvg?

Any libxml2 or librsvg experts?

Community
  • 1
  • 1
JosephA
  • 1,187
  • 3
  • 13
  • 27

2 Answers2

2

Despite the fact, that malloc() and free() or whatever memory handling implementations are not necessarily thread safe in C < 11, there's always the problem of shared/global memory. File handles to the same file in different threads aren't that bad as long as they're read only.

However, starting with libxml2 2.4.7, you might be able to enable thread safety at the API level, for single threads per document: http://www.xmlsoft.org/threads.html

When I look at the sources of libxml2 2.9.1, I'm positive that thread safety is fully implemented, despite global mutexes, there's also an atomic allocation function.

Downloads: ftp://xmlsoft.org/libxml2

Regards, Rabi

rabi
  • 21
  • 3
0

make 100% sure xmlCleanupParser() is never called in your code or in any of the other libraries using libxml2, it destroys the global state by assuming there is no remaining use of the parser in the whole program.

http://xmlsoft.org/html/libxml-parser.html#xmlCleanupParser

Daniel