Memset might be causing a crash !!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bikram Soni

    #1

    Memset might be causing a crash !!

    My application is multi threaded and it gets about 60 reuquests per minute.


    The code Below seems to be causing a memory leak.. not sure whats causing it

    Kindly Help


    Code:
      long size = utf8String.length() * 2;
      std::string nativeString;
    
     	  if(size < 1024) 
    	  {
    
    		  const int BUF_SIZE = 1024;     // 1024 NEEDSWORK: don't hardcode buf size Priya:replaced with length of the string
    		  char tempString[BUF_SIZE];
    		  memset(tempString,0,BUF_SIZE);
    		  size = BUF_SIZE;
    		  nativeConv.fromUnicodeString(tempString, size, ustring, status);
    		  nativeString = tempString;
    		  //delete[]tempString;
    
    	  }
    	  else 
    	  {//Thisblock gets executed only if the size > 1024
    
    		 
    	    const int BUF_SIZE = 4000;     
    		  char tempString[BUF_SIZE];
    		  memset(tempString,0,BUF_SIZE);
    		  size = BUF_SIZE;
    		  nativeConv.fromUnicodeString(tempString, size, ustring, status);
    		  nativeString = tempString;
    	   
    	   
    	   /*char* tempString;   
    		  memset(tempString,0,size);  
              nativeConv.fromUnicodeString(tempString, size, ustring, status);
    		  nativeString = std::string(tempString);
    		  delete[]tempString;
      */
    	  }
    Last edited by NeoPa; Nov 18 '10, 01:54 PM. Reason: Added CODE tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I guess my first question is what is memset doing in a C++ program in the first place? Is there some reason you can't use string objects?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I'm no C++ expert, but I notice that lines 12 and 25 are different from line 31. What is the correct way to assign a character-array to a string?

      tempString is an automatic variable with block scope. Why do you try to delete it at lines 13 and 32?

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        The part you noticed (31) is commented out.

        memset function initiate all bytes with a value(2nd parameter)
        else block get executed only if size is greater than 1023

        Why do you need to assign size? where you just can use BUF_SIZE

        memory leak???????

        Comment

        • mac11
          Contributor
          • Apr 2007
          • 256

          #5
          Yeah, I can't see any potential for a memory leak in this code. Is there any chance that fromUnicodeStri ng is allocating an array under tempString for you? I have no idea what fromUnicodeStri ng is, can you provide a link to some doc for it?

          Maybe show us whats around the code you showed above? If you're multithreaded perhaps you've got something non-thread safe somewhere?

          std::string operator= (assignment) requires that the thing you give it is a null terminated string. Are you sure tempString (line 12 for example) is null terminated? (again, no idea what fromUnicodeStri ng does)

          Comment

          Working...