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

Author Topic: Memory problem with buffer  (Read 7135 times)

0 Members and 1 Guest are viewing this topic.

madhav_iiith

  • Newbie
  • *
  • Posts: 5
    • View Profile
Memory problem with buffer
« on: July 11, 2009, 07:50:03 pm »
Hi,
     I have to record some audio and after every 5 minutes I need to create a new wav file from the audio captured.  This process of recording must continue until I stop it Here  is the psuedo code of what I am doing

Code: [Select]

bool flag =True;
while( true){
                           if(flag is True)
                           flag = false
                            Recorder.Start(SampleRate);


                             if( timeInterval  is  5 minutes )
                                      then
                                             flag = true
                                             Recorder.stop() //stop the recorder
                                             //Obtain the buffer  and the write it into a file
                                              sf::SoundBuffer Buffer = Recorder->GetBuffer();
                                               Buffer.SaveToFile(SomeAudioFile.wav)
}

When I implement this in the form of a code and use top command to see the memory requirements  of this process . The momory consumption is increasing very rapildly. within 10 minutes after the start of this program The memory it was using was 1.5 gb  Is it because of not clearance of the soundbuffer ??  I checked for the member functions of class SoundBuffer But I could not find any function which can clear the memory. Can any one suggest what to do . If I cannot solve this problem I need to search for other software  which can record audio  :(  :( :(  After a lot of searching for 15 hours I could find this and I want to use it coz its easy. Please help me resolve this problem . Thanks in advance

Regards,
Nasini Madhava Rao

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Memory problem with buffer
« Reply #1 on: July 11, 2009, 10:53:50 pm »
Could you please give us a complete and minimal example that reproduces the problem, so that we can try / debug it directly?
Laurent Gomila - SFML developer

madhav_iiith

  • Newbie
  • *
  • Posts: 5
    • View Profile
Memory problem with buffer
« Reply #2 on: July 11, 2009, 11:00:59 pm »
I need to create a wav file every 1 minute once the audio capture starts .  I am doing so with the above mentioned code. Now My system crashes after it records for 5-10 minutes. Now I saw the memory it was consuming by the help of top command in Linux . The memory required by my program is continuously increasing and Hence finally resulting in a crash

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Memory problem with buffer
« Reply #3 on: July 11, 2009, 11:21:39 pm »
Could you please give us a complete and minimal example that reproduces the problem, so that we can try / debug it directly?
Laurent Gomila - SFML developer

madhav_iiith

  • Newbie
  • *
  • Posts: 5
    • View Profile
Memory problem with buffer
« Reply #4 on: July 12, 2009, 12:31:50 am »
This code is causing me segFault
Code: [Select]

SampleRate = 44100;bool z=1;int FileCount =1;
 sf::SoundBufferRecorder *Recorder=new sf::SoundBufferRecorder;
time_t sec1,sec2;
time(&sec1);
while(1)
{
 time(&sec2);
      if(z ){
                 Recorder->Start(SampleRate);z =0;
      }
if(sec2-sec1==30)
  {
                sec1=sec2;
                z=1;
                Recorder->Stop();
                char arr[100];
                          sprintf(arr,"%d",FileCount);
                           FileCount++;
                        string temp=arr;
                         AudioName ="Audio_"+temp+".wav";

 
                         const sf::SoundBuffer &B = Recorder->GetBuffer();
                                 B.SaveToFile(AudioName);
                    free(Recorder);
                     Recorder = new sf::SoundBufferRecorder;


   }
}

               On running the above code for 200minutes with 2gb ram crashes . I dont know why but for every file that I create 5MB of more memory is required which can be inferred from the "top"  command in Linux

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Memory problem with buffer
« Reply #5 on: July 12, 2009, 12:47:09 am »
I really meant a complete example, with #includes, main, etc. so that I can copy/paste it directly ;)
You know, if I have to spend 30 minutes to tweak every source code I receive just to make it compile, when you already have a code that works...

Anyway, what happens if you use delete instead of free? Even better: what if you use the same recorder instance instead of creating a new one in every loop? Does it solve the problem?

PS: you should also make your indentation more consistent
Laurent Gomila - SFML developer

madhav_iiith

  • Newbie
  • *
  • Posts: 5
    • View Profile
Memory problem with buffer
« Reply #6 on: July 12, 2009, 01:25:22 am »
Code: [Select]

#include<iostream>
#include <SFML/Audio.hpp>
#include<pthread.h>
#include<cstdlib>
using namespace std;

void* SoundCapture(void *unncessary){
string AudioName;
int SampleRate = 44100;bool z=1;int FileCount =1;
sf::SoundBufferRecorder *Recorder=new sf::SoundBufferRecorder;
time_t sec1,sec2;
time(&sec1);
while(1)
{
time(&sec2);
if(z ){
Recorder->Start(SampleRate);z =0;
}
if(sec2-sec1==30)
{
sec1=sec2;
z=1;
Recorder->Stop();
char arr[100];
sprintf(arr,"%d",FileCount);
FileCount++;
string temp=arr;
AudioName ="Audio_"+temp+".wav";


const sf::SoundBuffer &B = Recorder->GetBuffer();
B.SaveToFile(AudioName);
free(Recorder);
Recorder = new sf::SoundBufferRecorder;


}
}
}
main(){
pthread_t t;
        int a;
        //pthread_create(&t[0],NULL,CaptureVideo,(void *)a[0]);
        pthread_create(&t,NULL,SoundCapture,(void *)a);
        void *status;
        pthread_join(t,&status);
        pthread_exit(NULL);
}


I need to use Threads in my code because I need to record Video simultaneously for my application


to compile dont forget add -lpthread

While the code is running we can infer its memory requirements from top command
i Linux

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Memory problem with buffer
« Reply #7 on: July 12, 2009, 01:58:40 am »
Could you please read the answers Laurent gave you? :roll:

Do not use free() to deallocate variables allocated with new! Better, you don't use malloc() and free() in C++ at all, there are only few use cases (and yours is certainly none of them). By the way, you are producing a memory leak. Is there a reason you need dynamic memory management? Use the stack where possible. And rather employ typesafe C++ classes and functions (std::stringstream) than error-prone C functions (sprintf).

Furthermore, why do you prefer pthread (a non-portable C library which is dangerous, especially if you don't know the language well) to SFML's threads?

Additionally, an infinite loop which does nothing in the most cases is wasting CPU time. Use sf::Sleep(). And functions having a return type different to void must return a value. And main()'s return type is int, so state it! There are so many mistakes in your code, you really seem to lack basic knowledge of C++. I suggest you'd read a good book to learn the language. Believe me, it's worth the time.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

madhav_iiith

  • Newbie
  • *
  • Posts: 5
    • View Profile
Memory problem with buffer
« Reply #8 on: July 12, 2009, 02:20:41 am »
I have to synchronize both audio and video That's why I am using Pthreads. I dont have any idea abt the SFML threads. btw I have solved the problem. Thanks

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Memory problem with buffer
« Reply #9 on: July 12, 2009, 10:30:25 am »
So.. because of what was your program crashing ?
Want to play movies in your SFML application? Check out sfeMovie!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Memory problem with buffer
« Reply #10 on: July 12, 2009, 03:59:56 pm »
Quote from: "madhav_iiith"
btw I have solved the problem. Thanks
Independent of that, you should consider my advice, unless you always want to have dumb errors resulting of bad knowledge about the programming language.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: