c++ - textbox filling memory -


i have win32 api application.
during debugging in visual studio found out applications memory usage increasing. i'm not experienced enough visual studio find out more.

but did find out when write in 1 of textbox elements memory usage increases more. think problem has zeromemory, because when make snapshot of heap @ 10 seconds in resonable stuff , 160.000 chars size of 0 bytes , @ 260 seconds 4.500.000 chars after while program crashes.

i don't have functions spamming zeromemory. the textbox im talking mw_txtoutput think others affected not much:

main.cpp

// logging macros needs log console defined #define log_info(x)     { console.writetolog("<td><b><font color=\"#0079f2\">[info]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); logtotextbox(mw_txtoutput, x); } #define log_warning(x)  { console.writetolog("<td><b><font color=\"#e4e600\">[warning]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); logtotextbox(mw_txtoutput, x); } #define log_error(x)    { console.writetolog("<td><b><font color=\"#e68a00\">[error]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); logtotextbox(mw_txtoutput, x); } #define log_critical(x) { console.writetolog("<td><b><font color=\"#e60000\">[critical]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); logtotextbox(mw_txtoutput, x); }   // defines window elements #define button_send 100 #define button_finish 101  #include "log.h" #include "misc.h" #include <tchar.h>   using namespace std;   struct windowsize { int width1, height1, width2, height2; }; windowsize txtoutputsize; hinstance hinst; hwnd mw, sw; hwnd mw_txtoutput, mw_txtinput, mw_btnsend; hwnd sw_txtuserpublickey, sw_txtpubkeylable, sw_txtprivatekeycontext, sw_txtprivkeylable, sw_txtprivkeypassword, sw_btnfinish;  bool window1closed, endprog;  lresult callback wndproc(hwnd mw, uint msg, wparam wparam, lparam lparam) {     hdc hdc;     paintstruct ps;     lpcstr userpublickey = "name of public key";     lpcstr pubkeylable = "label of public key";     lpcstr privatekeycontext = "private key context";     lpcstr privkeylable = "label of private key";     lpcstr privkeypassword = "password of private key";      int len = sendmessage(mw_txtinput, wm_gettextlength, 0, 0);     lpcstr lpbuffer = new char[len];      switch (msg) {     case wm_create:         break;     case wm_command:         switch (loword(wparam)) {         case button_send:             sendmessage(mw_txtinput, wm_gettext, (wparam)len + 1, (lparam)lpbuffer);             if(len >= 1) {                 setwindowtext(mw_txtinput, _t(""));                 messagebox(null, lpbuffer, "message", mb_ok);             }             break;         case button_finish:             messagebox(null, "test", "test", mb_ok);             destroywindow(sw);             postquitmessage(0);             break;         }         break;     case wm_paint:         hdc = beginpaint(sw, &ps);         textout(hdc, 5, 5, userpublickey, strlen(userpublickey));         textout(hdc, 5, 55, pubkeylable, strlen(pubkeylable));         textout(hdc, 5, 105, privatekeycontext, strlen(privatekeycontext));         textout(hdc, 5, 155, privkeylable, strlen(privkeylable));         textout(hdc, 5, 205, privkeypassword, strlen(privkeypassword));         endpaint(sw, &ps);         break;         case wm_close:         window1closed = true;         destroywindow(mw);         break;     case wm_destroy:         window1closed = true;         postquitmessage(0);         break;     default:         return defwindowproc(mw, msg, wparam, lparam);     }     return 0; }  int winapi winmain(hinstance hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {     log console;     if (!console.initlog()) {         messagebox(             null,             (lpcstr)"log initialization failed!",             (lpcstr)"fatal error",             mb_topmost | mb_iconerror | mb_retrycancel | mb_defbutton1             );         exit(exit_failure);     }      endprog = false;       lpcstr szwindowclass = "win32app";      wndclassex wc;     msg msg;     rect rect;     wc.cbsize = sizeof(wndclassex);     wc.style = 0;     wc.lpfnwndproc = wndproc;     wc.cbclsextra = 0;     wc.cbwndextra = 0;     wc.hinstance = hinstance;     wc.hicon = loadicon(null, idi_application);     wc.hiconsm = loadicon(null, idi_application);     wc.hcursor = loadcursor(null, idc_arrow);     wc.hbrbackground = (hbrush)(color_window);     wc.lpszmenuname = null;     wc.lpszclassname = szwindowclass;      if (!registerclassex(&wc)) {         log_critical("window registration failed");     return 0;     }      hinst = hinstance;      mw = createwindowa(szwindowclass, text("echat"), (ws_overlapped | ws_caption | ws_sysmenu | ws_minimizebox), cw_usedefault, cw_usedefault, 900, 550, null, null, hinstance, null);      if (mw == null) {         log_critical("main window creation failed");         return 0;     }      if (getclientrect(mw, &rect)) {         txtoutputsize.width1 = rect.right - rect.left;         txtoutputsize.height1 = rect.bottom - rect.top;     }      mw_txtoutput = createwindowex(         ws_ex_clientedge,         text("edit"),         null,         ws_child | ws_visible | es_readonly | es_multiline | ws_vscroll,         5, 5,         txtoutputsize.width1 - 10, txtoutputsize.height1 - 100 - 15,         mw,         null,         null,         null         );      mw_txtinput = createwindowex(         ws_ex_clientedge,         text("edit"),         "",         ws_child | ws_visible | es_autovscroll | es_multiline | es_left,         5, txtoutputsize.height1 - 5 - 100,         txtoutputsize.width1 - 100, 100,         mw,         null,         null,         null         );      mw_btnsend = createwindowex(null,         "button",         "send",         ws_tabstop | ws_visible | ws_child | bs_defpushbutton,         txtoutputsize.width1 - 90,         txtoutputsize.height1 - 5 - 100,         txtoutputsize.width1 - (txtoutputsize.width1 - 100) - 15,         txtoutputsize.height1 - (txtoutputsize.height1 - 100) - 5,         mw,         (hmenu)button_send,         getmodulehandle(null),         null);      showwindow(mw, nshowcmd);     updatewindow(mw);      // setup window     wndclassex sw_c;     rect sw_rect;      sw_c.cbsize = sizeof(wndclassex);     sw_c.style = 0;     sw_c.lpfnwndproc = wndproc;     sw_c.cbclsextra = 0;     sw_c.cbwndextra = 0;     sw_c.hinstance = hinstance;     sw_c.hicon = loadicon(null, idi_application);     sw_c.hiconsm = loadicon(null, idi_application);     sw_c.hcursor = loadcursor(null, idc_arrow);     sw_c.hbrbackground = (hbrush)(color_window);     sw_c.lpszmenuname = null;     sw_c.lpszclassname = "setup";      if (!registerclassex(&sw_c)) {         log_critical("setup window registration failed");         return 0;     }      sw = createwindowa(         szwindowclass,         text("setup"),         (ws_overlapped | ws_caption)         , cw_usedefault, cw_usedefault,         500, 400,         null,         null,         hinstance,         null);      if (sw == null) {         log_critical("setup window creation failed");         return 0;     }      if (getclientrect(sw, &sw_rect)) {         txtoutputsize.width2 = sw_rect.right - sw_rect.left;         txtoutputsize.height2 = sw_rect.bottom - sw_rect.top;     }      sw_btnfinish = createwindowex(         null,         "button",         "finish",         ws_tabstop | ws_visible | ws_child | bs_defpushbutton,         430,         330,         50,         25,         sw,         (hmenu)button_finish,         getmodulehandle(null),         null);      sw_txtuserpublickey = createwindowex(         ws_ex_clientedge,         text("edit"),         null,         ws_child | ws_visible,         5, 25,         475, 25,         sw,         null,         null,         null         );      sw_txtpubkeylable = createwindowex(         ws_ex_clientedge,         text("edit"),         null,         ws_child | ws_visible,         5, 75,         475, 25,         sw,         null,         null,         null         );      sw_txtprivatekeycontext = createwindowex(         ws_ex_clientedge,         text("edit"),         null,         ws_child | ws_visible,         5, 125,         475, 25,         sw,         null,         null,         null         );      sw_txtprivkeylable = createwindowex(         ws_ex_clientedge,         text("edit"),         null,         ws_child | ws_visible,         5, 175,         475, 25,         sw,         null,         null,         null         );      sw_txtprivkeypassword = createwindowex(         ws_ex_clientedge,         text("edit"),         null,         ws_child | ws_visible | es_password,         5, 225,         475, 25,         sw,         null,         null,         null         );      // initialize nonclientmetrics structure     nonclientmetrics ncm;     ncm.cbsize = sizeof(ncm);      // obtain non-client metrics     systemparametersinfo(spi_getnonclientmetrics, sizeof(ncm), &ncm, 0);      // create new font     hfont hnewfont = createfontindirect(&ncm.lfmessagefont);      // set new font     sendmessage(mw_txtinput, wm_setfont, (wparam)hnewfont, 0);     sendmessage(mw_txtoutput, wm_setfont, (wparam)hnewfont, 0);     sendmessage(mw_btnsend, wm_setfont, (wparam)hnewfont, 0);     sendmessage(sw_btnfinish, wm_setfont, (wparam)hnewfont, 0);     sendmessage(sw_txtpubkeylable, wm_setfont, (wparam)hnewfont, 0);     sendmessage(sw_txtprivkeylable, wm_setfont, (wparam)hnewfont, 0);     sendmessage(sw_txtprivatekeycontext, wm_setfont, (wparam)hnewfont, 0);     sendmessage(sw_txtprivkeypassword, wm_setfont, (wparam)hnewfont, 0);     sendmessage(sw_txtuserpublickey, wm_setfont, (wparam)hnewfont, 0);      showwindow(sw, nshowcmd);     updatewindow(sw);        while (endprog == false) {         if (getmessage(&msg, null, 0, 0) > 0) {             translatemessage(&msg);             dispatchmessage(&msg);         }         if (window1closed) {             endprog = true;         }     }       return msg.wparam; } 

log.cpp

#include "log.h" #include "misc.h"  #define log_info(x)     { log::writetolog("<td><b><font color=\"#0079f2\">[info]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); } #define log_warning(x)  { log::writetolog("<td><b><font color=\"#e4e600\">[warning]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); } #define log_error(x)    { log::writetolog("<td><b><font color=\"#e68a00\">[error]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); } #define log_critical(x) { log::writetolog("<td><b><font color=\"#e60000\">[critical]:</font></b></td><td> %s </td><td>(<i>%s</i>, line <i>%d</i>, function <i>%s</i>)</td>", (x), __filename__, __line__, __funcsig__); }   using namespace std;   log::log() { }   log::~log() {     exitlog(); }  bool log::initlog() {     string time_string = getctime("%y-%m-%d_%h-%m");      logfile.open(time_string + ".log.html", fstream::out);     if (!logfile.is_open()) { return false; }      logfile <<         "<!doctype html>" << endl <<         "<html>" << endl <<         "<head>" << endl <<         "<title>echat log: " + time_string + "</title>" << endl <<         "</head>" << endl <<         "<body>" << endl <<         "<font face=\"consolas\" size=\"2\">" << endl <<          "<table>" << endl;      isrunning = true;      log_info("logging started")      return true; }  bool log::exitlog() {     log_info("logging stopped")     logfile <<         "</table></font>" << endl <<         "<!--echat log end-->" << endl <<         "</body>" << endl <<         "</html>";     logfile.close();     isrunning = false;      return false; }  void log::log_loop() {     while (isrunning) {         isrunning = false;         log_info("leaving..")     } }  bool log::writetolog(char* pcformat, ...) {     char actext[1024];           char acnewtext[2048];     va_list valist;      int icursor = 0;      va_start(valist, pcformat);     vsprintf(actext, pcformat, valist);     va_end(valist);      zeromemory(acnewtext, 2048 * sizeof(char));     (int ichar = 0; ichar < (int)strlen(actext); ichar++) {         if (actext[ichar] == '\n') {             acnewtext[icursor++] = '<';             acnewtext[icursor++] = 'b';             acnewtext[icursor++] = 'r';             acnewtext[icursor++] = '>';         } else {             acnewtext[icursor] = actext[ichar];             icursor++;         }     }      logfile << "<tr>" << acnewtext << "</tr>" << endl;      return true; } 

misc.cpp

#include "misc.h"  void addtotextbox(hwnd txtbox, string txt) {     int len = sendmessage(txtbox, wm_gettextlength, 0, 0);     lpcstr lpbuffer = new char[len];      sendmessage(txtbox, wm_gettext, (wparam)len + 1, (lparam)lpbuffer);     txt = lpbuffer + txt + text("\r\n");     lpbuffer = txt.c_str();     setwindowtext(txtbox, lpbuffer);     sendmessage(txtbox, wm_vscroll, sb_bottom, 0l); }  void logtotextbox(hwnd txtbox, string txt) {     int len = sendmessage(txtbox, wm_gettextlength, 0, 0);     lpcstr lpbuffer = new char[len];     string timestamp = "[" + getctime("%h:%m:%s") + "]";      sendmessage(txtbox, wm_gettext, (wparam)len + 1, (lparam)lpbuffer);     txt = lpbuffer + timestamp + ' ' + txt + text("\r\n");     lpbuffer = txt.c_str();     setwindowtext(txtbox, lpbuffer);     sendmessage(txtbox, wm_vscroll, sb_bottom, 0l); }  string getctime(string format) { // "%y-%m-%d_%h-%m" = 1900-12-01_23-59 "%h:%m:%s" = 23:59:59     stringstream buffer;     time_t time = chrono::system_clock::to_time_t(chrono::system_clock::now());      buffer << put_time(&tm(*localtime(&time)), format.c_str());     return buffer.str(); } 


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -