Trying to make an ingame text editor.

Started by
15 comments, last by Syerjchep 11 years, 3 months ago
Why are you using a 'character grid' instead of an std::list<std::string>? You could check each string and split it when you encounter a newline, pushing back multiple substrings to the list, and popping the front of the list when you get beyond your desired buffer size (say, 100 lines or more).

Which function is crashing? Under what conditions does it crash?

Are you saying the program crashes before you even enter int main()? If so, it's probably because your "char charactergrid[1000][1000];" takes up almost a full megabyte on the stack. You shouldn't put that kind of memory on the stack, it's not made for that. Using a std::vector (not 2D!) or std::list of std::strings is a far superior and 'correct' solution.

After a bit of testing, I found It is the character show function.

Also, I can't just make the strings into smaller strings, as they are going to be edited, which may or may not include adding or removing newline characters that would move all the other rows around.

Also, apparently the loop doesn't immedently crash the program, rather, at some point something goes wrong a few seconds in randomly, and suddenly every single iteration of the loop takes over a second, so I was able to add a crash prevention of sorts, that will automatically shut down the whole program if a single iteration of the scripting loop takes over 200ms.

Advertisement
Also, I can't just make the strings into smaller strings, as they are going to be edited, which may or may not include adding or removing newline characters that would move all the other rows around.


In that case, why not have the entire "buffer" be a single string, with newlines and all?
Then anytime the string changes in response to keypresses, you parse the the string into a std::vector<std::string> at every location there is a newline and at every location where a line is greater than 'n' characters wide.

The std::vector<std::string> is the formatted data ready for drawing and is generated from the huge std::string which is the text the user edits.

Also, apparently the loop doesn't immedently crash the program, rather, at some point something goes wrong a few seconds in randomly, and suddenly every single iteration of the loop takes over a second, so I was able to add a crash prevention of sorts, that will automatically shut down the whole program if a single iteration of the scripting loop takes over 200ms.


That sounds like you are hiding the problem rather than understanding and fixing the problem. wink.png

Also, I can't just make the strings into smaller strings, as they are going to be edited, which may or may not include adding or removing newline characters that would move all the other rows around.


In that case, why not have the entire "buffer" be a single string, with newlines and all?
Then anytime the string changes in response to keypresses, you parse the the string into a std::vector<std::string> at every location there is a newline and at every location where a line is greater than 'n' characters wide.

The std::vector<std::string> is the formatted data ready for drawing and is generated from the huge std::string which is the text the user edits.

Also, apparently the loop doesn't immedently crash the program, rather, at some point something goes wrong a few seconds in randomly, and suddenly every single iteration of the loop takes over a second, so I was able to add a crash prevention of sorts, that will automatically shut down the whole program if a single iteration of the scripting loop takes over 200ms.


That sounds like you are hiding the problem rather than understanding and fixing the problem. wink.png

Well in my last rewrite (updated like 5 minutes ago) that's what I did, without the vectors.

It's all just one string, no more characterGrid.

But, for whatever reason, it still crashed.

I guess I'll try to isolate the function again.

Random guess: applyText() doesn't like being passed empty strings?

Using a debugger, you can find out exactly where something is crashing and why.


void DisplayText(const std::string &text)
{
	unsigned int lineNum = 0;
    std::string lineText;
	
	for(unsigned int i = 0; i < text.size(); i++)
	{
		//Check if it's the end of the line.
		if(text[i] == '\n')
		{
			//Check if there's something to display.
			if(!lineText.empty())
			{
				//Display the line.
				applyText(20, (20 * lineNum), lineText);
				
				//Clear the temporary string.
				lineText.clear();
			}
			
			//Increase the line number.
			lineNum++;
		}
		else
		{
			//Add this character to the temporary string.
			lineText.push_back(text[i]);
		}
	}
	
	//Display the final line (because there is no final endline).
	if(!lineText.empty())
	{
		applyText(20, (20 * lineNum), lineText);
	}
}

Random guess: applyText() doesn't like being passed empty strings?

Using a debugger, you can find out exactly where something is crashing and why.


void DisplayText(const std::string &text)
{
	unsigned int lineNum = 0;
    std::string lineText;
	
	for(unsigned int i = 0; i < text.size(); i++)
	{
		//Check if it's the end of the line.
		if(text[i] == '\n')
		{
			//Check if there's something to display.
			if(!lineText.empty())
			{
				//Display the line.
				applyText(20, (20 * lineNum), lineText);
				
				//Clear the temporary string.
				lineText.clear();
			}
			
			//Increase the line number.
			lineNum++;
		}
		else
		{
			//Add this character to the temporary string.
			lineText.push_back(text[i]);
		}
	}
	
	//Display the final line (because there is no final endline).
	if(!lineText.empty())
	{
		applyText(20, (20 * lineNum), lineText);
	}
}

This works, and I'm sure it's better.

Yet it's still crashing.

I added try catch statements, yet they return nothing.

Gonna try and figure out how to use a debugger now.

try and catch() only catch exceptions when an exception is thrown with throw. They don't catch crashes in general. Debuggers are definitely the way to go.

I finally fixed it after almost a week of fustration.

And I only had to add one like of code.

Guess what it was?

I forgot to free the textures created by applyText. e.g. it was just a memory leak

This topic is closed to new replies.

Advertisement