Skip to content

Commit

Permalink
Do not leak each created KviInputEditorSpellCheckerBlock
Browse files Browse the repository at this point in the history
Objects allocated by splitTextIntoSpellCheckerBlocks into buffer vector are
never freed. Fix this by turning vector of pointers into vector of objects.
  • Loading branch information
i-garrison committed Nov 11, 2023
1 parent b53073b commit 6ed340c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
36 changes: 15 additions & 21 deletions src/kvirc/ui/KviInputEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,10 @@ bool KviInputEditor::checkWordSpelling(const QString & szWord)
#define ADD_SPELLCHECKER_BLOCK(_lBuffer, _szText, _iStart, _bSpellCheckable, _bCorrect) \
do \
{ \
KviInputEditorSpellCheckerBlock * pBlock = new KviInputEditorSpellCheckerBlock(); \
pBlock->szText = _szText; \
pBlock->iStart = _iStart; \
pBlock->iLength = pBlock->szText.length(); \
pBlock->bSpellCheckable = _bSpellCheckable; \
pBlock->bCorrect = _bCorrect; \
_lBuffer.push_back(pBlock); \
_lBuffer.push_back({_szText, _iStart, _szText.length(), _bSpellCheckable, _bCorrect}); \
} while(0)

void KviInputEditor::splitTextIntoSpellCheckerBlocks(const QString & szText, std::vector<KviInputEditorSpellCheckerBlock *> & lBuffer)
void KviInputEditor::splitTextIntoSpellCheckerBlocks(const QString & szText, std::vector<KviInputEditorSpellCheckerBlock> & lBuffer)
{
#ifdef COMPILE_ENCHANT_SUPPORT
if(szText.isEmpty())
Expand Down Expand Up @@ -516,7 +510,7 @@ void KviInputEditor::rebuildTextBlocks()
qDeleteAll(m_p->lTextBlocks);
m_p->lTextBlocks.clear();

std::vector<KviInputEditorSpellCheckerBlock *> lSpellCheckerBlocks;
std::vector<KviInputEditorSpellCheckerBlock> lSpellCheckerBlocks;

#ifdef COMPILE_ENCHANT_SUPPORT
splitTextIntoSpellCheckerBlocks(m_szTextBuffer, lSpellCheckerBlocks);
Expand All @@ -542,16 +536,16 @@ void KviInputEditor::rebuildTextBlocks()

KviInputEditorTextBlock * pBlock;

for(auto spb : lSpellCheckerBlocks)
for(auto& spb : lSpellCheckerBlocks)
{
if(spb->bSpellCheckable && !spb->bCorrect)
if(spb.bSpellCheckable && !spb.bCorrect)
uFlags |= KviInputEditorTextBlock::IsSpellingMistake;
else
uFlags &= ~KviInputEditorTextBlock::IsSpellingMistake;

const QChar * pBuffer = spb->szText.unicode();
const QChar * pBuffer = spb.szText.unicode();
const QChar * p = pBuffer;
const QChar * e = p + spb->szText.length();
const QChar * e = p + spb.szText.length();

while(p < e)
{
Expand Down Expand Up @@ -623,7 +617,7 @@ void KviInputEditor::rebuildTextBlocks()
{
unsigned char uFore;
unsigned char uBack;
/* int iNextChar = */ KviControlCodes::getUnicodeColorBytes(spb->szText, p - spb->szText.unicode(), &uFore, &uBack);
/* int iNextChar = */ KviControlCodes::getUnicodeColorBytes(spb.szText, p - spb.szText.unicode(), &uFore, &uBack);
if(uFore != KviControlCodes::NoChange)
{
uCurFore = uFore;
Expand Down Expand Up @@ -1143,7 +1137,7 @@ void KviInputEditor::showContextPopup(const QPoint & pos)
#ifdef COMPILE_ENCHANT_SUPPORT
// check if the cursor is in a spell-checkable block

std::vector<KviInputEditorSpellCheckerBlock *> lBuffer;
std::vector<KviInputEditorSpellCheckerBlock> lBuffer;
splitTextIntoSpellCheckerBlocks(m_szTextBuffer, lBuffer);

m_iSpellCheckPosition = qMin(charIndexFromXPosition(pos.x()), m_szTextBuffer.length());
Expand Down Expand Up @@ -1191,15 +1185,15 @@ void KviInputEditor::showContextPopupHere()
showContextPopup(QPoint(fXPos, iBottom));
}

KviInputEditorSpellCheckerBlock * KviInputEditor::findSpellCheckerBlockAtCursor(std::vector<KviInputEditorSpellCheckerBlock *> & lBlocks)
KviInputEditorSpellCheckerBlock * KviInputEditor::findSpellCheckerBlockAtCursor(std::vector<KviInputEditorSpellCheckerBlock> & lBlocks)
{
KviInputEditorSpellCheckerBlock * pCurrentBlock = nullptr;

for(auto pBlock : lBlocks)
for(auto& block : lBlocks)
{
if(m_iSpellCheckPosition <= (pBlock->iStart + pBlock->iLength))
if(m_iSpellCheckPosition <= (block.iStart + block.iLength))
{
pCurrentBlock = pBlock;
pCurrentBlock = &block;
break;
}
}
Expand All @@ -1222,7 +1216,7 @@ void KviInputEditor::fillSpellCheckerCorrectionsPopup()
#ifdef COMPILE_ENCHANT_SUPPORT
// check if the cursor is in a spellcheckable block

std::vector<KviInputEditorSpellCheckerBlock *> lBuffer;
std::vector<KviInputEditorSpellCheckerBlock> lBuffer;
splitTextIntoSpellCheckerBlocks(m_szTextBuffer, lBuffer);

KviInputEditorSpellCheckerBlock * pCurrentBlock = findSpellCheckerBlockAtCursor(lBuffer);
Expand Down Expand Up @@ -1305,7 +1299,7 @@ void KviInputEditor::spellCheckerPopupCorrectionActionTriggered()
if(szWord.isEmpty())
return;

std::vector<KviInputEditorSpellCheckerBlock *> lBuffer;
std::vector<KviInputEditorSpellCheckerBlock> lBuffer;
splitTextIntoSpellCheckerBlocks(m_szTextBuffer, lBuffer);

KviInputEditorSpellCheckerBlock * pCurrentBlock = findSpellCheckerBlockAtCursor(lBuffer);
Expand Down
8 changes: 4 additions & 4 deletions src/kvirc/ui/KviInputEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class KviInputEditorSpellCheckerBlock
{
public:
QString szText;
int iStart;
int iLength;
qsizetype iStart;
qsizetype iLength;
bool bSpellCheckable;
bool bCorrect;
};
Expand Down Expand Up @@ -975,8 +975,8 @@ private slots:
QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
void paintEvent(QPaintEvent * e) override;
bool checkWordSpelling(const QString & szWord);
void splitTextIntoSpellCheckerBlocks(const QString & szText, std::vector<KviInputEditorSpellCheckerBlock *> & lBuffer);
KviInputEditorSpellCheckerBlock * findSpellCheckerBlockAtCursor(std::vector<KviInputEditorSpellCheckerBlock *> & lBlocks);
void splitTextIntoSpellCheckerBlocks(const QString & szText, std::vector<KviInputEditorSpellCheckerBlock> & lBuffer);
KviInputEditorSpellCheckerBlock * findSpellCheckerBlockAtCursor(std::vector<KviInputEditorSpellCheckerBlock> & lBlocks);
void fillSpellCheckerCorrectionsPopup();

void rebuildTextBlocks();
Expand Down

0 comments on commit 6ed340c

Please sign in to comment.