Bug 73692 - possible memory leak in View Source
Summary: possible memory leak in View Source
Status: RESOLVED FIXED
Alias: None
Product: kmail
Classification: Applications
Component: GUI (show other bugs)
Version: 1.6
Platform: unspecified Linux
: NOR major
Target Milestone: ---
Assignee: kdepim bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-01-28 17:36 UTC by Ingo Klöcker
Modified: 2007-09-14 12:17 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ingo Klöcker 2004-01-28 17:36:36 UTC
Version:           1.6 (using KDE 3.2.0, compiled sources)
Compiler:          gcc version 3.3.1 (SuSE Linux)
OS:          Linux (i686) release 2.4.21-166-default

- Selected a message with a large attachment (message size approx. 4.1 MB). Memory usage jumped from ~32.6 to ~45.3 MB.
- After selecting another message (small, no attachment) memory usage goes down again. So far so good.

- Selected a message with a large attachment (message size approx. 4.1 MB). Memory usage jumped from ~32.6 to ~45.3 MB.
- Did "View Source". Memory usage jumped to ~120 (!!!) MB.
- After closing the "View Source" window memory usage goes down to ~111 MB.
- After selecting another message (small, no attachment) memory usage goes down to ~98 MB.
- Reselected the message with the large attachment. Memory usage stayed at ~98 MB. It seems an implicitely shared copy of this message is reused.
- Again "View Source" => ~127 MB
- Close "View Source" window => ~127 MB (i.e. no change)
- Select another message => ~127 MB (i.e. still no change)

Todo:
- Check for memory leak.
- Decrease memory usage.
Comment 1 Till Adam 2004-02-01 12:16:07 UTC
Subject: kdepim/kmail

CVS commit by tilladam: 

Fix memory usage problems with "view source" by:

o not leaking the text highlighter
o checking the mail size and going to Qt::LogText mode for mails > 500kb
  because the rich text mode is very inefficient and uses about 15 bytes
  per character. This loses the boldification of headers before the : for
  large mails, but I guess that's preferrable to using massive amounts of
  memory.

Thanks to tronical for Qt consulting. :)

CCMAIL: 73692-done@bugs.kde.org


  M +26 -19    mailsourceviewer.cpp   1.6
  M +15 -0     mailsourceviewer.h   1.5


--- kdepim/kmail/mailsourceviewer.cpp  #1.5:1.6
@@ -41,15 +41,8 @@
 #include <qregexp.h>
 #include <qaccel.h>
-#include <qsyntaxhighlighter.h>
 
 namespace KMail {
 
-class MailSourceHighlighter : public QSyntaxHighlighter
-{
-public:
-  MailSourceHighlighter( QTextEdit* edit )
-    : QSyntaxHighlighter( edit )
-  {}
-  int highlightParagraph( const QString& text, int ) {
+int MailSourceHighlighter::highlightParagraph( const QString& text, int ) {
     QRegExp regexp( "^([\\w-]+:\\s)" );
     if( regexp.search( text ) != -1 ) {
@@ -59,18 +52,32 @@ public:
     }
     return 0;
-  }
-};
+}
 
 MailSourceViewer::MailSourceViewer( QWidget *parent, const char *name )
-  : KTextBrowser( parent, name )
+  : KTextBrowser( parent, name ), mSourceHighLighter( 0 )
 {
   setWFlags( WDestructiveClose );
   QAccel *accel = new QAccel( this, "browser close-accel" );
   accel->connectItem( accel->insertItem( Qt::Key_Escape ), this , SLOT( close() ));
-  setTextFormat( Qt::PlainText );
   setWordWrap( KTextBrowser::NoWrap );
-  new MailSourceHighlighter( this );
   KWin::setIcons(winId(), kapp->icon(), kapp->miniIcon());
 }
 
+MailSourceViewer::~MailSourceViewer()
+{
+   if ( mSourceHighLighter != 0 )
+      delete mSourceHighLighter;
+}
+
+void MailSourceViewer::setText( const QString& text )
+{
+   if ( text.length() > 500000 ) {
+     setTextFormat( Qt::LogText );
+   } else {
+     setTextFormat( Qt::PlainText );
+     mSourceHighLighter = new MailSourceHighlighter( this );
+   }
+   KTextBrowser::setText( text );
+}
+
 }

--- kdepim/kmail/mailsourceviewer.h  #1.4:1.5
@@ -34,4 +34,5 @@
 
 #include <ktextbrowser.h>
+#include <qsyntaxhighlighter.h>
 
 
@@ -46,8 +47,22 @@
 namespace KMail {
 
+class MailSourceHighlighter : public QSyntaxHighlighter
+{
+public:
+  MailSourceHighlighter( QTextEdit* edit )
+    : QSyntaxHighlighter( edit )
+  {}
+  int highlightParagraph( const QString& text, int ); 
+};
+
+
 class MailSourceViewer : public KTextBrowser
 {
 public:
   MailSourceViewer( QWidget *parent = 0, const char *name = 0 );
+  ~MailSourceViewer();
+  void setText( const QString& text );
+private:
+  MailSourceHighlighter *mSourceHighLighter;
 };