www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Potential memory leak?

reply Qian Xu <qian.xu funkwerk-itk.com> writes:
Hi All,

I am using QtD to do some gui stuff. As the QtD documentation described, Qt-
data types should be declared with keyword "scope", so that all variables 
can be deallocated in a right order.

I found a memory leak problem accidentally, when I executed the following 
code very frequently:

// public QRegion united(QRegion region) {...}
 
  scope regionA = new QRegion;
  scope regionB = regionA.united(anotherRegion); // Good Example
  scope regionA = regionA.united(anotherRegion); // Bad Example

I create a variable regionA and unite it with another region. The result of 
the function united is assigned to variable regionB. Everything is fine.

The third line sounds be correct as well. A new instance will be created and 
then the variable regionA will be assigned to this new instance, the old 
instance will be deallocated as soon as the code exits its scope. 

However the old instance, where the variable regionA previously was pointed 
to, still exists. 

I cannot reproceduce this problem with simple D data types.
But do you think this is a bug of dmd 1.051?

Qian Xu
Nov 27 2009
parent reply Eldar Insafutdinov <e.insafutdinov gmail.com> writes:
Qian Xu Wrote:

 Hi All,
 
 I am using QtD to do some gui stuff. As the QtD documentation described, Qt-
 data types should be declared with keyword "scope", so that all variables 
 can be deallocated in a right order.
 
 I found a memory leak problem accidentally, when I executed the following 
 code very frequently:
 
 // public QRegion united(QRegion region) {...}
  
   scope regionA = new QRegion;
   scope regionB = regionA.united(anotherRegion); // Good Example
   scope regionA = regionA.united(anotherRegion); // Bad Example
 
 I create a variable regionA and unite it with another region. The result of 
 the function united is assigned to variable regionB. Everything is fine.
 
 The third line sounds be correct as well. A new instance will be created and 
 then the variable regionA will be assigned to this new instance, the old 
 instance will be deallocated as soon as the code exits its scope. 
 
 However the old instance, where the variable regionA previously was pointed 
 to, still exists. 
 
 I cannot reproceduce this problem with simple D data types.
 But do you think this is a bug of dmd 1.051?
 
 Qian Xu
 
 
Hi, You only have to use scope for top-level QObject subclasses. For anything else you should use auto. Nevertheless, it would be useful if you submit a bug report. Thanks.
Nov 29 2009
parent Qian Xu <qian.xu funkwerk-itk.com> writes:
Eldar Insafutdinov wrote:
 
 You only have to use scope for top-level QObject subclasses. For anything
 else you should use auto. Nevertheless, it would be useful if you submit a
 bug report.
Even when my application is multi-thread? I am not sure, if I can declare QPixmap also as "auto"
Nov 30 2009