Skip to content

Commit cf4dace

Browse files
Fix crash in Linux platform channel example. (#155735)
When running this example it was crashing, so investigated a fixed a couple of bugs.
1 parent bff1351 commit cf4dace

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

examples/platform_channel/linux/my_application.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ static void update_charging_state(MyApplication* self) {
4242

4343
const gchar* charge_event = "discharging";
4444
for (guint i = 0; i < self->battery_devices->len; i++) {
45-
UpDevice* device =
46-
static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i));
45+
UpDevice* device = UP_DEVICE(g_ptr_array_index(self->battery_devices, i));
4746

4847
guint state;
4948
g_object_get(device, "state", &state, nullptr);
@@ -97,8 +96,7 @@ static void up_device_removed_cb(MyApplication* self, UpDevice* device) {
9796
static FlMethodResponse* get_battery_level(MyApplication* self) {
9897
// Find the first available battery and use that.
9998
for (guint i = 0; i < self->battery_devices->len; i++) {
100-
UpDevice* device =
101-
static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i));
99+
UpDevice* device = UP_DEVICE(g_ptr_array_index(self->battery_devices, i));
102100

103101
double percentage;
104102
g_object_get(device, "percentage", &percentage, nullptr);
@@ -225,8 +223,7 @@ static void my_application_activate(GApplication* application) {
225223
g_autoptr(GPtrArray) devices = up_client_get_devices(self->up_client);
226224
#endif
227225
for (guint i = 0; i < devices->len; i++) {
228-
g_autoptr(UpDevice) device =
229-
static_cast<UpDevice*>(g_ptr_array_index(devices, i));
226+
UpDevice* device = UP_DEVICE(g_ptr_array_index(devices, i));
230227
up_device_added_cb(self, device);
231228
}
232229

@@ -268,8 +265,7 @@ static void my_application_dispose(GObject* object) {
268265
MyApplication* self = MY_APPLICATION(object);
269266

270267
for (guint i = 0; i < self->battery_devices->len; i++) {
271-
UpDevice* device =
272-
static_cast<UpDevice*>(g_ptr_array_index(self->battery_devices, i));
268+
UpDevice* device = UP_DEVICE(g_ptr_array_index(self->battery_devices, i));
273269
g_signal_handlers_disconnect_matched(device, G_SIGNAL_MATCH_DATA, 0, 0,
274270
nullptr, nullptr, self);
275271
}
@@ -278,7 +274,7 @@ static void my_application_dispose(GObject* object) {
278274

279275
g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
280276
g_clear_object(&self->up_client);
281-
g_clear_object(&self->battery_devices);
277+
g_clear_pointer(&self->battery_devices, g_ptr_array_unref);
282278
g_clear_object(&self->battery_channel);
283279
g_clear_object(&self->charging_channel);
284280
g_clear_pointer(&self->last_charge_event, g_free);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:platform_channel/main.dart' as platform_channel;
7+
8+
void main() {
9+
testWidgets('Platform channel smoke test', (WidgetTester tester) async {
10+
platform_channel.main(); // builds the app and schedules a frame but doesn't trigger one
11+
await tester.pump(); // triggers a frame
12+
13+
expect(find.textContaining('Battery level: '), findsOneWidget);
14+
});
15+
}

0 commit comments

Comments
 (0)