Closed perdrix52 closed 1 year ago
I propose to add the following declaration in MimeMessage to call on the memory manager present in QObject derivated classes:
class SMTP_MIME_EXPORT MimeMessage : public QObject
{
Q_OBJECT
public:
That won't resolve the leak unless you change the ctor to require a QObject as parent and any items you new in MimeMessage will also need to have MimeMessage as their parent.
You could default to a nullptr as parent but that would continue to leak the memory
Have you studied the lifecycle of QObject? Members of QObject instances are deleted when you destroy the instance.
A much simpler solution might be to change the definition of content in the class from:
MimePart *content;
to
std::unique_ptr<MimePart> content;
Yes I have BUT ONLY IF they are Children of another QOBJECT
To clarify: Qt uses parent-child relationships to manage memory. If you provide the QTcpSocket object with a parent when you create it, the parent will take care of cleaning it up. The parent can be, for example, the GUI window that uses the socket. Once the window dies (i.e. is closed) the socket dies.
You can do without the parent but then indeed you have to delete the object manually.
A much simpler solution might be to change the definition of content in the class from:
MimePart *content;
to
std::unique_ptr<MimePart> content;
Smart pointers require at least C++11. Qt already had memory managed pointers before, plus QObjects have access to a static memory manager.
Fixed the memory leak in https://github.com/bluetiger9/SmtpClient-for-Qt/commit/73f1bcdd756022f9d87987762d900ffb6d29afcb, by adding a delete
when createAutoMimeContent == true
.
MimeMessage::MimeMessage(bool createAutoMimeContent) : hEncoding(MimePart::_8Bit) { if (createAutoMimeContent) this->content = new MimeMultiPart(); }
The default ctor news a MimeMultiPart object that is never deleted.
So when I code e.g.:
a leak of 248 bytes is created