From fa7b2dd651fc568c15e215633eacc7094fd4c052 Mon Sep 17 00:00:00 2001
From: Mladen Milinkovic <maxrd2@smoothware.net>
Date: Thu, 14 Oct 2021 23:08:07 +0200
Subject: [PATCH] Removed QSharedData instances from stack

---
 src/application_subtitle.cpp  | 42 +++++++++++++++++------------------
 src/formats/formatmanager.cpp |  8 +++----
 src/formats/inputformat.h     |  8 +++----
 src/tests/subtitletest.cpp    | 10 ++++-----
 src/tests/subtitletest.h      |  6 ++++-
 5 files changed, 39 insertions(+), 35 deletions(-)

diff --git a/src/application_subtitle.cpp b/src/application_subtitle.cpp
index 406e4a3a..a1d29fd1 100644
--- a/src/application_subtitle.cpp
+++ b/src/application_subtitle.cpp
@@ -459,10 +459,10 @@ Application::openSubtitleTr(const QUrl &url, bool warnClashingUrls)
 	if(!closeSubtitleTr())
 		return;
 
-	Subtitle subtitleTr;
+	QExplicitlySharedDataPointer<Subtitle> subtitleTr(new Subtitle());
 	QTextCodec *codec = codecForEncoding(KRecentFilesActionExt::encodingForUrl(url));
 
-	FormatManager::Status res = FormatManager::instance().readSubtitle(subtitleTr, false, url, &codec, &m_subtitleTrFormat);
+	FormatManager::Status res = FormatManager::instance().readSubtitle(*subtitleTr, false, url, &codec, &m_subtitleTrFormat);
 	if(res != FormatManager::SUCCESS) {
 		if(res == FormatManager::ERROR) {
 			KMessageBox::sorry(
@@ -474,7 +474,7 @@ Application::openSubtitleTr(const QUrl &url, bool warnClashingUrls)
 	}
 
 	m_subtitleTrUrl = url;
-	m_subtitle->setSecondaryData(subtitleTr, false);
+	m_subtitle->setSecondaryData(*subtitleTr, false);
 	processTranslationOpened(codec, m_subtitleTrFormat);
 }
 
@@ -484,10 +484,10 @@ Application::reopenSubtitleTrWithCodec(QTextCodec *codec)
 	if(m_subtitleTrUrl.isEmpty())
 		return;
 
-	Subtitle subtitleTr;
+	QExplicitlySharedDataPointer<Subtitle> subtitleTr(new Subtitle());
 	QString subtitleTrFormat;
 
-	FormatManager::Status res = FormatManager::instance().readSubtitle(subtitleTr, false, m_subtitleTrUrl, &codec, &subtitleTrFormat);
+	FormatManager::Status res = FormatManager::instance().readSubtitle(*subtitleTr, false, m_subtitleTrUrl, &codec, &subtitleTrFormat);
 	if(res != FormatManager::SUCCESS) {
 		if(res == FormatManager::ERROR) {
 			KMessageBox::sorry(
@@ -498,7 +498,7 @@ Application::reopenSubtitleTrWithCodec(QTextCodec *codec)
 		return;
 	}
 
-	m_subtitle->setSecondaryData(subtitleTr, false);
+	m_subtitle->setSecondaryData(*subtitleTr, false);
 	processTranslationOpened(codec, m_subtitleTrFormat);
 }
 
@@ -686,18 +686,18 @@ Application::joinSubtitles()
 	static JoinSubtitlesDialog *dlg = new JoinSubtitlesDialog(m_mainWindow);
 
 	if(dlg->exec() == QDialog::Accepted) {
-		Subtitle secondSubtitle;
+		QExplicitlySharedDataPointer<Subtitle> secondSubtitle(new Subtitle());
 
 		const QUrl url = dlg->subtitleUrl();
 		QTextCodec *codec = codecForEncoding(KRecentFilesActionExt::encodingForUrl(url));
 		const bool primary = dlg->selectedTextsTarget() != Secondary;
 
-		FormatManager::Status res = FormatManager::instance().readSubtitle(secondSubtitle, primary, url, &codec, nullptr);
+		FormatManager::Status res = FormatManager::instance().readSubtitle(*secondSubtitle, primary, url, &codec, nullptr);
 		if(res == FormatManager::SUCCESS) {
 			if(dlg->selectedTextsTarget() == Both)
-				secondSubtitle.setSecondaryData(secondSubtitle, true);
+				secondSubtitle->setSecondaryData(*secondSubtitle, true);
 
-			m_subtitle->appendSubtitle(secondSubtitle, dlg->shiftTime().toMillis());
+			m_subtitle->appendSubtitle(*secondSubtitle, dlg->shiftTime().toMillis());
 		} else {
 			KMessageBox::sorry(m_mainWindow, i18n("Could not read the subtitle file to append."));
 		}
@@ -712,15 +712,15 @@ Application::splitSubtitle()
 	if(dlg->exec() != QDialog::Accepted)
 		return;
 
-	Subtitle newSubtitle;
-	m_subtitle->splitSubtitle(newSubtitle, dlg->splitTime().toMillis(), dlg->shiftNewSubtitle());
-	if(!newSubtitle.linesCount()) {
+	QExplicitlySharedDataPointer<Subtitle> newSubtitle(new Subtitle());
+	m_subtitle->splitSubtitle(*newSubtitle, dlg->splitTime().toMillis(), dlg->shiftNewSubtitle());
+	if(!newSubtitle->linesCount()) {
 		KMessageBox::information(m_mainWindow, i18n("The specified time does not split the subtitles."));
 		return;
 	}
 
 	QUrl splitUrl = saveSplitSubtitle(
-		newSubtitle,
+		*newSubtitle,
 		m_subtitleUrl,
 		m_subtitleEncoding,
 		m_subtitleFormat,
@@ -734,7 +734,7 @@ Application::splitSubtitle()
 
 	QUrl splitTrUrl;
 	if(m_translationMode) {
-		splitTrUrl = saveSplitSubtitle(newSubtitle, m_subtitleTrUrl, m_subtitleTrEncoding, m_subtitleTrFormat, false);
+		splitTrUrl = saveSplitSubtitle(*newSubtitle, m_subtitleTrUrl, m_subtitleTrEncoding, m_subtitleTrFormat, false);
 
 		if(splitTrUrl.path().isEmpty()) {
 			// there was an error saving the split part, undo the splitting of m_subtitle
@@ -761,21 +761,21 @@ Application::syncWithSubtitle()
 	static SyncSubtitlesDialog *dlg = new SyncSubtitlesDialog(m_mainWindow);
 
 	if(dlg->exec() == QDialog::Accepted) {
-		Subtitle referenceSubtitle;
+		QExplicitlySharedDataPointer<Subtitle> referenceSubtitle(new Subtitle());
 
 		const QUrl url = dlg->subtitleUrl();
 
-		FormatManager::Status res = FormatManager::instance().readSubtitle(referenceSubtitle, true, url, nullptr, nullptr);
+		FormatManager::Status res = FormatManager::instance().readSubtitle(*referenceSubtitle, true, url, nullptr, nullptr);
 		if(res == FormatManager::SUCCESS) {
 			if(dlg->adjustToReferenceSubtitle()) {
-				if(referenceSubtitle.linesCount() <= 1)
+				if(referenceSubtitle->linesCount() <= 1)
 					KMessageBox::sorry(m_mainWindow, i18n("The reference subtitle must have more than one line to proceed."));
 				else
 					m_subtitle->adjustLines(Range::full(),
-											referenceSubtitle.firstLine()->showTime().toMillis(),
-											referenceSubtitle.lastLine()->showTime().toMillis());
+											referenceSubtitle->firstLine()->showTime().toMillis(),
+											referenceSubtitle->lastLine()->showTime().toMillis());
 			} else /*if(dlg->synchronizeToReferenceTimes())*/ {
-				m_subtitle->syncWithSubtitle(referenceSubtitle);
+				m_subtitle->syncWithSubtitle(*referenceSubtitle);
 			}
 		} else {
 			KMessageBox::sorry(m_mainWindow, i18n("Could not parse the reference subtitle file."));
diff --git a/src/formats/formatmanager.cpp b/src/formats/formatmanager.cpp
index 551c1d3e..2c7479f2 100644
--- a/src/formats/formatmanager.cpp
+++ b/src/formats/formatmanager.cpp
@@ -154,8 +154,8 @@ FormatManager::readBinary(Subtitle &subtitle, const QUrl &url, bool primary,
 						  QTextCodec **codec, QString *formatName) const
 {
 	foreach(InputFormat *format, m_inputFormats) {
-		Subtitle newSubtitle;
-		Status res = format->readBinary(newSubtitle, url);
+		QExplicitlySharedDataPointer<Subtitle> newSubtitle(new Subtitle());
+		Status res = format->readBinary(*newSubtitle, url);
 		if(res == ERROR)
 			continue;
 		if(res == SUCCESS) {
@@ -163,9 +163,9 @@ FormatManager::readBinary(Subtitle &subtitle, const QUrl &url, bool primary,
 				*formatName = format->name();
 			*codec = KCharsets::charsets()->codecForName(SCConfig::defaultSubtitlesEncoding());
 			if(primary)
-				subtitle.setPrimaryData(newSubtitle, true);
+				subtitle.setPrimaryData(*newSubtitle, true);
 			else
-				subtitle.setSecondaryData(newSubtitle, true);
+				subtitle.setSecondaryData(*newSubtitle, true);
 		}
 		return res;
 	}
diff --git a/src/formats/inputformat.h b/src/formats/inputformat.h
index d7a290e6..99d945f5 100644
--- a/src/formats/inputformat.h
+++ b/src/formats/inputformat.h
@@ -17,15 +17,15 @@ class InputFormat : public Format
 public:
 	bool readSubtitle(Subtitle &subtitle, bool primary, const QString &data) const
 	{
-		Subtitle newSubtitle;
+		QExplicitlySharedDataPointer<Subtitle> newSubtitle(new Subtitle());
 
-		if(!parseSubtitles(newSubtitle, data))
+		if(!parseSubtitles(*newSubtitle, data))
 			return false;
 
 		if(primary)
-			subtitle.setPrimaryData(newSubtitle, true);
+			subtitle.setPrimaryData(*newSubtitle, true);
 		else
-			subtitle.setSecondaryData(newSubtitle, true);
+			subtitle.setSecondaryData(*newSubtitle, true);
 
 		return true;
 	}
diff --git a/src/tests/subtitletest.cpp b/src/tests/subtitletest.cpp
index 3fdd4415..7612567c 100644
--- a/src/tests/subtitletest.cpp
+++ b/src/tests/subtitletest.cpp
@@ -33,17 +33,17 @@ SubtitleTest::testSort()
 {
 	QFETCH(QVector<int>, lines);
 
-	sub.removeLines(RangeList(Range::full()), SubtitleTarget::Both);
+	sub->removeLines(RangeList(Range::full()), SubtitleTarget::Both);
 
 	for(int n: lines) {
 		SubtitleLine *l = new SubtitleLine(n * 1000, n * 1000 + 500);
 		l->primaryDoc()->setPlainText(QString::number(n));
-		sub.insertLine(l);
+		sub->insertLine(l);
 	}
 
-	QVERIFY(sub.count() == lines.size());
-	for(int i = 0; i < sub.count(); i++)
-		QVERIFY(qRound(sub.at(i)->showTime().toSeconds()) == i + 1);
+	QVERIFY(sub->count() == lines.size());
+	for(int i = 0; i < sub->count(); i++)
+		QVERIFY(qRound(sub->at(i)->showTime().toSeconds()) == i + 1);
 }
 
 #define QCoreApplication Application
diff --git a/src/tests/subtitletest.h b/src/tests/subtitletest.h
index b8aec1ed..8efd59ea 100644
--- a/src/tests/subtitletest.h
+++ b/src/tests/subtitletest.h
@@ -15,11 +15,15 @@ class SubtitleTest : public QObject
 {
 	Q_OBJECT
 
-	SubtitleComposer::Subtitle sub;
+public:
+	SubtitleTest() : sub(new SubtitleComposer::Subtitle) {}
 
 private slots:
 	void testSort_data();
 	void testSort();
+
+private:
+	QExplicitlySharedDataPointer<SubtitleComposer::Subtitle> sub;
 };
 
 #endif // SUBTITLESORTTEST_H
-- 
GitLab

