Browse Source

fixed biggest memleaks \o/

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/slim/trunk@110 7c53e7cc-98ea-0310-8f1f-a0b24da60408
sip 17 years ago
parent
commit
6674fc28c6
6 changed files with 61 additions and 45 deletions
  1. 43 42
      app.cpp
  2. 3 1
      app.h
  3. 3 0
      cfg.cpp
  4. 1 0
      cfg.h
  5. 5 2
      image.cpp
  6. 6 0
      panel.cpp

+ 43 - 42
app.cpp

@@ -24,7 +24,6 @@
 #include <algorithm>
 #include <algorithm>
 #include "app.h"
 #include "app.h"
 #include "numlock.h"
 #include "numlock.h"
-#include "image.h"
 
 
 
 
 extern App* LoginApp;
 extern App* LoginApp;
@@ -113,7 +112,8 @@ void App::Run() {
 #endif
 #endif
 
 
     // Read configuration and theme
     // Read configuration and theme
-    cfg.readConf(CFGFILE);
+    cfg = new Cfg;
+    cfg->readConf(CFGFILE);
     string themebase = "";
     string themebase = "";
     string themefile = "";
     string themefile = "";
     string themedir = "";
     string themedir = "";
@@ -122,7 +122,7 @@ void App::Run() {
         themeName = testtheme;
         themeName = testtheme;
     } else {
     } else {
         themebase = string(THEMESDIR) + "/";
         themebase = string(THEMESDIR) + "/";
-        themeName = cfg.getOption("current_theme");
+        themeName = cfg->getOption("current_theme");
         string::size_type pos;
         string::size_type pos;
         if ((pos = themeName.find(",")) != string::npos) {
         if ((pos = themeName.find(",")) != string::npos) {
             // input is a set
             // input is a set
@@ -137,7 +137,7 @@ void App::Run() {
     while (!loaded) {
     while (!loaded) {
         themedir =  themebase + themeName;
         themedir =  themebase + themeName;
         themefile = themedir + THEMESFILE;
         themefile = themedir + THEMESFILE;
-        if (!cfg.readConf(themefile)) {
+        if (!cfg->readConf(themefile)) {
             if (themeName == "default") {
             if (themeName == "default") {
                 cerr << APPNAME << ": Failed to open default theme file "
                 cerr << APPNAME << ": Failed to open default theme file "
                      << themefile << endl;
                      << themefile << endl;
@@ -170,7 +170,7 @@ void App::Run() {
 #ifndef XNEST_DEBUG
 #ifndef XNEST_DEBUG
         OpenLog();
         OpenLog();
 		
 		
-		if (cfg.getOption("daemon") == "yes") {
+		if (cfg->getOption("daemon") == "yes") {
 			daemonmode = true;
 			daemonmode = true;
 		}
 		}
 
 
@@ -216,7 +216,7 @@ void App::Run() {
     HideCursor();
     HideCursor();
 
 
     // Create panel
     // Create panel
-    LoginPanel = new Panel(Dpy, Scr, Root, &cfg, themedir);
+    LoginPanel = new Panel(Dpy, Scr, Root, cfg, themedir);
 
 
     // Start looping
     // Start looping
     XEvent event;
     XEvent event;
@@ -241,8 +241,8 @@ void App::Run() {
 
 
         Action = WAIT;
         Action = WAIT;
         LoginPanel->GetInput()->Reset();
         LoginPanel->GetInput()->Reset();
-        if (firstloop && cfg.getOption("default_user") != "") {
-            LoginPanel->GetInput()->SetName(cfg.getOption("default_user") );
+        if (firstloop && cfg->getOption("default_user") != "") {
+            LoginPanel->GetInput()->SetName(cfg->getOption("default_user") );
             firstloop = false;
             firstloop = false;
         }
         }
 
 
@@ -294,7 +294,7 @@ int App::GetServerPID() {
 
 
 // Hide the cursor
 // Hide the cursor
 void App::HideCursor() {
 void App::HideCursor() {
-	if (cfg.getOption("hidecursor") == "true") {
+	if (cfg->getOption("hidecursor") == "true") {
 		XColor		    black;
 		XColor		    black;
 		char		    cursordata[1];
 		char		    cursordata[1];
 		Pixmap		    cursorpixmap;
 		Pixmap		    cursorpixmap;
@@ -321,12 +321,12 @@ void App::Login() {
     pid = fork();
     pid = fork();
     if(pid == 0) {
     if(pid == 0) {
         // Login process starts here
         // Login process starts here
-        SwitchUser Su(pw, &cfg, DisplayName);
+        SwitchUser Su(pw, cfg, DisplayName);
         string session = LoginPanel->getSession();
         string session = LoginPanel->getSession();
-        string loginCommand = cfg.getOption("login_cmd");
+        string loginCommand = cfg->getOption("login_cmd");
         replaceVariables(loginCommand, SESSION_VAR, session);
         replaceVariables(loginCommand, SESSION_VAR, session);
         replaceVariables(loginCommand, THEME_VAR, themeName);
         replaceVariables(loginCommand, THEME_VAR, themeName);
-        string sessStart = cfg.getOption("sessionstart_cmd");
+        string sessStart = cfg->getOption("sessionstart_cmd");
         if (sessStart != "") {
         if (sessStart != "") {
             replaceVariables(sessStart, USER_VAR, pw->pw_name);
             replaceVariables(sessStart, USER_VAR, pw->pw_name);
             system(sessStart.c_str());
             system(sessStart.c_str());
@@ -348,7 +348,7 @@ void App::Login() {
     if (WIFEXITED(status) && WEXITSTATUS(status)) {
     if (WIFEXITED(status) && WEXITSTATUS(status)) {
         LoginPanel->Message("Failed to execute login command");
         LoginPanel->Message("Failed to execute login command");
     } else {
     } else {
-         string sessStop = cfg.getOption("sessionstop_cmd");
+         string sessStop = cfg->getOption("sessionstop_cmd");
          if (sessStop != "") {
          if (sessStop != "") {
             replaceVariables(sessStop, USER_VAR, pw->pw_name);
             replaceVariables(sessStop, USER_VAR, pw->pw_name);
             system(sessStop.c_str());
             system(sessStop.c_str());
@@ -383,13 +383,13 @@ void App::Reboot() {
     alarm(0);
     alarm(0);
 
 
     // Write message
     // Write message
-    LoginPanel->Message((char*)cfg.getOption("reboot_msg").c_str());
+    LoginPanel->Message((char*)cfg->getOption("reboot_msg").c_str());
     sleep(3);
     sleep(3);
 
 
     // Stop server and reboot
     // Stop server and reboot
     StopServer();
     StopServer();
     RemoveLock();
     RemoveLock();
-    system(cfg.getOption("reboot_cmd").c_str());
+    system(cfg->getOption("reboot_cmd").c_str());
     exit(OK_EXIT);
     exit(OK_EXIT);
 }
 }
 
 
@@ -399,19 +399,19 @@ void App::Halt() {
     alarm(0);
     alarm(0);
 
 
     // Write message
     // Write message
-    LoginPanel->Message((char*)cfg.getOption("shutdown_msg").c_str());
+    LoginPanel->Message((char*)cfg->getOption("shutdown_msg").c_str());
     sleep(3);
     sleep(3);
 
 
     // Stop server and halt
     // Stop server and halt
     StopServer();
     StopServer();
     RemoveLock();
     RemoveLock();
-    system(cfg.getOption("halt_cmd").c_str());
+    system(cfg->getOption("halt_cmd").c_str());
     exit(OK_EXIT);
     exit(OK_EXIT);
 }
 }
 
 
 void App::Suspend() {
 void App::Suspend() {
     sleep(1);
     sleep(1);
-    system(cfg.getOption("suspend_cmd").c_str());
+    system(cfg->getOption("suspend_cmd").c_str());
 }
 }
 
 
 
 
@@ -424,7 +424,7 @@ void App::Console() {
     int height = (XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)) - (posy * 2)) / fonty;
     int height = (XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)) - (posy * 2)) / fonty;
 
 
     // Execute console
     // Execute console
-    const char* cmd = cfg.getOption("console_cmd").c_str();
+    const char* cmd = cfg->getOption("console_cmd").c_str();
     char *tmp = new char[strlen(cmd) + 60];
     char *tmp = new char[strlen(cmd) + 60];
     sprintf(tmp, cmd, width, height, posx, posy, fontx, fonty);
     sprintf(tmp, cmd, width, height, posx, posy, fontx, fonty);
     system(tmp);
     system(tmp);
@@ -541,10 +541,10 @@ int App::StartServer() {
 
 
     static const int MAX_XSERVER_ARGS = 256;
     static const int MAX_XSERVER_ARGS = 256;
     static char* server[MAX_XSERVER_ARGS+2] = { NULL };
     static char* server[MAX_XSERVER_ARGS+2] = { NULL };
-    server[0] = (char *)cfg.getOption("default_xserver").c_str();
-    string argOption = cfg.getOption("xserver_arguments");
+    server[0] = (char *)cfg->getOption("default_xserver").c_str();
+    string argOption = cfg->getOption("xserver_arguments");
     /* Add mandatory -xauth option */
     /* Add mandatory -xauth option */
-    argOption = argOption + " -auth " + cfg.getOption("authfile");
+    argOption = argOption + " -auth " + cfg->getOption("authfile");
     char* args = new char[argOption.length()+2]; // NULL plus vt
     char* args = new char[argOption.length()+2]; // NULL plus vt
     strcpy(args, argOption.c_str());
     strcpy(args, argOption.c_str());
 
 
@@ -615,7 +615,7 @@ int App::StartServer() {
         break;
         break;
     }
     }
 
 
-    string numlock = cfg.getOption("numlock");
+    string numlock = cfg->getOption("numlock");
     if (numlock == "on") {
     if (numlock == "on") {
         NumLock::setOn();
         NumLock::setOn();
     } else if (numlock == "off") {
     } else if (numlock == "off") {
@@ -708,7 +708,7 @@ void App::blankScreen()
 void App::setBackground(const string& themedir) {
 void App::setBackground(const string& themedir) {
     string filename;
     string filename;
     filename = themedir + "/background.png";
     filename = themedir + "/background.png";
-    Image *image = new Image;
+    image = new Image;
     bool loaded = image->Read(filename.c_str());
     bool loaded = image->Read(filename.c_str());
     if (!loaded){ // try jpeg if png failed
     if (!loaded){ // try jpeg if png failed
         filename = "";
         filename = "";
@@ -716,18 +716,18 @@ void App::setBackground(const string& themedir) {
         loaded = image->Read(filename.c_str());
         loaded = image->Read(filename.c_str());
     }
     }
     if (loaded) {
     if (loaded) {
-        string bgstyle = cfg.getOption("background_style");
+        string bgstyle = cfg->getOption("background_style");
         if (bgstyle == "stretch") {
         if (bgstyle == "stretch") {
             image->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
             image->Resize(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
         } else if (bgstyle == "tile") {
         } else if (bgstyle == "tile") {
             image->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
             image->Tile(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)));
         } else if (bgstyle == "center") {
         } else if (bgstyle == "center") {
-    	    string hexvalue = cfg.getOption("background_color");
+    	    string hexvalue = cfg->getOption("background_color");
             hexvalue = hexvalue.substr(1,6);
             hexvalue = hexvalue.substr(1,6);
     	    image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
     	    image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
         			    hexvalue.c_str());
         			    hexvalue.c_str());
         } else { // plain color or error
         } else { // plain color or error
-    	    string hexvalue = cfg.getOption("background_color");
+    	    string hexvalue = cfg->getOption("background_color");
             hexvalue = hexvalue.substr(1,6);
             hexvalue = hexvalue.substr(1,6);
     	    image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
     	    image->Center(XWidthOfScreen(ScreenOfDisplay(Dpy, Scr)), XHeightOfScreen(ScreenOfDisplay(Dpy, Scr)),
         			    hexvalue.c_str());
         			    hexvalue.c_str());
@@ -736,18 +736,19 @@ void App::setBackground(const string& themedir) {
         XSetWindowBackgroundPixmap(Dpy, Root, p);
         XSetWindowBackgroundPixmap(Dpy, Root, p);
     }
     }
     XClearWindow(Dpy, Root);
     XClearWindow(Dpy, Root);
-
+    
     XFlush(Dpy);
     XFlush(Dpy);
+    delete image;
 }
 }
 
 
 // Check if there is a lockfile and a corresponding process
 // Check if there is a lockfile and a corresponding process
 void App::GetLock() {
 void App::GetLock() {
-    std::ifstream lockfile(cfg.getOption("lockfile").c_str());
+    std::ifstream lockfile(cfg->getOption("lockfile").c_str());
     if (!lockfile) {
     if (!lockfile) {
         // no lockfile present, create one
         // no lockfile present, create one
-        std::ofstream lockfile(cfg.getOption("lockfile").c_str(), ios_base::out);
+        std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
         if (!lockfile) {
         if (!lockfile) {
-            cerr << APPNAME << ": Could not create lock file: " << cfg.getOption("lockfile").c_str() << std::endl;
+            cerr << APPNAME << ": Could not create lock file: " << cfg->getOption("lockfile").c_str() << std::endl;
             exit(ERR_EXIT);
             exit(ERR_EXIT);
         }
         }
         lockfile << getpid() << std::endl;
         lockfile << getpid() << std::endl;
@@ -765,9 +766,9 @@ void App::GetLock() {
                 exit(0);
                 exit(0);
             } else {
             } else {
                 cerr << APPNAME << ": Stale lockfile found, removing it" << std::endl;
                 cerr << APPNAME << ": Stale lockfile found, removing it" << std::endl;
-                std::ofstream lockfile(cfg.getOption("lockfile").c_str(), ios_base::out);
+                std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
                 if (!lockfile) {
                 if (!lockfile) {
-                    cerr << APPNAME << ": Could not create new lock file: " << cfg.getOption("lockfile") << std::endl;
+                    cerr << APPNAME << ": Could not create new lock file: " << cfg->getOption("lockfile") << std::endl;
                     exit(ERR_EXIT);
                     exit(ERR_EXIT);
                 }
                 }
                 lockfile << getpid() << std::endl;
                 lockfile << getpid() << std::endl;
@@ -779,21 +780,21 @@ void App::GetLock() {
 
 
 // Remove lockfile and close logs
 // Remove lockfile and close logs
 void App::RemoveLock() {
 void App::RemoveLock() {
-    remove(cfg.getOption("lockfile").c_str());
+    remove(cfg->getOption("lockfile").c_str());
 }
 }
 
 
 // Redirect stdout and stderr to log file
 // Redirect stdout and stderr to log file
 void App::OpenLog() {
 void App::OpenLog() {
-    FILE *log = fopen (cfg.getOption("logfile").c_str(),"a");
+    FILE *log = fopen (cfg->getOption("logfile").c_str(),"a");
     if (!log) {
     if (!log) {
-        cerr <<  APPNAME << ": Could not accesss log file: " << cfg.getOption("logfile") << endl;
+        cerr <<  APPNAME << ": Could not accesss log file: " << cfg->getOption("logfile") << endl;
         RemoveLock();
         RemoveLock();
         exit(ERR_EXIT);
         exit(ERR_EXIT);
     }
     }
     fclose(log);
     fclose(log);
-    freopen (cfg.getOption("logfile").c_str(),"a",stdout);
+    freopen (cfg->getOption("logfile").c_str(),"a",stdout);
     setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
     setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
-    freopen (cfg.getOption("logfile").c_str(),"a",stderr);
+    freopen (cfg->getOption("logfile").c_str(),"a",stderr);
     setvbuf(stderr, NULL, _IONBF, BUFSIZ);
     setvbuf(stderr, NULL, _IONBF, BUFSIZ);
 }
 }
 
 
@@ -869,10 +870,10 @@ void App::CreateServerAuth() {
         }
         }
         mcookie[31] = digits[r];
         mcookie[31] = digits[r];
 	/* reinitialize auth file */
 	/* reinitialize auth file */
-	authfile = cfg.getOption("authfile");
+	authfile = cfg->getOption("authfile");
 	remove(authfile.c_str());
 	remove(authfile.c_str());
         putenv(StrConcat("XAUTHORITY=", authfile.c_str()));
         putenv(StrConcat("XAUTHORITY=", authfile.c_str()));
-        cmd = cfg.getOption("xauth_path") + " -q -f " + authfile + " add :0 . " + mcookie;
+        cmd = cfg->getOption("xauth_path") + " -q -f " + authfile + " add :0 . " + mcookie;
         system(cmd.c_str());
         system(cmd.c_str());
 }
 }
 
 
@@ -884,9 +885,9 @@ char* App::StrConcat(const char* str1, const char* str2) {
 }
 }
 
 
 void App::UpdatePid() {
 void App::UpdatePid() {
-    std::ofstream lockfile(cfg.getOption("lockfile").c_str(), ios_base::out);
+    std::ofstream lockfile(cfg->getOption("lockfile").c_str(), ios_base::out);
     if (!lockfile) {
     if (!lockfile) {
-	    cerr << APPNAME << ": Could not update lock file: " << cfg.getOption("lockfile").c_str() << std::endl;
+	    cerr << APPNAME << ": Could not update lock file: " << cfg->getOption("lockfile").c_str() << std::endl;
 	    exit(ERR_EXIT);
 	    exit(ERR_EXIT);
     }
     }
     lockfile << getpid() << std::endl;
     lockfile << getpid() << std::endl;

+ 3 - 1
app.h

@@ -22,6 +22,7 @@
 #include <iostream>
 #include <iostream>
 #include "panel.h"
 #include "panel.h"
 #include "cfg.h"
 #include "cfg.h"
+#include "image.h"
 
 
 class App {
 class App {
 public:
 public:
@@ -73,11 +74,12 @@ private:
     // Options
     // Options
     char* DispName;
     char* DispName;
 
 
-    Cfg cfg;
+    Cfg *cfg;
 
 
     Pixmap BackgroundPixmap;
     Pixmap BackgroundPixmap;
 
 
     void blankScreen();
     void blankScreen();
+    Image* image;
     void setBackground(const string& themedir);
     void setBackground(const string& themedir);
 	
 	
     bool daemonmode;
     bool daemonmode;

+ 3 - 0
cfg.cpp

@@ -104,6 +104,9 @@ Cfg::Cfg() {
 
 
 }
 }
 
 
+Cfg::~Cfg() {
+	options.clear();
+}
 /*
 /*
  * Creates the Cfg object and parses
  * Creates the Cfg object and parses
  * known options from the given configfile / themefile
  * known options from the given configfile / themefile

+ 1 - 0
cfg.h

@@ -28,6 +28,7 @@ class Cfg {
 
 
 public:
 public:
     Cfg();
     Cfg();
+    ~Cfg();
     bool readConf(string configfile);
     bool readConf(string configfile);
     string parseOption(string line, string option);
     string parseOption(string line, string option);
     const string& getError() const;
     const string& getError() const;

+ 5 - 2
image.cpp

@@ -353,8 +353,11 @@ void Image::Crop(const int x, const int y, const int w, const int h) {
     int y2 = y + h;
     int y2 = y + h;
     unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
     unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
     memset(new_rgb, 0, 3 * w * h);
     memset(new_rgb, 0, 3 * w * h);
-    unsigned char *new_alpha = (unsigned char *) malloc(w * h);
-    memset(new_alpha, 0, w * h);
+    unsigned char *new_alpha = NULL;
+    if (png_alpha != NULL) {
+        new_alpha = (unsigned char *) malloc(w * h);
+        memset(new_alpha, 0, w * h);
+    }
 
 
     int ipos = 0;
     int ipos = 0;
     int opos = 0;
     int opos = 0;

+ 6 - 0
panel.cpp

@@ -122,6 +122,7 @@ Panel::Panel(Display* dpy, int scr, Window root, Cfg* config,
 
 
     // Merge image into background
     // Merge image into background
     image->Merge(bg, X, Y);
     image->Merge(bg, X, Y);
+    delete bg;
     PanelPixmap = image->createPixmap(Dpy, Scr, Root);
     PanelPixmap = image->createPixmap(Dpy, Scr, Root);
 
 
     // Read (and substitute vars in) the welcome message
     // Read (and substitute vars in) the welcome message
@@ -138,6 +139,11 @@ Panel::~Panel() {
     XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &welcomecolor);
     XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &welcomecolor);
     XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &entercolor);
     XftColorFree (Dpy, DefaultVisual(Dpy, Scr), DefaultColormap(Dpy, Scr), &entercolor);
     XFreeGC(Dpy, TextGC);
     XFreeGC(Dpy, TextGC);
+    XftFontClose(Dpy, font);
+    XftFontClose(Dpy, msgfont);
+    XftFontClose(Dpy, introfont);
+    XftFontClose(Dpy, welcomefont);
+    XftFontClose(Dpy, enterfont);
     delete In;
     delete In;
     delete image;
     delete image;