Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed unnecessary transfer of the ownership before release which returns the pointer itself #11726

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/track/taglib/trackmetadata_id3v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,10 @@ void writeTextIdentificationFrame(
auto pFrame =
std::make_unique<TagLib::ID3v2::TextIdentificationFrame>(id, stringType);
pFrame->setText(toTString(text));
pTag->addFrame(pFrame.get());
// Now that the plain pointer in pFrame is owned and managed by
// pTag we need to release the ownership to avoid double deletion!
pFrame.release();

// Release returns a pointer to the managed object and releases the ownership,
Copy link
Contributor

@uklotzde uklotzde Jul 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is obsolete after dropping the 2-step get-release pattern. Repeating the std definition of release() is pointless and confusing. Also applies to other locations in the code.

The 2-step pattern is required for exception safe C++ programming. But since any exception thrown by addFrame() is expected to be fatal a resource leak doesn't matter then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code has been written with purpose and the comment explained why. If you decide to take a simpler approach then delete the obsolete comment. I don't care, does not really matter. But please change it in a consistent way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resource leak reported by Coverity is obviously a false positive.

// the plain pointer in pFrame is owned and managed by pTag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this should be sufficient

Suggested change
// Release returns a pointer to the managed object and releases the ownership,
// the plain pointer in pFrame is owned and managed by pTag
// pTag takes the ownership of pFrame

pTag->addFrame(pFrame.release());
}
}

Expand Down Expand Up @@ -414,10 +414,10 @@ void writeUserTextIdentificationFrame(
std::make_unique<TagLib::ID3v2::UserTextIdentificationFrame>(stringType);
pFrame->setDescription(toTString(description));
pFrame->setText(toTString(text));
pTag->addFrame(pFrame.get());
// Now that the plain pointer in pFrame is owned and managed by
// pTag we need to release the ownership to avoid double deletion!
pFrame.release();

// Release returns a pointer to the managed object and releases the ownership,
// the plain pointer in pFrame is owned and managed by pTag
pTag->addFrame(pFrame.release());
}
}
}
Expand Down Expand Up @@ -488,10 +488,10 @@ void writeCommentsFrame(
std::make_unique<TagLib::ID3v2::CommentsFrame>(stringType);
pFrame->setDescription(toTString(description));
pFrame->setText(text);
pTag->addFrame(pFrame.get());
// Now that the plain pointer in pFrame is owned and managed by
// pTag we need to release the ownership to avoid double deletion!
pFrame.release();

// Release returns a pointer to the managed object and releases the ownership,
// the plain pointer in pFrame is owned and managed by pTag
pTag->addFrame(pFrame.release());
}
}
// Cleanup: Remove non-standard comment frames to avoid redundant and
Expand Down Expand Up @@ -576,10 +576,10 @@ void writeGeneralEncapsulatedObjectFrame(
pFrame->setDescription(toTString(description));
pFrame->setObject(toTByteVector(data));
pFrame->setMimeType(mimeType);
pTag->addFrame(pFrame.get());
// Now that the plain pointer in pFrame is owned and managed by
// pTag we need to release the ownership to avoid double deletion!
pFrame.release();

// Release returns a pointer to the managed object and releases the ownership,
// the plain pointer in pFrame is owned and managed by pTag
pTag->addFrame(pFrame.release());
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/util/db/dbconnectionpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ bool DbConnectionPool::createThreadLocalConnection() {
<< *pConnection;
return false; // abort
}
m_threadLocalConnections.setLocalData(pConnection.get()); // transfer ownership
pConnection.release(); // release ownership

// Release returns a pointer to the managed object and releases the ownership,
// the plain pointer in pConnection is owned and managed by m_threadLocalConnections
m_threadLocalConnections.setLocalData(pConnection.release());

DEBUG_ASSERT(m_threadLocalConnections.hasLocalData());
DEBUG_ASSERT(m_threadLocalConnections.localData());
kLogger.info()
Expand Down