View Single Post
Re: PB crashes after typing "meta unload podbot"
Old
  (#4)
Immortal_BLG
Member
 
Status: Offline
Posts: 171
Join Date: Nov 2007
Location: Russian Federation
Default Re: PB crashes after typing "meta unload podbot" - 10-03-2012

Quote:
Originally Posted by Whistler
try casting the string to (char *)
PRECACHE_SOUND (STRING (ALLOC_STRING ((char *)"weapons/xbow_hit1.wav")))

or (more C++'ish):
PRECACHE_SOUND (STRING (ALLOC_STRING (reinterpret_cast<char *>("weapons/xbow_hit1.wav"))))
You are wrong here as it is STRING macro returns the constant pointer.
Code:
#define STRING(offset) (const char *)(gpGlobals->pStringBase + (int)offset)
Therefore it will be correct to make so:
Code:
PRECACHE_SOUND ((char *)STRING (ALLOC_STRING ("weapons/xbow_hit1.wav")));	// C style
PRECACHE_SOUND (const_cast<char *>(STRING (ALLOC_STRING ("weapons/xbow_hit1.wav"))));	// C++ style
Quote:
Originally Posted by KWo
If I have already allocated a memory area for precached model, what should I use in SET_MODEL function then? I don't think I need to allocate the memory the second time for the model name... I believe I should store somewhere the pointer to that area of precached model, then I should use this pointer somehow in SET_MODEL function. Can You describe it more clearly?
I will try: for example when you call the PRECACHE_MODEL() function you transfer through argument a string (a name of model to precache) which is in address space of your module, the engine sets this pointer to the sv.model_precache array and then uses it, and so, when metamod unloads your module, address space in which was a model name becomes invalid therefore if the engine will try to compare for example your string with any another - at once segfault will jump out.

Code:
// From Q1 sources, but it's still actual....
void PF_precache_model (const char *const s)
{
	int		i;
	
	if (sv.state != ss_loading)
		PR_RunError ("PF_Precache_*: Precache can only be done in spawn functions");

	PR_CheckEmptyString (s);

	for (i=0 ; i<MAX_MODELS ; i++)
	{
		if (!sv.model_precache[i])
		{
			sv.model_precache[i] = s;	// Here the engine takes pointer to model name from address space of your module.
			return;
		}
		if (!strcmp(sv.model_precache[i], s))	// Here we can get segfault if 'sv.model_precache[i]' points into address space of unloaded module.
			return;
	}
	PR_RunError ("PF_precache_model: overflow");
}
  
Reply With Quote