Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Memory Handling Check

Memory Handling Check

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancehelpquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anzy
    wrote on last edited by
    #1

    Hi.. I'm very new to C++ and have just written 2 methods that are pretty unstable (ie. same input may or may not run correctly). My gut feeling tells me it's because of my poor memory and string management. Could someone kindly review the code and let me know if there's any obvious error? I would seriously very appreciate it. Method 1 ============

    char* _LoggedInUserID;
    char* _LoggedInUserName;
    DWORD CALLBACK streamDisclaimer(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
    {
    DWORD read;
    CRtfStream *st = reinterpret_cast(dwCookie);
    const int MAX_USER_TAG = 10;

    char\* userTag = (char\*) malloc(strlen("")+1);
    strncpy(userTag, "", strlen(""));
    userTag\[strlen("")\] = '\\0';
    
    size\_t displayLen = strlen(\_DlgLoggedInUserID);
    if(\_DlgLoggedInUserName != NULL) displayLen += strlen(\_DlgLoggedInUserName) + 3;  	char\* displayStr = (char\*) malloc(displayLen+1);
    
    if(\_DlgLoggedInUserName != NULL){
    	sprintf(displayStr, "%s (%s)", \_DlgLoggedInUserName, \_DlgLoggedInUserID);
    }else{
    	sprintf(displayStr, "%s", \_DlgLoggedInUserID);
    }
    displayStr\[displayLen\] = '\\0';
    
    LPBYTE tmpBuff = new BYTE\[cb + (displayLen\*MAX\_USER\_TAG)\];
    
    ReadFile(st->fileDisclaimer.get(), tmpBuff, cb, &read, NULL);
    string tmBuffStr = (char\*) tmpBuff;
    size\_t iPos      = tmBuffStr.find(string(userTag), 0);
    
    if(iPos != tmBuffStr.npos){
    	int cnt = 0;
    	while(iPos != tmBuffStr.npos && cnt < MAX\_USER\_TAG){
    		tmBuffStr = tmBuffStr.erase(iPos, strlen(userTag));
    		tmBuffStr.insert(iPos, displayStr);
    		iPos = tmBuffStr.find(string(userTag), 0);
    		cnt++;
    	}
    	int newLength = read - (strlen(userTag)\*cnt) + (displayLen\*cnt);
    	strncpy((char\*)pbBuff, tmBuffStr.c\_str(), newLength);
    	pbBuff\[newLength\] = '\\0';
    	\*pcb = newLength;
    }else{
    	strncpy((char\*)pbBuff, (char\*)tmpBuff, read);
    	\*pcb = read;
    }
    
    // clean up
    delete \[\] userTag;
    delete \[\] displayStr;
    
    userTag    = NULL;
    displayStr = NULL;
    tmpBuff    = NULL;
    
    return 0;
    

    }

    Method 2 =============

    char* _DlgLoggedInUserID;
    char* _DlgLoggedInUserName;

    void CGina::setLoggedInUserID(PWSTR pszUserName){

    // free memory dynamically allocated
    delete \[\] \_LoggedInUserID;  
    delete \[\] \_LoggedInUserName;
    
    // deallocate memory that's not used
    \_LoggedInUserID   = NULL;
    \_LoggedInUserName = NULL;
    
    if(pszUserName != NULL)
    {
    	int nstrlen = WideCharToMultiByte(CP\_ACP, 0, pszUserName, -1, NULL, 0, NULL, NULL);
    	PSTR UserNameStr = (PSTR)HeapAlloc(GetProcessHeap(),0,ns
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups