Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTextEdit + Singleton = Memleak?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTextEdit + Singleton = Memleak?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Adelost
    wrote on last edited by
    #1

    I am using Qt 4.8.4 and if I make a singleton out of a QMainWindow and assign a QTextEdit, I get a huge memory leak. Any ideas?

    @class Window : public QMainWindow
    {
    Q_OBJECT

    private:
    Window()
    {
    QTextEdit* textEdit = new QTextEdit(this);
    this->setCentralWidget(textEdit);
    textEdit->setPlainText(
    "\n"
    " INITIALIZING\n"
    "\n"
    " ~-_ Steamin'\n"
    " -~ Hot\n"
    " c|
    | COFFEE\n"
    "");
    }

    public:
    static Window* instance()
    {
    static Window instance;
    return &instance;
    }
    };@

    EDIT
    Found a way to solve it! If I swap my lazy singleton to an ordinary singleton, things will work out.
    @Window* instance()
    {
    // NOTE: "s_instance" is a static member variable
    if(!s_instance)
    s_instance = new Window();

    return s_instance;
    }@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      How do you know you get a huge memory leak? (Do you mean on termination of your program? Technically that's not a memory leak, more of a moral thing...)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Adelost
        wrote on last edited by
        #3

        I tested it with Qt 5.0.2 as well, same result.

        [quote author="DerManu" date="1368224963"](Do you mean on termination of your program?)[/quote] Yes during termination. Still feels quite bad thought.

        My main looks like this:
        @int main(int argc, char *argv[])
        {
        // Detect memory leaks
        #if defined(DEBUG) | defined(_DEBUG)
        _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
        #endif

        // Create window
        QApplication a(argc, argv);
        Window* w = Window::instance();
        w->show();
        
        return a.exec();
        

        }@

        Haven't counted, but the memleak seems rather huge.
        !http://i.imgur.com/gEnd0Tb.png(Memory leak QTextEdit.)!

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DerManu
          wrote on last edited by
          #4

          [quote author="Adelost" date="1368225736"]Yes during termination. Still feels quite bad thought.[/quote] Yes, you should feel a little dirty, that's okay.

          Just a guess: Delete the singleton memory in a controlled fashion before QApplication goes out of scope. Or try hard to get rid of the singleton, in case you haven't done that yet.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Adelost
            wrote on last edited by
            #5

            QTextEdit really doesn't like singletons it seems. Even if I delete the Widget manually still memleak. Gives me the creeps.
            @Window()
            {
            QTextEdit* textEdit = new QTextEdit();
            delete textEdit; // Deletes widget

            // Still memleak, hmm...
            }@

            The only widget affected so far seem to be QTextEdit and QPlainTextEdit, ordinary widgets seems to work fine.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Adelost
              wrote on last edited by
              #6

              Found a way to solve it! If I swap my lazy singleton to an ordinary singleton, things will work out.

              @Window* instance()
              {
              if(!s_instance)
              s_instance = new Window();

              return s_instance;
              }@

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved