On 08/27/2013 10:56 PM, Krzysztof Kosiński wrote:
2013/8/27 Arshdeep Singh <moduli16@...400...>:
I added: new (&(priv->nodes)) std::mapstd::string,RecolorWheelNode(); to recolor_wheel_init (RecolorWheel *wheel) .
Honestly I have never seen this style of code, so I really don't understand what it does. The good news is it does get to run the dialog box of 'Recolor Artwork' i.e. no error while the dialog is loaded, but the moment I draw a shape (active selection) the application crashes.
This 'style of code' is known as 'placement new' and it calls the constructor of an object with an explicit 'this' pointer. It needs to be used when memory for an object is allocated from a different source than operator new.
In normal C++, placement new is rarely used, because objects are typically allocated and constructed at the same time with operator new. However, if you allocate memory from a different source (in this case from GObject), you need to call the constructor yourself.
Be careful with placement new onto GObjects allocated by g_object_new. g_object_new puts some GObject type info into the first few bytes of the struct which is trashed when placement new invokes the whole constructor hierarchy beginning at GObject (the C++ class) since this class' constructor can not preserve the type info field's memory content. You will not notice that in normal operation except when using dynamic GObject type casts (those CLASS_NAME(object) macros) which will throw warnings on the console, though not crash, or when using g_object_unref, since the reference counter is initialized with 1 but overwritten to 0. Also, at many points in Inkscape's code base there are asserts that will crash Inkscape when they encounter a GObject lacking type information.
Best, Sebastian