Skip to content

Commit

Permalink
mem-leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Sep 12, 2019
1 parent f109b37 commit a162eb4
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 89 deletions.
2 changes: 1 addition & 1 deletion Blink.xcodeproj/xcshareddata/xcschemes/Blink.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableASanStackUseAfterReturn = "YES"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
stopOnEveryMainThreadCheckerIssue = "YES"
migratedStopOnEveryIssue = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
Expand Down
1 change: 1 addition & 0 deletions Blink/Repl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@

- (int)clear_main:(int)argc argv:(char **)argv;
- (int)history_main:(int)argc argv:(char **)argv;
- (void)forceExit;
@end
9 changes: 7 additions & 2 deletions Blink/Repl.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ @implementation Repl {
Replxx* _replxx;
__weak TermDevice *_device;
__weak TermStream *_stream;
BOOL _forceExit;
}

void __hints(char const* line, int bp, replxx_hints* lc, ReplxxColor* color, void* ud) {
Expand Down Expand Up @@ -543,7 +544,7 @@ - (void)loopWithCallback:(BOOL(^)(NSString *cmd)) callback
break;
}

if (!_stream) {
if (_forceExit) {
return;
}

Expand All @@ -556,7 +557,7 @@ - (void)loopWithCallback:(BOOL(^)(NSString *cmd)) callback
}
});

if (!_stream) {
if (_forceExit) {
return;
}

Expand Down Expand Up @@ -659,6 +660,10 @@ - (int)history_main:(int)argc argv:(char **)argv
return 0;
}

- (void)forceExit {
_forceExit = YES;
}




Expand Down
11 changes: 4 additions & 7 deletions Blink/SessionRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SessionMeta: Codable {
}

protocol SuspendableSession: class {
var sessionRegistry: SessionRegistry? { get set }
// var sessionRegistry: SessionRegistry? { get set }
var meta: SessionMeta { get }
init(meta: SessionMeta?)
func resume(with unarchiver: NSKeyedUnarchiver)
Expand All @@ -48,11 +48,11 @@ protocol SuspendableSession: class {

extension SuspendableSession {
func suspendIfNeeded() {
sessionRegistry?.suspendIfNeeded(session: self)
SessionRegistry.shared.suspendIfNeeded(session: self)
}

func resumeIfNeeded() {
sessionRegistry?.resumeIfNeeded(session: self)
SessionRegistry.shared.resumeIfNeeded(session: self)
}
}

Expand All @@ -73,7 +73,6 @@ extension SuspendableSession {
let key = meta.key
_metaIndex[key] = meta
_sessionsIndex[key] = session
session.sessionRegistry = self
}

subscript<T: SuspendableSession>(key: UUID) -> T {
Expand All @@ -100,11 +99,9 @@ extension SuspendableSession {
}

func remove(forKey key: UUID) {
if let session = _sessionsIndex.removeValue(forKey: key) {
session.sessionRegistry = nil
}
_metaIndex.removeValue(forKey: key)
_fsRemove(forKey: key)
_sessionsIndex.removeValue(forKey: key)
}

func remove(session: SuspendableSession?) {
Expand Down
9 changes: 5 additions & 4 deletions Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ public class SpaceController: UIViewController {

SessionRegistry.shared.track(session: term)

self._currentKey = term.meta.key
_viewportsController.setViewControllers([term], direction: .forward, animated: animated) { (didComplete) in
DispatchQueue.main.async {
self._currentKey = term.meta.key
self._displayHUD()
self._attachInputToCurrentTerm()
if let completion = completion {
Expand All @@ -255,7 +255,7 @@ public class SpaceController: UIViewController {
else {
return
}

currentTerm()?.delegate = nil
SessionRegistry.shared.remove(forKey: currentKey)
_viewportsKeys.remove(at: idx)
if _viewportsKeys.isEmpty {
Expand All @@ -274,9 +274,10 @@ public class SpaceController: UIViewController {
term = SessionRegistry.shared[_viewportsKeys[idx - 1]]
}
term.bgColor = view.backgroundColor ?? .black


self._currentKey = term.meta.key

_viewportsController.setViewControllers([term], direction: direction, animated: true) { (didComplete) in
self._currentKey = term.meta.key
self._displayHUD()
self._attachInputToCurrentTerm()
}
Expand Down
19 changes: 10 additions & 9 deletions Blink/TermController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import UIKit
}

class TermController: UIViewController {
private weak var _sessionRegistry: SessionRegistry? = nil
private let _meta: SessionMeta

private var _termDevice = TermDevice()
Expand Down Expand Up @@ -141,6 +140,7 @@ class TermController: UIViewController {
}

@objc public func terminate() {
_termDevice.delegate = nil
_termView.terminate()
_session?.kill()
}
Expand Down Expand Up @@ -185,10 +185,12 @@ class TermController: UIViewController {

deinit {
NotificationCenter.default.removeObserver(self)
_termDevice.attachView(nil)
_session?.device = nil
_session?.stream = nil
_session?.delegate = nil
_session = nil
// _termDevice.attachView(nil)
// _session?.device = nil
// _session?.stream = nil
// _session = nil
}

}
Expand All @@ -200,6 +202,7 @@ extension TermController: SessionDelegate {

public func sessionFinished() {
if _sessionParams.hasEncodedState() {
_session?.delegate = nil
_session = nil
return
}
Expand Down Expand Up @@ -242,18 +245,16 @@ extension TermController: TermDeviceDelegate {

extension TermController: SuspendableSession {

var sessionRegistry: SessionRegistry? {
get { _sessionRegistry }
set { _sessionRegistry = newValue }
}

var meta: SessionMeta { _meta }

var _decodableKey: String { "params" }

func startSession() {
guard _session == nil
else {
if view.bounds.size != _sessionParams.viewSize {
_session?.sigwinch()
}
return
}

Expand Down
6 changes: 3 additions & 3 deletions Blink/TermDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@

@end

@interface TermDevice : NSObject
{
@interface TermDevice : NSObject {
@public struct winsize win;
}

@property (readonly) TermStream *stream;
@property (readonly) TermView *view;
@property (readonly) TermInput *input;
@property (weak) id<TermDeviceDelegate> delegate;
@property id<TermDeviceDelegate> delegate;
@property (nonatomic) BOOL rawMode;
@property (nonatomic) BOOL echoMode;
@property (nonatomic) BOOL secureTextEntry;
Expand All @@ -68,6 +67,7 @@
- (void)blur;

- (void)write:(NSString *)input;
- (void)writeIn:(NSString *)input;
- (void)writeOut:(NSString *)output;
- (void)writeOutLn:(NSString *)output;
- (void)close;
Expand Down
6 changes: 6 additions & 0 deletions Blink/TermDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ - (void)write:(NSString *)input
}
}

- (void)writeIn:(NSString *)input
{
[self write:input];
}


- (void)writeOut:(NSString *)output {
fprintf(_stream.out, "%s", output.UTF8String);
}
Expand Down
2 changes: 1 addition & 1 deletion Blink/TermInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@

@property BOOL softwareKB;

@property TermDevice *device;
@property (weak) TermDevice *device;

- (void)deviceWrite:(NSString *)input;
- (void)copyLink:(id)sender;
Expand Down
3 changes: 2 additions & 1 deletion Blink/TermInput.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ + (NSString *)CTRL:(NSString *)c
return code;
} else {
char x = [c characterAtIndex:0];
return [NSString stringWithFormat:@"%c", x - 'a' + 1];
NSString *str = [NSString stringWithFormat:@"%c", x - 'a' + 1];
return str;
}
}

Expand Down
7 changes: 7 additions & 0 deletions Blink/TermView.m
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,15 @@ - (void)setIme:(NSString *)imeText completionHandler:(void (^ _Nullable)(_Nullab

- (void)terminate
{
_device = nil;
// Disconnect message handler
[_webView.configuration.userContentController removeScriptMessageHandlerForName:@"interOp"];
}

- (void)dealloc {
[self terminate];
[_layoutDebounceTimer invalidate];
_layoutDebounceTimer = nil;
}

@end
50 changes: 32 additions & 18 deletions Sessions/MCPSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,24 @@ @implementation WeakSSHClient


@implementation MCPSession {
int _sessionNum;
Session *_childSession;
NSString *_currentCmd;
NSMutableArray<WeakSSHClient *> *_sshClients;
}

static int _sessionNum = 0;
static int sessionNum() {
_sessionNum ++;
return _sessionNum;
}

@dynamic sessionParams;

- (id)initWithDevice:(TermDevice *)device andParams:(MCPParams *)params {
if (self = [super initWithDevice:device andParams:params]) {
_sshClients = [[NSMutableArray alloc] init];
_sessionNum = sessionNum();
}

return self;
Expand Down Expand Up @@ -228,20 +236,31 @@ - (void)sigwinch

- (void)kill
{
[_childSession kill];

ios_kill();

// Instruct ios_system to release the data for this shell:
ios_closeSession((__bridge void*)self);
ios_setContext(nil);
[_repl forceExit];
ios_switchSession((void *)_sessionNum);

if (_device.stream.in) {
fclose(_device.stream.in);
_device.stream.in = NULL;
if (_sshClients.count > 0) {
for (WeakSSHClient *client in _sshClients) {
[client.value kill];
}
} else {
if (_device.stream.in) {
fclose(_device.stream.in);
_device.stream.in = NULL;
}
}
_repl = nil;
_sshClients = nil;

if (_childSession) {
[_childSession kill];
} else {
ios_kill();
}

ios_closeSession((void *)_sessionNum);



[_device writeIn:@"\x03"];
}

- (void)suspend
Expand Down Expand Up @@ -279,16 +298,11 @@ - (void)setActiveSession {
stdout = _stream.out;
stderr = _stream.err;
stdin = _stream.in;
ios_switchSession((__bridge void*)self);
ios_switchSession((void *)_sessionNum);
stdout = savedStdOut;
stderr = savedStdErr;
stdin = savedStdIn;
}

- (void)dealloc {
_repl = nil;
_childSession = nil;
_sshClients = nil;
}

@end
3 changes: 3 additions & 0 deletions Sessions/MoshSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,9 @@ - (void)sigwinch

- (void)kill
{
// char ctrl6 = '6' - 'a' + 1;
[_device writeIn:@"\x1e\x2e"];
// [_device writeIn:[NSString stringWithFormat:@"%c.", ctrl6]];
pthread_kill(_tid, SIGINT);
}

Expand Down
9 changes: 1 addition & 8 deletions Sessions/Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@

@class SessionParams;

typedef struct SessionArgs {
CFTypeRef session;
const char *args;
bool attached;
} SessionArgs;


@protocol SessionDelegate

- (void)sessionFinished;
Expand All @@ -61,7 +54,7 @@ typedef struct SessionArgs {
@property (strong) TermStream *stream;
@property (strong) TermDevice *device;

@property (weak) NSObject<SessionDelegate>* delegate;
@property (weak) id<SessionDelegate> delegate;

- (id)init __unavailable;
- (id)initWithDevice:(TermDevice *)device andParams:(SessionParams *)params;
Expand Down
Loading

0 comments on commit a162eb4

Please sign in to comment.