Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to delete a sound?  (Read 8301 times)

0 Members and 1 Guest are viewing this topic.

AMD

  • Newbie
  • *
  • Posts: 10
    • View Profile
How to delete a sound?
« on: October 15, 2011, 07:41:15 pm »
Hello :)

I wrote a function to play a sound and delete the sound after playing but this isn't possible.
If I try to delete the sound the application crash!

Here is the small code:
Code: [Select]
   sf::SoundBuffer *Buffer = new sf::SoundBuffer;
    Buffer->LoadFromFile(sound);
    sf::Sound *Sound = new sf::Sound(*Buffer);
    Sound->Play();
//    sf::Sleep(Buffer->GetDuration());
    delete Sound;
    delete Buffer;


If I delete the both lines with "delete Sound" and "delete Buffer" the function works good but I need more and more ram if I play the sound again.
I try to include a sleep and wait if the sound done but it makes no diffrence....

I hope you understand all the problem and can help me to fix this.
(I am using SFML 1.6)

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
How to delete a sound?
« Reply #1 on: October 15, 2011, 08:02:31 pm »
First of all, there's no legitimate reason you should be using pointers in this small sample, so stop.
I use the latest build of SFML2

AMD

  • Newbie
  • *
  • Posts: 10
    • View Profile
How to delete a sound?
« Reply #2 on: October 15, 2011, 08:06:42 pm »
I'am not sure what you mean  :oops:

Can you show me a fixxed code how to do it right?!

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
How to delete a sound?
« Reply #3 on: October 15, 2011, 08:12:02 pm »
Code: [Select]
sf::SoundBuffer Buffer;
Buffer.LoadFromFile(sound);
sf::Sound Sound(Buffer);
Sound.Play();
I use the latest build of SFML2

AMD

  • Newbie
  • *
  • Posts: 10
    • View Profile
How to delete a sound?
« Reply #4 on: October 15, 2011, 08:17:43 pm »
This will not work...
Always if I include the sound function outside from the void main() it will not work... I dont know why but outside the main it works only with new... you know why?

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
How to delete a sound?
« Reply #5 on: October 15, 2011, 08:24:27 pm »
Quote from: "AMD"
This will not work...
Always if I include the sound function outside from the void main() it will not work... I dont know why but outside the main it works only with new... you know why?
First of all, void main? What language are you using? C/C++ require you to use int main, not void main. Also, you aren't being very clear (or I'm half asleep, which seems to be happening a lot lately). Can you describe in detail what you are doing and what the problems are? I recommend providing complete and minimal code that demonstrates your problem.
I use the latest build of SFML2

AMD

  • Newbie
  • *
  • Posts: 10
    • View Profile
How to delete a sound?
« Reply #6 on: October 15, 2011, 08:35:48 pm »
Yes I using C/C++ and you can use void main and not int main  :roll:
I think normally you write:
Code: [Select]
int main() {
//Some code here
return 0;
};


But this will work to:
Code: [Select]
void main() {
//Code...
return;
};


Don't return the nil  :wink:
But thats not important for the problem...

I will show some short codes with the problem:
THIS WORK:
Code: [Select]
int main()
{
sf::SoundBuffer Buffer;
Buffer.LoadFromFile(sound);
sf::Sound Sound(Buffer);
Sound.Play();

return 0;
};


THIS WORK NOT:
Code: [Select]

void CallMySound()
{
sf::SoundBuffer Buffer;
Buffer.LoadFromFile(sound);
sf::Sound Sound(Buffer);
Sound.Play();
};

int main()
{
CallMySound();
return 0;
};



THIS WORK:
Code: [Select]

void CallMySound()
{
    sf::SoundBuffer *Buffer = new sf::SoundBuffer;
    Buffer->LoadFromFile(sound);
    sf::Sound *Sound = new sf::Sound(*Buffer);
    Sound->Play();
};

int main()
{
CallMySound();
return 0;
};



You see it? I can't play a sound outside the main!
The function will load correctly but I don't listen any sound! The new stuff fix this but that's not really good for the ram if I call this again and again...

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
How to delete a sound?
« Reply #7 on: October 15, 2011, 08:40:35 pm »
No, you can't use void main. One of the key rules of C/C++ is that main MUST return an int. Any system which does not require this is in violation of the C++ standard.

Also, your problem is a scope problem. When the function ends and you aren't using pointers, the sound is automatically deleted, stopping the sound. You shouldn't be creating a function just for sound anyways, there's no reason to and it doesn't work. Also, you should probably reboot your computer. You've got a massive memory leak that probably hasn't been cleaned up properly by your OS (unless it's a more recent OS).
I use the latest build of SFML2

AMD

  • Newbie
  • *
  • Posts: 10
    • View Profile
How to delete a sound?
« Reply #8 on: October 15, 2011, 08:48:54 pm »
Okay, I am using int main :wink:

But I have no solution for my problem...
It must be possible to play a sound in other function without to use a pointer but this is not possible in my case? But why?
You see my last post with the second code which isn't working! I don't using pointers and I don't listen any sound  :cry:

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
How to delete a sound?
« Reply #9 on: October 15, 2011, 09:14:27 pm »
Quote from: "AMD"
Okay, I am using int main :wink:

But I have no solution for my problem...
It must be possible to play a sound in other function without to use a pointer but this is not possible in my case? But why?
You see my last post with the second code which isn't working! I don't using pointers and I don't listen any sound  :cry:
I already said this. This is a scope error. When the function ends and you aren't using pointers, the sound is deleted because it goes out of scope. However, using pointers isn't an option either, as that creates a nasty memory leak which may or may not be cleaned up by your OS. The ONLY solution is to NOT use a separate function for playing sound.
I use the latest build of SFML2

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
How to delete a sound?
« Reply #10 on: October 15, 2011, 09:25:32 pm »
Or at least having a sound list, which you will delete later. Think of new and delete as a source block { }

If you open ( { , new) , you've got to close ( } , delete).

 

anything