Post

Replies

Boosts

Views

Activity

UnsafeMutablePointer direct exposure.
I am trying to use the swift type UnsafeMutablePointer directly in C++. According to the documentation mentioned, swift expose this type to C++. But I am not able to use it . void GetPointerFromSwift () { // Calls a swift function to get a pointer. swift::UnsafeMutablePointer<swit::Int> x = Interop::GetPointer () }
7
0
600
Sep ’24
Network Name (local domain Name) of a Mac (Mac-OS)
I want to get the network-name (domain-name) on my Mac-Machine. Where iin the Settings does this domain name gets configured. I refer to this page which talks about computer name and host name, I could find where my hostname is present (Settings-&amp;gt;General-&amp;gt;Sharing-&amp;gt;local host name) but not anything related to the network-name (local -domain) . Even try to fetch this info using the linux api to getdomainname, api call succeeded but it returns Nothing. #include &amp;lt;iostream&amp;gt; #include &amp;lt;unistd.h&amp;gt; #include &amp;lt;limits.h&amp;gt; #include &amp;lt;cstring&amp;gt; int main() { char domainname[255]; // Get the domain name if (getdomainname(domainname, 255) != 0) { std::cout &amp;lt;&amp;lt; "Error getting domain name" &amp;lt;&amp;lt; std::endl; return 1; } std::cout &amp;lt;&amp;lt; "Domain name: " &amp;lt;&amp;lt; domainname &amp;lt;&amp;lt; std::endl; return 0; } Output Domain name: I even came across Search-Domains, Does it have anything to do with the network-name (domain name of the machine)?
5
0
1.4k
Apr ’24
How ARC works withUnsafeMutablePointer Interface.
I am using withUnsafeMutablePointer to get raw pointer from Data. Once I get a Pointer I follow these steps: I create a Wrapper using that Pointer. After that I increase it ARC and pass it as a opaque to C++ When I was on First Step if my thread get suspended and after some time if it resumes then is there a possibility that the Memory will get freed due to ARC. Adding basic Code Flow depicting what i am doing. public class DataHolder { public init () {} public var data_wrapper : Data? } func InternalReceiveHandler (_ pContent : Data?) -> Void { var holder : DataHolder = DataHolder.init () withUnsafeMutablePointer (to : &pContent) { data_pointer in holder.data_wrapper = Data.init (noBytesCopy : data_pointer, count : no_of_bytes, deallocator : .none) return Unmanaged.passRetained (holder).toOpaque () } } Is there a possibility that when I am creating the wrapper my thread get suspended and when it get resumed the Memory the pointer was pointing can be freed by ARC.
5
0
786
Aug ’24
NWConnection cancel: Do we need to wait for pending receive callbacks to be cancelled?
Hi, I’m using Network Framework to implement a UDP client via NWConnection, and I’m looking for clarification about the correct and fully safe shutdown procedure, especially regarding resource release. I have initiated some pending receive calls on the NWConnection (using receive). After calling connection.cancel(), do we need to wait for the cancellation of these pending receives? As mentioned in this thread, NWConnection retains references to the receive closures and releases them once they are called. If a receive closure holds a reference to the NWConnection itself, do we need to wait for these closures to be called to avoid memory leaks? Or, if there are no such retained references, we don't need to wait for the cancellation of the pending I/O and cancelled state for NWConnection?
5
0
126
1w
NWListener cancelation semantics for UDP: Do we need to wait for .cancelled state? Should newConnectionHandler be set to nil?
Hi, I’m using Network Framework to implement a UDP listener via NWListener. I am looking for clarification about the correct and fully safe shutdown procedure, especially regarding resource release. After calling listener.cancel(), do we need to wait for the .cancelled state before exiting the application? Or can we just exit once the cancellation is initiated, assuming the OS will close the NWListener and there will be no resource leak? Is it recommended (or required) to set newConnectionHandler = nil when shutting down a UDP listener? My understanding is that if there is no NWListener attached, then whenever a connection is accepted by the OS, it will not be delivered to the application and the OS will simply drop it.
3
0
79
1w
Returning Typed Pointer From Swift to C++
I have a struct defined in Swift, i want to pass it's instance pointer from swift to C++. When I am trying to directly return Typed Pointer from Swift Function to C++, the function doesn't get expose to C++. Code which i have tried. // Defined Structure public struct MyStruct { public init (_ pValue : Int) { uValue = pValue } public var uValue : Int } var my_struct = MyStruct(20) // Function which returns Struct Pointer to C++ // When I return typed pointer this function doesn't get exposed to C++ public func PassStructPointer () -> UnsafeMutablePointer<MyStruct> { withUnsafeMutablePointer(to: &my_struct) { pointer in return pointer } } But when I pass UnsafeRawMutablePointer instead of type pointer then the function does get expose to C++ var my_struct = MyStruct(20) // This get expose to C++. public func PassStructPointer () -> UnsafeMutableRawPointer { return withUnsafeMutableBytes(of: &my_struct) { pointer in return pointer.baseAddress! } } Can we not pass typed pointer of the types defined by us?
2
0
628
Sep ’24
Passing C++ Types as Reference using SWIFT_IMMORTAL_REFERENCE.
I have a Class defined in C++, I want to pass the instance of class from C++ to Swift as a reference type. By default swift maps C++ classes as value types but we can change this behavior by using SWIFT_IMMORTAL_REFERENCE annotation mentioned here. The example mentioned here is of Singelton class but I have a usecase where i require more than one instance. Cpp Class Skeleton. class Cpp { public: void Print () noexcept; void SetValue (int pValue) noexcept; // Method which is Invoked by Swift. static Cpp& ReturnObj () noexcept; private: int vValue; } SWIFT_IMMORTAL_REFERENCE; Definition of Return Obj Cpp& Cpp::ReturnObj () noexcept { static Cpp obj; return obj; } Swift Co var obj : Cpp = Cpp.ReturnObj() withUnsafeBytes(of: &obj) {(pointer : UnsafeRawBufferPointer) in print (pointer) print (pointer.baseAddress!) } Output Address Printed by C++ 0x100008000 Address Printed by Swift 0x00007ff7bfeff108 So from the above observation copy is passed. How to do pass by reference then?
2
0
740
Sep ’24
How can we get FQDN (Fully Qualified Domain Name) in IOS and What Api to Use?
I have a use-case were I want to use the the FQDN (Fully Qualified Domain Name) in IOS-Device, which can be used to connect to a Device instead of using the IP-Address. FQDN will be consisting of the machine-name or host-name (Most common term) and the domain-name of the network i.e network-name (local domain assigned to that device). Which IOS Api can be used Here?
1
0
1k
Apr ’24
Understanding how ARC is different for Struct vs Class
Hi, I was trying to understand how swift manages it memory just wanted to verify my understanding on it. For Value Types i.e. Struct ARC (Automatic Reference Counting) is not there, Memory is Managed/Confined on the basis of scope of that Variable. And For Struct whenver we do assignment a Copy is been created. For Classes, Swift Manages Memroy with the help of ARC i.e. whenever I create a instance of class its reference count get increased and when we assign same instance to new variable then it also result in increment of Reference Count. The Memory will get deallocated when all the variables pointing to that object are no longer in use.
1
0
845
Aug ’24
Passing User Defined Swift Structure to C++ using In Param
I have a Usecase where I want to pass user-defined swift structure instance from Swift to C++ as argument to the C++ Function. In the documentation it's mentioned that swift exposes these structures to c++. Swift Structure. public struct MyStruct { public init (_ pValue : Int) { uValue = pValue } public var uValue : Int } I am able to Create Instance in C++ . Code void CppClass::CreateSwiftStruct () { Interop::MyStruct my_struct = Interop::MyStruct::init (20); } But when I define a C++ Function which takes Interop::MyStruct as argument then that function doesn't get exposed to swift, so i am not able to call it. Skeleton For CppClass class CppClass { static void PassStruct (Interop::MyStruct pStruct); static void Test (); } Here PassStruct Method doesn't get exposed to C++ but Test does. How can I pass Struct Instance in swift to C++ Function as In Param?
1
0
564
Sep ’24