Skip to content

Commit 9536f85

Browse files
committed
fix: demo crash
1 parent d90dfc8 commit 9536f85

File tree

6 files changed

+8
-34
lines changed

6 files changed

+8
-34
lines changed

dart_native/example/lib/ios/ios_main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ class _IOSAppState extends State<IOSApp> {
2323
Future<void> initPlatformState() async {
2424
RuntimeSon stub = RuntimeSon();
2525
DelegateStub delegate = DelegateStub();
26-
testIOS(stub, delegate);
2726
// Benchmark
2827
String testString =
2928
'This is a long string: sdlfdksjflksndhiofuu2893873(*(%¥#@)*&……¥撒肥料开发时傅雷家书那份会计师东方丽景三等奖';
@@ -41,6 +40,7 @@ class _IOSAppState extends State<IOSApp> {
4140
}
4241

4342
print("DartNative Cost: ${DateTime.now().millisecondsSinceEpoch - time}");
43+
testIOS(stub, delegate);
4444
}
4545

4646
@override

dart_native/example/lib/ios/unit_test.dart

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,24 +154,15 @@ testIOS(RuntimeStub stub, DelegateStub delegate) {
154154
NSNotificationCenter.defaultCenter.addObserver(
155155
delegate, delegate.handleNotification, 'SampleDartNotification', nil);
156156

157-
Isolate.spawn(_checkTimer, stub.pointer.address);
158-
Isolate.spawn(_checkTimer1, stub.pointer.address);
157+
Isolate.spawn(_checkTimer, 'isolate0');
158+
Isolate.spawn(_checkTimer, 'isolate1');
159159
}
160160

161-
void _checkTimer(int addr) async {
162-
RuntimeStub stub = RuntimeStub.fromPointer(Pointer.fromAddress(addr));
161+
void _checkTimer(String isolateID) async {
162+
RuntimeStub stub = RuntimeStub();
163163
Timer.periodic(new Duration(seconds: 1), (Timer t) {
164164
stub.fooCompletion(() {
165-
print('hello completion block on another isolate!');
166-
});
167-
});
168-
}
169-
170-
void _checkTimer1(int addr) async {
171-
RuntimeStub stub = RuntimeStub.fromPointer(Pointer.fromAddress(addr));
172-
Timer.periodic(new Duration(seconds: 1), (Timer t) {
173-
stub.fooCompletion(() {
174-
print('hello completion block on third isolate!');
165+
print('hello completion block on $isolateID!');
175166
});
176167
});
177168
}

dart_native/ios/Classes/DNObjectDealloc.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,13 @@ - (instancetype)initWithHost:(NSObject *)host
6262
if (self) {
6363
_host = host;
6464
_hostAddress = (int64_t)host;
65-
// if ([host isKindOfClass:NSClassFromString(@"Fuck")]) {
66-
// fuxk = _hostAddress;
67-
// }
6865
_dartPort = dartPort;
6966
objc_setAssociatedObject(host, _cmd, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
7067
}
7168
return self;
7269
}
7370

7471
- (void)dealloc {
75-
// if (_hostAddress == fuxk) {
76-
//
77-
// }
7872
NotifyDeallocToDart(_hostAddress, _dartPort);
7973
}
8074

312 Bytes
Binary file not shown.

dart_native/lib/src/ios/runtime/internal/nsobject_lifecycle.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import 'dart:ffi';
22

33
import 'package:dart_native/src/ios/common/callback_manager.dart';
44
import 'package:dart_native/src/ios/common/library.dart';
5-
import 'package:dart_native/src/ios/runtime/class.dart';
65
import 'package:dart_native/src/ios/runtime/internal/block_lifecycle.dart';
76
import 'package:dart_native/src/ios/runtime/internal/native_runtime.dart';
87
import 'package:dart_native/src/ios/runtime/nsobject.dart';
9-
import 'package:ffi/ffi.dart';
108

119
void passObjectToNative(NSObject obj) {
1210
// Ignore null and nil
@@ -31,9 +29,6 @@ void _dealloc(Pointer<Void> ptr) {
3129
});
3230
_finalizerMap.remove(ptr);
3331
}
34-
if (isolateDeadObject.finalizer == null) {
35-
isolateDeadObject.finalizer = () {};
36-
}
3732
}
3833

3934
Map<Pointer<Void>, List<Finalizer>> _finalizerMap = {};
@@ -57,11 +52,3 @@ removeFinalizerForObject(NSObject obj) {
5752

5853
Pointer<NativeFunction<Void Function(Pointer<Void>)>> nativeObjectDeallocPtr =
5954
Pointer.fromFunction(_dealloc);
60-
61-
final Fuck isolateDeadObject = Fuck();
62-
63-
class Fuck extends NSObject {
64-
Fuck([Class isa]) : super(isa ?? Class('Fuck', Class('NSObject')));
65-
66-
Fuck.fromPointer(Pointer<Void> ptr) : super.fromPointer(ptr);
67-
}

dart_native/lib/src/ios/runtime/nsobject.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class NSObject extends id {
2626
passObjectToNative(this);
2727
}
2828

29+
/// Before call [fromPointer], MAKE SURE the [ptr] for object exists.
30+
/// If [ptr] was already freed, you would get a crash!
2931
NSObject.fromPointer(Pointer<Void> ptr) : super(ptr) {
3032
if (ptr == null || object_isClass(ptr) != 0) {
3133
throw 'Pointer $ptr is not for NSObject!';

0 commit comments

Comments
 (0)