Customcompleter EXC_BAD_ACCESS crash
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
void setCompleter(QCompleter *completer);
@void ACodeEditor::setCompleter(QCompleter *completer)
//here is the error!!
QObject::disconnect(c , 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?
Do you use uninitialized 'c' field inside ACodeEditor::setCompleter?
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.