1 registered members (AndrewAMD),
840
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
function parameter and var pointer
#431114
10/09/13 15:17
10/09/13 15:17
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
It's me again... I have following function to play a sound (it's called in while loop). This function plays a beginning sound, if the beginning sound is over, it plays the loop sound:
#define SND_MULTIPLIER 0.75
#define SND_RANGE 20
var SND_sfx_volume=100;
void ent_sound_start(ENTITY* ent_,SOUND* sndBegin,SOUND* sndLoop,SOUND* sndEnd,var* sndBeginHndl,var* sndLoopHndl,var* sndEndHndl)
{
snd_stop(*sndEndHndl);
if(sndBegin!=NULL
&& !snd_playing(*sndBeginHndl)
&& !snd_playing(*sndLoopHndl)
&& *sndBeginHndl==0)
{*sndBeginHndl=ent_playsound(ent_,sndBegin,SND_sfx_volume*SND_MULTIPLIER*SND_RANGE);}
if(sndLoop!=NULL
&& !snd_playing(*sndBeginHndl)
&& !snd_playing(*sndLoopHndl))
{*sndLoopHndl=ent_playloop(ent_,sndLoop,SND_sfx_volume*SND_MULTIPLIER*SND_RANGE);}
}
99% percent of the time I call this function, it works very well. But sometimes I get a random crash in the instruction "snd_playing(*sndLoopHndl)". I really don't know what's wrong. I guess it's something with pointers.
Last edited by oliver2s; 10/09/13 15:29.
|
|
|
Re: function parameter and var pointer
[Re: oliver2s]
#431115
10/09/13 15:29
10/09/13 15:29
|
Joined: Dec 2008
Posts: 1,218 Germany
Rackscha
Serious User
|
Serious User
Joined: Dec 2008
Posts: 1,218
Germany
|
i don't even have a clue, why this compiles:
if(sndBegin!=NULL)
&& !snd_playing(*sndBeginHndl)
&& !snd_playing(*sndLoopHndl)
&& *sndBeginHndl==0)
{}
there is a () missing around the whole statement! Lite-C requires brackets around the whole condition in an if statement so this is usually not possible: and has to look like
MY Website with news of my projects: (for example my current Muliplayer Bomberman, GenesisPrecompiler for LiteC and TileMaster, an easy to use Tile editor) Sparetime-Development
|
|
|
Re: function parameter and var pointer
[Re: Ch40zzC0d3r]
#431119
10/09/13 16:26
10/09/13 16:26
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
As I mentioned the crashing instruction (only randomly) is this: "snd_playing(*sndLoopHndl)". I found that out with the following code:
void ent_sound_start(ENTITY* ent_,SOUND* sndBegin,SOUND* sndLoop,SOUND* sndEnd,var* sndBeginHndl,var* sndLoopHndl,var* sndEndHndl)
{
diag("\n main_ent_sound_start.1");
snd_stop(*sndEndHndl);
diag("\n main_ent_sound_start.2");
if(sndBegin!=NULL)
{
diag("\n main_ent_sound_start.2.1");
if(!snd_playing(*sndBeginHndl))
{
diag("\n main_ent_sound_start.2.1.1");
if(!snd_playing(*sndLoopHndl))
{
diag("\n main_ent_sound_start.2.1.1.1");
if(*sndBeginHndl==0)
{
diag("\n main_ent_sound_start.2.1.1.1.1");
*sndBeginHndl=ent_playsound(ent_,sndBegin,SND_sfx_volume*SND_MULTIPLIER*SND_RANGE);
diag("\n main_ent_sound_start.2.1.1.1.2");
}
diag("\n main_ent_sound_start.2.1.1.2");
}
diag("\n main_ent_sound_start.2.1.2");
}
diag("\n main_ent_sound_start.2.2");
}
diag("\n main_ent_sound_start.3");
if(sndLoop!=NULL)
{
diag("\n main_ent_sound_start.3.1");
if(!snd_playing(*sndBeginHndl))
{
diag("\n main_ent_sound_start.3.1.1");
if(!snd_playing(*sndLoopHndl))
{
diag("\n main_ent_sound_start.3.1.1.1");
*sndLoopHndl=ent_playloop(ent_,sndLoop,SND_sfx_volume*SND_MULTIPLIER*SND_RANGE);
diag("\n main_ent_sound_start.3.1.1.2");
}
diag("\n main_ent_sound_start.3.1.2");
}
diag("\n main_ent_sound_start.3.2");
}
diag("\n main_ent_sound_start.4");
}
If it crashs, "main_ent_sound_start.2.1.1" is the last entry in acklog.txt.
|
|
|
Re: function parameter and var pointer
[Re: oliver2s]
#431131
10/09/13 18:11
10/09/13 18:11
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Well if that crashes sndLoopHndl will most likely be a vagabonding pointer. You should inspect the code that calls this function and especially the value for the argument sndLoopHndl.
Is there a specific reasong why you pass a pointer to the handle instead of the handle itself?
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: function parameter and var pointer
[Re: Uhrwerk]
#431132
10/09/13 18:18
10/09/13 18:18
|
Joined: Aug 2002
Posts: 3,258 Mainz
oliver2s
OP
Expert
|
OP
Expert
Joined: Aug 2002
Posts: 3,258
Mainz
|
Is there a specific reasong why you pass a pointer to the handle instead of the handle itself? Yes, because the handle have to get a new value. If I pass the handle instead of the pointer, I just get a local variable inside the function.
|
|
|
Re: function parameter and var pointer
[Re: oliver2s]
#431135
10/09/13 18:34
10/09/13 18:34
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
I don't know if the following quote affects you or not or even can be the reason for your crash (in any thinkable circumstance): Seien Sie vorsichtig wenn Sie beim Aufrufen anderer Funktionen nicht-statische, lokale Variablen als Argumente übergeben. Lokale Variablen existieren nämlich ausschließlich innerhalb der aufrufenden Funktion. Sobald diese per return oder wait() beendet wird, werden die Argumente ungültig.
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
|