Customcompleter EXC_BAD_ACCESS crash



  • 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.



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


  • Moderators

    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.



  • thanks Krzysztof Kawa.

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


Log in to reply
 

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