3 ms·
C-style memory management is important to know about but in C++ land, RAII and C++11 features are here to help. Move semantics, use of containers, references et
by 72deluxe 11y ago
C-style memory management is important to know about but in C++ land, RAII and C++11 features are here to help. Move semantics, use of containers, references etc. all mean many of these bugs are a thing of the past.
In addition, extra tools and profiling / static code analysis has helped me to write better code.
- 0xcde4c3db 11y agoThe language has gotten to a pretty nice place, but the ecosystem is nowhere near caught up. A lot of APIs still haven't even incorporated the idiom of passing const references, almost exclusively taking pointers to non-const and defining their own idiosyncratic ownership schemes. Qt4 is a good example: RAII of widgets is hazardous because windows/containers will delete their "children" (i.e. things that are under them in the widget hierarchy, not members), even if they're on the stack. This means that simply declaring local variables in the wrong order can cause a double delete. I encountered this in tutorial code on the Qt site, which kind of put me off choosing Qt for new projects.
- 72deluxe 11y agoThis is very true. What did you use instead? wxWidgets is the same - when a wxWindow gets deleted, it will remove its children in the view hierarchy, which are likely not children of it as data members. I think a lot of this comes from the fact that underneath native pointers are being used (eg handles on Windows) and widgets/controls are not necessarily going to be data members of a parent. It also means you can reparent widgets/controls to other GUI items at runtime. I do this (for example) when switching notebook pages to rehouse a GUI element in order to save RAM; I just assign it new data to display and it means less RAM usage and creation time for each notebook page. For your own data controls, we can still ensure that they clean themselves up alright and safely, just use pointers for the GUI items. In some sense, the GUI toolkit takes care of deleting its own members, eg. wxWidgets will delete all of a parent wxWindow children at teardown, so it isn't a leak situation.
- 0xcde4c3db 11y ago> This is very true. What did you use instead? Since I was looking at it for hobby/educational projects, I basically decided to not do something with a GUI. I think I had also looked at wxWidgets and FLTK, hoping that one of them was cleaner and more modern; FLTK managed the "cleaner" part, at least. The last time I looked at C++ GUI libraries it seemed like JUCE was the most promising, but it also had a bit of weirdness (GPL license without linking exception, heavy bias toward audio applications).