Merge lp:~albaguirre/platform-api/fix-1206146 into lp:platform-api

Proposed by Alberto Aguirre
Status: Merged
Approved by: kevin gunn
Approved revision: 264
Merged at revision: 269
Proposed branch: lp:~albaguirre/platform-api/fix-1206146
Merge into: lp:platform-api
Diff against target: 345 lines (+43/-77)
2 files modified
android/default/default_ubuntu_application_sensor.cpp (+39/-57)
android/include/private/application/sensors/events.h (+4/-20)
To merge this branch: bzr merge lp:~albaguirre/platform-api/fix-1206146
Reviewer Review Type Date Requested Status
Thomas Voß (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+235066@code.launchpad.net

Commit message

Fix memory leak on sensor event notifications (LP: #1206146)

Description of the change

Fix memory leak on sensor event notifications (LP: #1206146)

The object instantiated by make_holder was leaked.

I did not see why two heap allocations and a copy where needed for an object whose lifetime is only the duration of the notification callback.

The callback receives an opaque pointer to such object and no api is provided to delete the event object provided.

So I've opted to just make a stack allocation.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Thomas Voß (thomas-voss) wrote :

LGTM.

review: Approve
Revision history for this message
Ricardo Salveti (rsalveti) wrote :

From IRC (sorry, missed your ping):
<AlbertA2> rsalveti: do I need to go through the silo process for this: https://code.launchpad.net/~albaguirre/platform-api/fix-1206146/+merge/235066 to get it merged?
<AlbertA2> rsalveti: given that it's only touching android code...
<AlbertA2> rsalveti: I'm unsure of what the process is in this situations...

Yeah, please create a silo, test, and land as a package. Once that's done, ping me and I'll sync the code on the android side.

Thanks!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'android/default/default_ubuntu_application_sensor.cpp'
2--- android/default/default_ubuntu_application_sensor.cpp 2014-05-27 07:54:20 +0000
3+++ android/default/default_ubuntu_application_sensor.cpp 2014-09-17 23:40:39 +0000
4@@ -33,21 +33,6 @@
5
6 namespace
7 {
8-template<typename T>
9-struct Holder
10-{
11- Holder(const T&value = T()) : value(value)
12- {
13- }
14-
15- T value;
16-};
17-
18-template<typename T>
19-Holder<T>* make_holder(const T& value)
20-{
21- return new Holder<T>(value);
22-}
23
24 enum sensor_value_t { MIN_DELAY, MIN_VALUE, MAX_VALUE, RESOLUTION };
25 template<ubuntu::application::sensors::SensorType sensor_type>
26@@ -56,7 +41,8 @@
27 SensorListener() : on_accelerometer_event(NULL),
28 on_proximity_event(NULL),
29 on_light_event(NULL),
30- on_orientation_event(NULL)
31+ on_orientation_event(NULL),
32+ context(nullptr)
33 {
34 }
35
36@@ -69,16 +55,15 @@
37 if (!on_orientation_event)
38 return;
39
40- ubuntu::application::sensors::OrientationEvent::Ptr ev(
41- new ubuntu::application::sensors::OrientationEvent(
42+ ubuntu::application::sensors::OrientationEvent ev(
43 reading->timestamp,
44 reading->vector[0],
45 reading->vector[1],
46- reading->vector[2])
47+ reading->vector[2]
48 );
49
50 on_orientation_event(
51- make_holder(ev), this->context
52+ &ev, this->context
53 );
54
55 break;
56@@ -88,16 +73,15 @@
57 if (!on_accelerometer_event)
58 return;
59
60- ubuntu::application::sensors::AccelerometerEvent::Ptr ev(
61- new ubuntu::application::sensors::AccelerometerEvent(
62+ ubuntu::application::sensors::AccelerometerEvent ev(
63 reading->timestamp,
64 reading->acceleration[0],
65 reading->acceleration[1],
66- reading->acceleration[2])
67+ reading->acceleration[2]
68 );
69
70 on_accelerometer_event(
71- make_holder(ev), this->context
72+ &ev, this->context
73 );
74
75 break;
76@@ -107,14 +91,13 @@
77 if (!on_proximity_event)
78 return;
79
80- ubuntu::application::sensors::ProximityEvent::Ptr ev(
81- new ubuntu::application::sensors::ProximityEvent(
82+ ubuntu::application::sensors::ProximityEvent ev(
83 static_cast<uint64_t>(reading->timestamp),
84- reading->distance)
85+ reading->distance
86 );
87
88 on_proximity_event(
89- make_holder(ev), this->context
90+ &ev, this->context
91 );
92
93 break;
94@@ -124,14 +107,13 @@
95 if (!on_light_event)
96 return;
97
98- ubuntu::application::sensors::LightEvent::Ptr ev(
99- new ubuntu::application::sensors::LightEvent(
100+ ubuntu::application::sensors::LightEvent ev(
101 reading->timestamp,
102- reading->light)
103+ reading->light
104 );
105
106 on_light_event(
107- make_holder(ev), this->context
108+ &ev, this->context
109 );
110
111 break;
112@@ -309,18 +291,18 @@
113 uas_proximity_event_get_timestamp(
114 UASProximityEvent* event)
115 {
116- auto ev = static_cast<Holder<ubuntu::application::sensors::ProximityEvent::Ptr>*>(event);
117+ auto ev = static_cast<ubuntu::application::sensors::ProximityEvent*>(event);
118
119- return ev->value->get_timestamp();
120+ return ev->get_timestamp();
121 }
122
123 UASProximityDistance
124 uas_proximity_event_get_distance(
125 UASProximityEvent* event)
126 {
127- auto ev = static_cast<Holder<ubuntu::application::sensors::ProximityEvent::Ptr>*>(event);
128+ auto ev = static_cast<ubuntu::application::sensors::ProximityEvent*>(event);
129
130- if (ev->value->get_distance() == proximity->max_value())
131+ if (ev->get_distance() == proximity->max_value())
132 return U_PROXIMITY_FAR;
133
134 return U_PROXIMITY_NEAR;
135@@ -469,8 +451,8 @@
136 uas_light_event_get_timestamp(
137 UASLightEvent* event)
138 {
139- auto ev = static_cast<Holder<ubuntu::application::sensors::LightEvent::Ptr>*>(event);
140- return ev->value->get_timestamp();
141+ auto ev = static_cast<ubuntu::application::sensors::LightEvent*>(event);
142+ return ev->get_timestamp();
143 }
144
145 UStatus
146@@ -481,8 +463,8 @@
147 if (event == NULL || value == NULL)
148 return U_STATUS_ERROR;
149
150- auto ev = static_cast<Holder<ubuntu::application::sensors::LightEvent::Ptr>*>(event);
151- *value = ev->value->get_light();
152+ auto ev = static_cast<ubuntu::application::sensors::LightEvent*>(event);
153+ *value = ev->get_light();
154
155 return U_STATUS_SUCCESS;
156 }
157@@ -629,8 +611,8 @@
158 uas_accelerometer_event_get_timestamp(
159 UASAccelerometerEvent* event)
160 {
161- auto ev = static_cast<Holder<ubuntu::application::sensors::AccelerometerEvent::Ptr>*>(event);
162- return ev->value->get_timestamp();
163+ auto ev = static_cast<ubuntu::application::sensors::AccelerometerEvent*>(event);
164+ return ev->get_timestamp();
165 }
166
167 UStatus
168@@ -641,8 +623,8 @@
169 if (event == NULL || value == NULL)
170 return U_STATUS_ERROR;
171
172- auto ev = static_cast<Holder<ubuntu::application::sensors::AccelerometerEvent::Ptr>*>(event);
173- *value = ev->value->get_x();
174+ auto ev = static_cast<ubuntu::application::sensors::AccelerometerEvent*>(event);
175+ *value = ev->get_x();
176
177 return U_STATUS_SUCCESS;
178 }
179@@ -655,8 +637,8 @@
180 if (event == NULL || value == NULL)
181 return U_STATUS_ERROR;
182
183- auto ev = static_cast<Holder<ubuntu::application::sensors::AccelerometerEvent::Ptr>*>(event);
184- *value = ev->value->get_y();
185+ auto ev = static_cast<ubuntu::application::sensors::AccelerometerEvent*>(event);
186+ *value = ev->get_y();
187
188 return U_STATUS_SUCCESS;
189 }
190@@ -669,8 +651,8 @@
191 if (event == NULL || value == NULL)
192 return U_STATUS_ERROR;
193
194- auto ev = static_cast<Holder<ubuntu::application::sensors::AccelerometerEvent::Ptr>*>(event);
195- *value = ev->value->get_z();
196+ auto ev = static_cast<ubuntu::application::sensors::AccelerometerEvent*>(event);
197+ *value = ev->get_z();
198
199 return U_STATUS_SUCCESS;
200 }
201@@ -817,8 +799,8 @@
202 uas_orientation_event_get_timestamp(
203 UASOrientationEvent* event)
204 {
205- auto ev = static_cast<Holder<ubuntu::application::sensors::OrientationEvent::Ptr>*>(event);
206- return ev->value->get_timestamp();
207+ auto ev = static_cast<ubuntu::application::sensors::OrientationEvent*>(event);
208+ return ev->get_timestamp();
209 }
210
211 UStatus
212@@ -828,9 +810,9 @@
213 {
214 if (event == NULL || value == NULL)
215 return U_STATUS_ERROR;
216-
217- auto ev = static_cast<Holder<ubuntu::application::sensors::OrientationEvent::Ptr>*>(event);
218- *value = ev->value->get_azimuth();
219+
220+ auto ev = static_cast<ubuntu::application::sensors::OrientationEvent*>(event);
221+ *value = ev->get_azimuth();
222
223 return U_STATUS_SUCCESS;
224 }
225@@ -843,8 +825,8 @@
226 if (event == NULL || value == NULL)
227 return U_STATUS_ERROR;
228
229- auto ev = static_cast<Holder<ubuntu::application::sensors::OrientationEvent::Ptr>*>(event);
230- *value = ev->value->get_pitch();
231+ auto ev = static_cast<ubuntu::application::sensors::OrientationEvent*>(event);
232+ *value = ev->get_pitch();
233
234 return U_STATUS_SUCCESS;
235 }
236@@ -857,8 +839,8 @@
237 if (event == NULL || value == NULL)
238 return U_STATUS_ERROR;
239
240- auto ev = static_cast<Holder<ubuntu::application::sensors::OrientationEvent::Ptr>*>(event);
241- *value = ev->value->get_roll();
242+ auto ev = static_cast<ubuntu::application::sensors::OrientationEvent*>(event);
243+ *value = ev->get_roll();
244
245 return U_STATUS_SUCCESS;
246 }
247
248=== modified file 'android/include/private/application/sensors/events.h'
249--- android/include/private/application/sensors/events.h 2014-05-27 06:56:28 +0000
250+++ android/include/private/application/sensors/events.h 2014-09-17 23:40:39 +0000
251@@ -28,7 +28,7 @@
252 {
253 namespace sensors
254 {
255-class OrientationEvent : public platform::ReferenceCountedBase
256+class OrientationEvent
257 {
258 public:
259 OrientationEvent(uint64_t timestamp, float azimuth, float pitch, float roll)
260@@ -38,8 +38,6 @@
261 roll(roll)
262 {}
263
264- typedef ubuntu::platform::shared_ptr<OrientationEvent> Ptr;
265-
266 uint64_t get_timestamp()
267 {
268 return this->timestamp;
269@@ -56,13 +54,11 @@
270 float roll;
271
272 protected:
273- virtual ~OrientationEvent() {}
274-
275 OrientationEvent(const OrientationEvent&) = delete;
276 OrientationEvent& operator=(const OrientationEvent&) = delete;
277 };
278
279-class AccelerometerEvent : public platform::ReferenceCountedBase
280+class AccelerometerEvent
281 {
282 public:
283 AccelerometerEvent(uint64_t timestamp, float x, float y, float z)
284@@ -72,8 +68,6 @@
285 z(z)
286 {}
287
288- typedef ubuntu::platform::shared_ptr<AccelerometerEvent> Ptr;
289-
290 uint64_t get_timestamp()
291 {
292 return this->timestamp;
293@@ -90,20 +84,16 @@
294 float z;
295
296 protected:
297- virtual ~AccelerometerEvent() {}
298-
299 AccelerometerEvent(const AccelerometerEvent&) = delete;
300 AccelerometerEvent& operator=(const AccelerometerEvent&) = delete;
301 };
302
303-class ProximityEvent : public platform::ReferenceCountedBase
304+class ProximityEvent
305 {
306 public:
307 ProximityEvent(uint64_t timestamp, float distance) : timestamp(timestamp),
308 distance(distance)
309 {}
310-
311- typedef ubuntu::platform::shared_ptr<ProximityEvent> Ptr;
312
313 uint64_t get_timestamp()
314 {
315@@ -117,20 +107,16 @@
316 float distance;
317
318 protected:
319- virtual ~ProximityEvent() {}
320-
321 ProximityEvent(const ProximityEvent&) = delete;
322 ProximityEvent& operator=(const ProximityEvent&) = delete;
323 };
324
325-class LightEvent : public platform::ReferenceCountedBase
326+class LightEvent
327 {
328 public:
329 LightEvent(uint64_t timestamp, float light) : timestamp(timestamp),
330 light(light)
331 {}
332-
333- typedef ubuntu::platform::shared_ptr<LightEvent> Ptr;
334
335 uint64_t get_timestamp()
336 {
337@@ -144,8 +130,6 @@
338 float light;
339
340 protected:
341- virtual ~LightEvent() {}
342-
343 LightEvent(const LightEvent&) = delete;
344 LightEvent& operator=(const LightEvent&) = delete;
345 };

Subscribers

People subscribed via source and target branches