当我在下面的代码中调用FPlatformNetworkState := TPlatformNetworkState.Create;
时,我收到了段错误。
我对segfault 11的了解是,当我尝试访问我不允许的内存(类似于Windows中的AccessViolation)时会发生这种情况。但是,由于我正在创建一个新实例,不应该分配新的内存吗?
我应该如何创建它以便它不会访问不应该存在的内存?
完整单位:
unit UNetworkState;
{From http://delphi.radsoft.com.au/2013/11/checking-for-an-internet-connection-on-mobile-devices-with-delphi-xe5/ (16-2-2015)}
interface
type
TCustomNetworkState = class(TObject)
function CurrentSSID: string; virtual; abstract;
function IsConnected: Boolean; virtual; abstract;
function IsWifiConnected: Boolean; virtual; abstract;
function IsMobileConnected: Boolean; virtual; abstract;
end;
TNetworkState = class(TCustomNetworkState)
private
FPlatformNetworkState: TCustomNetworkState;
public
constructor Create;
destructor Destroy; override;
function CurrentSSID: string;
function IsConnected: Boolean; override;
function IsWifiConnected: Boolean; override;
function IsMobileConnected: Boolean; override;
end;
implementation
uses
{$IFDEF IOS}
//NetworkState.iOS;
{$ENDIF}
{$IFDEF ANDROID}
UNetworkStateAndroid;
{$ENDIF}
{ TNetworkState }
constructor TNetworkState.Create;
begin
inherited;
FPlatformNetworkState := TPlatformNetworkState.Create;
end;
destructor TNetworkState.Destroy;
begin
FPlatformNetworkState.Free;
inherited;
end;
function TNetworkState.CurrentSSID: string;
begin
Result := FPlatformNetworkState.CurrentSSID;
end;
function TNetworkState.IsConnected: Boolean;
begin
Result := FPlatformNetworkState.IsConnected;
end;
function TNetworkState.IsMobileConnected: Boolean;
begin
Result := FPlatformNetworkState.IsMobileConnected;
end;
function TNetworkState.IsWifiConnected: Boolean;
begin
Result := FPlatformNetworkState.IsWifiConnected;
end;
end.
调用create的表单:
unit UMainForm;
interface
uses
System.SysUtils,
System.Types,
System.UITypes,
System.Classes,
System.Variants,
FMX.Types,
FMX.Graphics,
FMX.Controls,
FMX.Forms,
FMX.Dialogs,
FMX.TabControl,
FMX.StdCtrls,
FMX.Gestures;
type
TappMainForm = class(TForm)
HeaderToolBar: TToolBar;
ToolBarLabel: TLabel;
TabControl1: TTabControl;
TabItem1: TTabItem;
TabItem2: TTabItem;
TabItem3: TTabItem;
TabItem4: TTabItem;
GestureManager1: TGestureManager;
NotificationsPanel: TPanel;
procedure FormCreate(Sender: TObject);
procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
var Handled: Boolean);
procedure EmbedForm(ArgParent : TControl; ArgForm : TCustomForm);
private
{ Private declarations }
public
{ Public declarations }
end;
var
appMainForm: TappMainForm;
implementation
{$R *.fmx}
uses
UNetworkState;
procedure TappMainForm.FormCreate(Sender: TObject);
var
NS : TNetworkState;
begin
{ This defines the default active tab at runtime }
TabControl1.ActiveTab := TabItem1;
NS.Create;
try
if not NS.IsConnected then begin
ShowMessage('No network connection detected.'#13#10'Please connect to the internet in your device''s settings.');
end else if NS.IsWifiConnected then begin
ShowMessage('WiFi connection detected.');
end else if NS.IsMobileConnected then begin
ShowMessage('Mobile connection detected.');
end;
finally
NS.Free;
end;
end;
procedure TappMainForm.FormGesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
begin
{$IFDEF ANDROID}
case EventInfo.GestureID of
sgiLeft:
begin
if TabControl1.ActiveTab <> TabControl1.Tabs[TabControl1.TabCount-1] then
TabControl1.ActiveTab := TabControl1.Tabs[TabControl1.TabIndex+1];
Handled := True;
end;
sgiRight:
begin
if TabControl1.ActiveTab <> TabControl1.Tabs[0] then
TabControl1.ActiveTab := TabControl1.Tabs[TabControl1.TabIndex-1];
Handled := True;
end;
end;
{$ENDIF}
end;
end.
答案 0 :(得分:3)
在您的表单代码中,调用NS.Create
是错误的。它需要NS := TNetworkState.Create
代替。