Welcome to the Builder Academy

Question To Free or Not To Free?

More
10 Jan 2018 02:59 #7318 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:
Code:
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.

More
11 Jan 2018 23:57 #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.
The following user(s) said Thank You: WhiskyTest

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

More
12 Jan 2018 20:00 #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.

More
14 Jan 2018 14:11 #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.

More
21 Jan 2018 00:03 #7415 by Ornir
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.

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
21 Jan 2018 21:28 #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

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

Time to create page: 0.291 seconds