Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Customcompleter EXC_BAD_ACCESS crash

    General and Desktop
    3
    4
    944
    Loading More Posts
    • 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.
    • S
      schaafsma.alex last edited by

      hello,

      im trying to make a simple code editor (just completer, codeeditor and syntaxhighlightin examples in one application).

      now i am getting a EXC_BAD_ACCESS could not access memory error and the app crashes.

      here is the the relevant code

      header:
      @public:
      void setCompleter(QCompleter *completer);
      private:
      QCompleter *c;@

      source:
      @void ACodeEditor::setCompleter(QCompleter *completer)
      {
      if(c) {
      //here is the error!!
      QObject::disconnect(c , 0,
      this, 0);
      }

      c = completer;
      
      if(!c) return;
      
      c->setWidget(this);
      c->setCompletionMode(QCompleter::PopupCompletion);
      c->setCaseSensitivity(Qt::CaseInsensitive);
      QObject::connect(c   , SIGNAL(activated(QString)),
                       this, SLOT  (insertCompletion(QString)));
      

      }@

      for a temporary solution i out-commented line 3 till 6 and it works fine.
      don't really know the consequence is for not disconnecting objects.

      does any one know how to fix this?

      thanks.

      1 Reply Last reply Reply Quote 0
      • B
        bodzio131 last edited by

        Do you use uninitialized 'c' field inside ACodeEditor::setCompleter?

        1 Reply Last reply Reply Quote 0
        • Chris Kawa
          Chris Kawa Moderators last edited by

          Do you initialize c to 0 anywhere? If not, "if( c )" will pass(as c is random) and a disconnect on this random c pointer will cause crash.

          Also, you don't seem to delete the old c, so it's a memory leak. You should call c->deleteLater() before overwriting it with new completer.
          As for the disconnecting - it's automatic when the emitting or receiving object is destroyed, so you don't really need it here. Just delete the old completer(via deleteLater()), or all those leaking pointers will still hold connections until the program ends.

          1 Reply Last reply Reply Quote 0
          • S
            schaafsma.alex last edited by

            thanks Krzysztof Kawa.

            i changed it in the constructor i have set c = 0 and replaced the diconnect with c->deletelater().
            now it works.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post