Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

make AudioPacket's leak detection work properly with multiple threads

  • Loading branch information...
commit 900a70d10c1b5c9f37546e2ae55eb6e15ea0d66c 1 parent 0eef179
root authored

Showing 1 changed file with 12 additions and 7 deletions. Show diff stats Hide diff stats

  1. +12 7 raw_frame/audio_packet.cpp
19 raw_frame/audio_packet.cpp
@@ -45,12 +45,15 @@ AudioPacket::AudioPacket(unsigned int rate, unsigned int channels,
45 45 }
46 46
47 47 void AudioPacket::leak_detect( ) {
48   - npackets++;
49   - if (npackets > 1000) {
  48 + //npackets++;
  49 + size_t current_npackets = __sync_add_and_fetch(&npackets, 1);
  50 +
  51 + if (current_npackets > 1000) {
50 52 fprintf(stderr,
51   - "More than 1000 AudioPackets are outstanding!\n"
  53 + "More than 1000 (%zd) AudioPackets are outstanding!\n"
52 54 "This is a sign of a memory leak.\n"
53   - "Please check your code!\n"
  55 + "Please check your code!\n",
  56 + npackets
54 57 );
55 58 }
56 59 }
@@ -63,17 +66,19 @@ AudioPacket::AudioPacket(void *src, size_t size) {
63 66 _sample_size = sd->_sample_size;
64 67 _data = (uint8_t *)malloc(_size);
65 68
  69 + leak_detect( );
  70 +
66 71 if (size < sizeof(struct sdata) + _size) {
67 72 throw std::runtime_error("Tried to deserialize invalid AudioPacket");
68 73 }
69 74
70 75 memcpy(_data, sd->_data, _size);
71   -
72   - leak_detect( );
73 76 }
74 77
75 78 AudioPacket::~AudioPacket( ) {
76   - npackets--;
  79 + //npackets--;
  80 + __sync_sub_and_fetch(&npackets, 1);
  81 +
77 82 if (_data != NULL) {
78 83 free(_data);
79 84 }

0 comments on commit 900a70d

Please sign in to comment.
Something went wrong with that request. Please try again.