setStyle -> USER Objects leak



  • Re: Change the size of ">>" arrow when a QToolBar is too small

    It is possible to change the size of the >> extension button in the Toolbar, by subclassing QProxyStyle as shown below.

    virtual int MyProxyStyle::pixelMetric(PixelMetric pm, const QStyleOption* option, const QWidget* widget) const
    {
    if( pm == QStyle::PM_ToolBarExtensionExtent )
    return XXX; // width for horizontal, height for vertical toolbars
    return QProxyStyle::pixelMetric(pm, option, widget);
    }

    But setStyle() using MyProxyStyle increases the USER Objects which eventually leads to crash after many days of continuously creation and deletion of Toolbar.

    Does setStyle() has memory leak?


  • Lifetime Qt Champion

    Hi,

    How are you using your custom style ?
    Why are you constantly deleting/creating that toolbar ?


  • Qt Champions 2017

    @KIT2005 said in setStyle -> USER Objects leak:

    Does setStyle() has memory leak?

    Well, on QWidgets:.setStyle, ownership is not transferred, so if you new the style each time you assign it, and never delete the last one, you will leak.



  • Hi,

    I have many pages with different controls. One such control has the toolbar and so when I switch from one page to another having the control with toolbar, the USER objects increases.

    During the creation of Toolbar I have used
    MyProxyStyle *proxyStyle = new MyProxyStyle ();
    if(proxyStyle )
    setStyle(proxyStyle );

    In the destructor, I have used
    if(proxyStyle )
    {
    delete proxyStyle ;
    proxyStyle = NULL;
    }

    I have concluded that by commenting the line setStyle() the USER objects are not increasing. So I am not sure if there is a leak with QT or i am doing something wrong


  • Qt Champions 2017

    @KIT2005
    Hi
    but is dtor called on each time you switch ?
    also the code shown has a local style
    MyProxyStyle *proxyStyle = ... // local

    so in dtor
    if(proxyStyle )
    {
    delete proxyStyle ;

    what style is that ? seems not to be the one you new.


  • Lifetime Qt Champion

    Your if statement won't help, you are creating a local variable named proxyStyle so unless you run out of memory, it will alway be true as the allocation will succeed.



  • @mrjj
    I am doing the page switch at regular interval and my dtor is also called everytime.

    MyProxyStyle *proxyStyle = ../ is not local but a member variable. I typed the statement to show the creation part and its a typo error.

    It is actually

    proxyStyle = new MyProxyStyle ();
    if(proxyStyle )
    setStyle(proxyStyle );



  • @mrjj

    Thanks for the hint"QWidgets:.setStyle, ownership is not transferred". It helped me fix the issue.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.