By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Memset might be causing a crash !!

P: n/a
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

Expand|Select|Wrap|Line Numbers
  1.   long size = utf8String.length() * 2;
  2.   std::string nativeString;
  4.        if(size < 1024) 
  5.       {
  7.           const int BUF_SIZE = 1024;     // 1024 NEEDSWORK: don't hardcode buf size Priya:replaced with length of the string
  8.           char tempString[BUF_SIZE];
  9.           memset(tempString,0,BUF_SIZE);
  10.           size = BUF_SIZE;
  11.           nativeConv.fromUnicodeString(tempString, size, ustring, status);
  12.           nativeString = tempString;
  13.           //delete[]tempString;
  15.       }
  16.       else 
  17.       {//Thisblock gets executed only if the size > 1024
  20.         const int BUF_SIZE = 4000;     
  21.           char tempString[BUF_SIZE];
  22.           memset(tempString,0,BUF_SIZE);
  23.           size = BUF_SIZE;
  24.           nativeConv.fromUnicodeString(tempString, size, ustring, status);
  25.           nativeString = tempString;
  28.        /*char* tempString;   
  29.           memset(tempString,0,size);  
  30.           nativeConv.fromUnicodeString(tempString, size, ustring, status);
  31.           nativeString = std::string(tempString);
  32.           delete[]tempString;
  33.   */
  34.       }
Nov 18 '10 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 5K+
P: 9,197
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?
Nov 18 '10 #2

Expert 100+
P: 2,396
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?
Nov 18 '10 #3

P: 1,059
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???????
Nov 19 '10 #4

P: 256
Yeah, I can't see any potential for a memory leak in this code. Is there any chance that fromUnicodeString is allocating an array under tempString for you? I have no idea what fromUnicodeString 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 fromUnicodeString does)
Nov 19 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.