davidre added a comment.

  Flickr works here F8325269: Screenshot_20200516_095930.png 
<https://phabricator.kde.org/F8325269>
  
  > For the leaks, I really have no idea. (also asked some friends but no help) 
C++ isn't my primary programming language. If you can share some 
document/tutorials/examples...
  
  In C++ there is automatic and dynamic storage duration (and some others but 
you don't need to care about them for now). Automatic is the normal when you 
write
  
     void f() {
        int i = 0;
        [complicated code]
    }
  
  i is automatically allocated at the start of the function and deallocated at 
the end of the function. Objects with automatic storage duration are 
deallocated at the end of the scope they were declared in. You can't use a 
variable that you declare in a for loop or inside an if block outside of them 
because the scope they were declared in (the foor loop or the if block) has 
ended. You can also manually create a scope by wrapping code with braces.
  
  Dynamic storage duration happens when you manually allocate memory by writing 
`new Object` or more C-like by calling `malloc` or `calloc`. Here the memory is 
not automatically freed again. It's on the programmer to deallocate it when 
it's no longer needed. You need to call `delete` (or `free` if you used 
`malloc`) to free the memory again.
  
  Here it's not a huge problem because I guess the module will be only 
instantiated once but it's something one should always keep an eye on it. 
Imagine if the class was instantiated multilple times. After some (or longer) 
time your memory would be full because each object allocates a 
`Plasma::DataEngineConsumer` but never frees the corresponding memory even if 
the object itself is destroyed. That's what's called a leak.  You have to call 
`delete consumer` in the destructor of `PotdModule`.
  
  There are some tools that help with memory managment `std::unique_ptr` and 
`QScopePointer` wrap a pointer created with new and will delete it if they are 
destroyed themelves, so you don't forget the delete. `std::shared_ptr` and 
`QSharedPointer` count how many places hold a refrence to it, if noone holds a 
reference anymore the memory is freed. Finally in Qt there is the 
`QObject(QObject *parent)` pattern.  A QObject will delete it this children. So 
if you write `new QWidget(this)` you don't need to care about freeing the 
memory occupied of  the new widget.

REPOSITORY
  R114 Plasma Addons

REVISION DETAIL
  https://phabricator.kde.org/D29140

To: guoyunhe, #plasma, davidre, broulik
Cc: davidre, ngraham, plasma-devel, Orage, LeGast00n, The-Feren-OS-Dev, cblack, 
jraleigh, zachus, fbampaloukas, ragreen, ZrenBot, himcesjf, lesliezhai, 
ali-mohamed, jensreuterberg, abetts, sebas, apol, ahiemstra, mart

Reply via email to