Welcome to the Builder Academy

Question Problems dynamically changing room name

More
27 Sep 2013 10:46 #4354 by Ornir
Hi everyone -

In working on the wilderness system for Luminari, I need to change the names of rooms on the fly as the player enters them. I have been doing the following:
Code:
if (world[room].name) free(world[room].name); if(regions) { world[room].name = strdup(region_table[regions->rnum].name); } else { world[room].name = "The Wilderness of Luminari"; }

This sets the room name to match the current region (a separate structure that is defined by a polygon, that is something for another post.)

Running this in valgrind says that I am performing an illegal free() at the line free(world[room].name).

If I just run this on the mud, it will crash the mud. This only happens occasionally, not for every room.

Anyone have any ideas? Has anyone been able to manage dynamic room names/descriptions? I am really at a loss, but it might just be because I have been staring at it for so long.

Thanks.

Luminari - a Pathfinder/D&D inspired adventure!
www.luminarimud.com
luminarimud.com 4100

Please Log in or Create an account to join the conversation.

More
28 Sep 2013 23:15 #4357 by thomas
Code:
world[room].name = strdup("The Wilderness of Luminari");

Please Log in or Create an account to join the conversation.

More
30 Sep 2013 07:35 #4360 by Ornir
Sorry if I am being a noob - But doesn't that leak the memory from the previous string, the one pointed to by .name prior to changing it?

I have changed it to a strcpy for the time being, and it SEEMS ok, but i have to dive into valgrind to be sure.

Luminari - a Pathfinder/D&D inspired adventure!
www.luminarimud.com
luminarimud.com 4100

Please Log in or Create an account to join the conversation.

More
01 Oct 2013 20:59 #4366 by thomas
What I meant was that you should switch the line in your code. Sorry I wasn't more verbose:
Code:
if (world[room].name) free(world[room].name); if(regions) { world[room].name = strdup(region_table[regions->rnum].name); } else { world[room].name = strdup("The Wilderness of Luminari"); }
Either that or go the other way; this would be feasible too:
Code:
static char *wilderness_name = str_dup("The Wilderness of Luminari"); if (world[room].name && world[room].name != wilderness_name) free(world[room].name); if(regions) { world[room].name = strdup(region_table[regions->rnum].name); } else { world[room].name = wilderness_name; }
Advantage here is that you only ever allocate one copy of the string.
The following user(s) said Thank You: Ornir

Please Log in or Create an account to join the conversation.

Time to create page: 0.201 seconds