- Posts: 345
- Thank you received: 73
To Free or Not To Free?
- WhiskyTest
-
Topic Author
- Offline
- Platinum Member
-
Less
More
5 years 8 months ago #7318
by WhiskyTest
To Free or Not To Free? was created by WhiskyTest
Quick explanation of what I am trying to achieve (in case I've gone off on a tangent) :
I want to be able to specify an additional 'char_data' and an 'int' in an event, using mud events.
As an example, to create a damage-over-time event I would like to have the damager, damagee and the type of damage included with the event.
So I've added the information to the event structure like this:
When free_mud_event is called, should I be freeing vStruct and/or vInt as well?
Should vInt be *vInt, like *sVariables is? Bit confused on this haha
I want to be able to specify an additional 'char_data' and an 'int' in an event, using mud events.
As an example, to create a damage-over-time event I would like to have the damager, damagee and the type of damage included with the event.
So I've added the information to the event structure like this:
struct mud_event_data {
struct event * pEvent; /***< Pointer reference to the event */
event_id iId; /***< General ID reference */
void * pStruct; /***< Pointer to NULL, Descriptor, Character .... */
char * sVariables; /***< String variable */
+ void *vStruct; // Pointer to Victim
+ int vInt; // variable integer for attacktype, damage, etc..
};
When free_mud_event is called, should I be freeing vStruct and/or vInt as well?
Should vInt be *vInt, like *sVariables is? Bit confused on this haha
Please Log in or Create an account to join the conversation.
- thomas
-
- Offline
- Administrator
-
Less
More
- Posts: 818
- Thank you received: 159
5 years 8 months ago #7321
by thomas
Replied by thomas on topic To Free or Not To Free?
Do not free it. In this case, the pointer points to memory allocated elsewhere.
A good rule of thumb is to check if you are CREATE'ing anything when assigning the pointer. I assume here, you
re just doing something like "vStruct = GET_FIGHTING(ch)". So, do not free it when no longer in use.
A good rule of thumb is to check if you are CREATE'ing anything when assigning the pointer. I assume here, you
re just doing something like "vStruct = GET_FIGHTING(ch)". So, do not free it when no longer in use.
The following user(s) said Thank You: WhiskyTest
Please Log in or Create an account to join the conversation.
- WhiskyTest
-
Topic Author
- Offline
- Platinum Member
-
Less
More
- Posts: 345
- Thank you received: 73
5 years 8 months ago #7326
by WhiskyTest
Replied by WhiskyTest on topic To Free or Not To Free?
Cool thanks
Please Log in or Create an account to join the conversation.
- thomas
-
- Offline
- Administrator
-
Less
More
- Posts: 818
- Thank you received: 159
5 years 8 months ago #7344
by thomas
Replied by thomas on topic To Free or Not To Free?
oh, realized strdup() comes in the same category as CREATE.
Please Log in or Create an account to join the conversation.
- Ornir
-
- Offline
- Senior Member
-
- Co-owner of Luminari
5 years 8 months ago #7415
by Ornir
Luminari - a Pathfinder/D&D inspired adventure!
www.luminarimud.com
luminarimud.com 4100
Replied by Ornir on topic To Free or Not To Free?
Be careful! Carrying pointers around in events for data created elsewhere can be very bad if the data is freed or modified out from under you. I had major issues trying this with a bleeding event that would give the attacker credit for your death.
It is much safer to use the existing string and stick the username and other data in there and then extract it in the event function. That way you can look up the player and if they are gone you are safe.
It is much safer to use the existing string and stick the username and other data in there and then extract it in the event function. That way you can look up the player and if they are gone you are safe.
Luminari - a Pathfinder/D&D inspired adventure!
www.luminarimud.com
luminarimud.com 4100
Please Log in or Create an account to join the conversation.
- WhiskyTest
-
Topic Author
- Offline
- Platinum Member
-
Less
More
- Posts: 345
- Thank you received: 73
5 years 8 months ago #7423
by WhiskyTest
Replied by WhiskyTest on topic To Free or Not To Free?
Yeah that's a really good point. I've been doing lots of checking when the event fires to make sure all relevant characters are still valid, but it is getting cumbersome.
Specially as you say there are so many ways the data can be affected outside the event.
I'll try using a string/lookup method.
This is part of the fun for me, puzzling out how to achieve these little tweaks :D
Specially as you say there are so many ways the data can be affected outside the event.
I'll try using a string/lookup method.
This is part of the fun for me, puzzling out how to achieve these little tweaks :D
Please Log in or Create an account to join the conversation.
Time to create page: 0.110 seconds