public void NativeToManagedTest() { NativeLibraries.Load(); // An easy way to invoke the marshaler is through the use of // plist_new_string, which creates a new object with a string value, // and plist_get_string_val, which gets that object's value. var plist = new PlistApi(); /* Allocate a value worth 10 MB */ string input = new string('x', 10 * 1024 * 1024); var handle = plist.plist_new_string(input); GC.Collect(); var p = Process.GetCurrentProcess(); var initialMemory = p.PrivateMemorySize64; for (int i = 0; i < 75; i++) { string output; // Every call will allocate a new string. If it's not properly cleaned up // by our marshaler, we will leak 10 MB in each call, or 750 MB in total. plist.plist_get_string_val(handle, out output); Assert.AreEqual(input, output); } handle.Dispose(); GC.Collect(); p.Refresh(); var currentMemory = p.PrivateMemorySize64; var delta = currentMemory - initialMemory; // If more than 10 MB was leaked, set off the alarm bells // You can verify this works by commenting out NativeStringMarshaler.CleanUpNativeData if (delta > 10 * 1024 * 1024 * 8 /* 10 MB */) { Assert.Fail("Memory was leaked"); } }
private bool HookDevice() { // Don't do anything if a device already connected. //if (DeviceConnected) return; // Load native imobiledevice shit.. NativeLibraries.Load(); bool result = false; ReadOnlyCollection <string> _pluggedDevices; int pluggedDevicesCount = 0; try { DeviceState = (iDeviceApi)LibiMobileDevice.Instance.iDevice; LockDownState = (LockdownApi)LibiMobileDevice.Instance.Lockdown; // Try to connect to any connected iDevice... DeviceState.idevice_get_device_list(out _pluggedDevices, ref pluggedDevicesCount); // Populate the list with found iDevices and then put // the said devices to our global list. pluggedDevices = _pluggedDevices; // Check if any device is present, if no do nothing but display message. if (pluggedDevices.Count <= 0) { // Not actually an error in our case. MessageBox.Show("No iDevice found!", "No iDevice connected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); ReleaseDevice(); result = false; } // Since we only plan (and only supports) 1 device at a time, check. else if (pluggedDevices.Count > 1) { MessageBox.Show("Please only connect one iDevice at a time!", "Multiple iDevices detected", MessageBoxButtons.OK, MessageBoxIcon.Error); result = false; } // Etasblish connection to found device. if (pluggedDevices.Count > 0) { // Connect to the first found device... var thisDevice = pluggedDevices[0]; DeviceState.idevice_new(out deviceHandle, thisDevice).ThrowOnError(); LockDownState.lockdownd_client_new_with_handshake(deviceHandle, out lockdownHandle, "Quamotion").ThrowOnError(); PlistReader = new PlistApi(LibiMobileDevice.Instance); DeviceConnected = (LockDownState != null ? true : false); if (DeviceConnected) { ParseDevice(); result = true; } } } catch (LockdownException) { ReleaseDevice(); throw new LockdownException("Couldn't connect to iDevice. Please make sure your device has trusted this pc."); } return(result); }