Getting a weird string return

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.

Getting a weird string return

Postby LunaRebirth » Sat May 13, 2017 3:11 am

I'm going to make this as short as possible, but it's been stressing me out for the last 12 hours.

Basically I'm reading content from a file to run as a lua script.
For each script, there is a new LuaScript() object created.
The jist of what I'm doing is absolutely nothing new, and pretty basic, so I don't understand why I'm having issues.

When I use lua to run LuaScript[x]->getCont() (Runs the content of the lua file), it runs fine with no issues.
But when I do something like a print statement using LuaScript[x]->getCont(), I'm given a piece of the content, with nothing else in the print.

For example,
cpp Code: Select all
printf("This text isn't appearing. This content is cut into a piece: %s\n", script[0]->getCont());

Pretending that content holds "test", the output would be:
"te"
even without the beginning part of the printf statement.

I don't understand what's going on as this is a new issue I have never encountered.
I've change LuaScript's cont variable to be a char* and an std::string with no change in results.

The weird thing is that running the Lua using getCont() uses the entire correct content. But using getCont() to print it does not work.
It also does not work while trying to send the getCont() to a server with send(), which is what I'm trying to use it for.

Anyone have any explanations as to why this might be happening?
LunaRebirth
 
Posts: 251
Joined: Sun May 11, 2014 12:13 am

Re: Getting a weird string return

Postby CuteAlien » Sat May 13, 2017 9:23 am

Never had that. What does it show in memory when you set a breakpoint there? Do you maybe use printf in several threads? It's not thread-safe, so you have to use mutexes around it when doing that.

Note that if your code overwrites memory at some place all kind of effects can happen. Hunting such a bug is harder. Usually you have to use divide&conquer - splitting your application up into smaller and smaller parts and testing each of those on their own until you located the problem. Or on Linux you can use a tool like Valgrind to see if it catches any errors.
IRC: #irrlicht on irc.freenode.net
Code snippets, patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Free racer created with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
User avatar
CuteAlien
Admin
 
Posts: 8150
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany

Re: Getting a weird string return

Postby LunaRebirth » Tue May 16, 2017 4:45 pm

Still can't figure it out. Everything seems fine except that I can't send it and can't print it. Using an if statement to compare it to what the content should be works fine.
Not overwriting any memory. Seems like an issue beyond my written code.
Looks like I'll redo some stuff and find a different way to get the results I'm looking for.
LunaRebirth
 
Posts: 251
Joined: Sun May 11, 2014 12:13 am

Re: Getting a weird string return

Postby LunaRebirth » Sat May 20, 2017 1:56 am

Turns out the problem is something I can't find on Google, though I didn't try for long.
If I make a const char* and assign a value, then make a string and assign it to the const char*'s value, it sometimes messes up.
Just an example, not real data:
const char* info = "aaaa"
std::string otherInfo = info

otherInfo then holds something like "???a???aaa"
(replacing ??? with random looking symbols)

Has anyone else experienced this?
LunaRebirth
 
Posts: 251
Joined: Sun May 11, 2014 12:13 am

Re: Getting a weird string return

Postby hendu » Sat May 20, 2017 7:43 am

That's not possible. You have corrupted software or hardware.
hendu
 
Posts: 2567
Joined: Sat Dec 18, 2010 12:53 pm

Re: Getting a weird string return

Postby CuteAlien » Sat May 20, 2017 11:08 am

If that's your complete code - then yeah - like hendu said - that shouldn't be. But more often when mixing pointers and string-classes the user messes up somewhere. Examples: You return a pointer to some string you created dynamically (like return std::string("bla").c_str() for a local string - that even might look like works as memory is often still there... but then your next function returns that pointer - and then you start to notice you got corruption). Or you return a pointer to a static string somewhere which buffers stuff, but you overwrite that static at some point. Then your string can look pretty much like your string (this happened to me just last week *sigh*).

What I'm trying to say - when string-pointers look like that it's nearly always a life-time thing - you are having pointers to strings which are no longer valid at this point.
IRC: #irrlicht on irc.freenode.net
Code snippets, patches&stuff: http://www.michaelzeilfelder.de/irrlicht.htm
Free racer created with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
User avatar
CuteAlien
Admin
 
Posts: 8150
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany

Re: Getting a weird string return

Postby LunaRebirth » Mon May 22, 2017 3:36 pm

Ah that could quite possibly be it. I was able to fix my getCont() function and stop the issue by creating a local variable that holds the content, then returns it. But returning the content directly is what was giving the corruption.
Seems I've found a second spot where this is occurring. Quite annoying.
LunaRebirth
 
Posts: 251
Joined: Sun May 11, 2014 12:13 am


Return to Beginners Help

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 1 guest