On Wed, 11 Jul 2018 21:13:39 +0200 Frédéric wrote:
F> I am creating a lot of bitmaps that I just want to store on disk; I do
F> not need to show them in the GUI. For this, I use wxMemoryDC and
F> wxBitmap. However, to speedup the process, I split the work in
F> different threads but I get a lot of segmentation faults when
F> constructing wxMemoryDC or when calling wxMemoryDC::SelectObject():
F> this apparently calls to SetFont or SetPen which ends in a reference
F> counting area of GDI objects that causes the segfaults.
F>
F> So I mutex protected wxMemory creation and all calls to SetFont and
F> SetForgroundColour... but the interest of multi-threading is not so
F> great then.
A possible solution is to use wxImage in the threads and then convert it
to wxBitmap. This should be MT-safe, I think.
F> I found those 2 threads from 10 years ago that describe quite well the
F> problem without giving any solution:
F> -
https://groups.google.com/forum/#!topic/wx-users/ZAnUaEot0Ys
F> -
http://wxwidgets.10942.n7.nabble.com/making-GDI-objects-thread-safe-td22667.html
F>
F> However, in one of the treads, Vadim said this:
F> "this should already work if you use wxMemoryDC for wxBitmap creation
F> and if you create your pens, brushes &c in the thread and only use them
F> there. If it doesn't, this is a bug we may fix."
F>
F> It seems to indicate that it is possible to make it work but I just do
F> not manage to do it. In particular, what does it mean to "create your
F> pends, brushes... in the thread"?
Create objects only accessible from this thread.
F> For example, I do the following in the worker threads:
F> dc.SetForegroundColour(*wxWHITE)
F> or
F> dc.SetFont(wxFont{15, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL,
F> wxFONTWEIGHT_BOLD})
F>
F> Of course my threads are completely independant from each other, they
F> do not share anything but clearly wxWHITE is common to all threads but
F> I do not modify it...
wxWHITE and other stock objects are just (fancy) global variables, so you
can't use them. You should be able to use wxColour(0xff, 0xff, 0xff)
however, at least under MSW. I'm not really sure about other systems
though.
Regards,
VZ
--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/