   Reading a line to it's exacting length...
  Hi!I have a Readline() function to read unicode characters from a file. It worked perfectly, but when I added some functionality to return the exacting length of the line, it made Unreal crash.There's no easy way for me to debug this, since it's run by Unreal (UDK).wchar_t* ReadLine(wifstream *Stream){wchar_t *Line = new wchar_t[256];wchar_t ControlChar = '0';int i = 0;while(ControlChar != '\n'){ControlChar = Stream->get();cout << ControlChar;Line = ControlChar;i++;if(i == 256)break;}i = i * 2;//Make a string of the right size...wchar_t *RightSize = new wchar_t;for(int j = 0; j <= i; j++)RightSize[j] = Line[j];return RightSize;}Doesn't a unicode character take 2 bytes? I would have thought that multiplying i with 2 would do the trick...I'm on Windows btw!   
  Let's see. It looks like you've assigned ansi chars to wide chars or tested wide chars against ansi chars. wchar_t ControlChar = '0';ControlChar != '\n'Try this instead. wchar_t ControlChar = L'0';ControlChar != L'\n'Also, consider using the index to control the loop and break from it only when the condition is met. while(i < 256){   ...   if( L'\n' == ControlChar ) break;   i++;}Also consider that L'\n' may not be the newline character you want. "In practice, there are many applications that erroneously use the C newline character '\n' instead". You might need to try '\r\n' or \x0D\x0A or \x000D\x000A. And yes, that complicates things. And you don't need to multiply i by 2, unless you want to know how many bytes are involved. Count characters or count bytes, but keep the count consistent. It looks like you want to count characters. wchar_t *RightSize = new wchar_t;If so then you don't need to multiply by two. Also ask yourself what your program gains by trimming the array. Is it really that much memory? Are you looking at more than 4096 bytes? (i.e. one page) And don't forget to call delete on Line and later on RightSize. ;Quote:Also ask yourself what your program gains by trimming the array. Is it really that much memory? Are you looking at more than 4096 bytes? (i.e. one page) This isn't about memory. It's about needing to use the line that was read from the file to load a level in Unreal, and to do that I need to have only the levelname, not a bunch of non-ASCII characters at the end...Unreal still crashes btw. I think you are correct about me wanting to know the number of characters in the string... but my current code doesn't seem to be working... :Edit: The string is already being read to it's exacting length. I'm interested in figuring out how to trim the resulting string...[Edited by - MatsVed on May 16, 2010 5:29:52 AM];  You'll probably also need to check if the input stream is still valid (istream::good, istream::eof, istream::bad, istream::fail, or the likes ...), in the case of a line which is not delimited with a newline character.   ;Quote:Original post by MatsVedUnreal still crashes btw. I think you are correct about me wanting to know the number of characters in the string... but my current code doesn't seem to be working... :wchar_t *RightSize = new wchar_t;for(int j = 0; j <= i; j++)RightSize[j] = Line[j];RightSize is i characters long, but the loop goes from 0 to i which is i+1 characters. I.e. you're writing 1 position past the end of the allocated memory.Quote:Original post by MatsVedThis isn't about memory. It's about needing to use the line that was read from the file to load a level in Unreal, and to do that I need to have only the levelname, not a bunch of non-ASCII characters at the end...Doesn't Unreal take null-terminated strings like pretty much every other API? If it does, not only could you skip copying the string into a new array altogether, but it's probably also (one of) the reason it crashes. Unreal simply starts reading one character at time from the beginning of the character array you pass it until it reaches a character with value zero. The string you return from ReadLine don't have a null-terminator, which means that Unreal will read past the end of the allocated memory and likely trigger a memory access violation error.Just add a zero value after your read-loop:while(/*...*/) {ControlChar = Stream->get();cout << ControlChar;Line = ControlChar;// ...}Line = 0; // Null-terminatorBut unless you really need to echo the input, I would consider instead just using:wstring Line;(*Stream) >> Line;return Line.c_str();;Quote:Original post by MatsVedThere's no easy way for me to debug this, since it's run by Unreal (UDK).You should be able to debug it quite easily. Assuming Your code is in a DLL, and you're using Visual Studio just change the debug settings on your DLL solution to launch the Unreal executable, and if it crashes in your DLL the debugger will work fine. If it crashes elsewhere you'll probably just be looking at the disassembly though.   