# Closing a switch handle

swiCloseSwitch closes a switch handle
, releases the associated switching context, and closes the MVIP switching device. Switching calls that are passed to a closed switch handle or an uninitialized switch handle return CTAERR_INVALID_HANDLE.

If the switch handle was opened with the SWI_ENABLE_RESTORE flag set in swiOpenSwitch, switch block
outputs affected by switching calls (using this handle) are restored to the state they were in when swiOpenSwitch was called.

If switch objects are shared, applications must coordinate switch closing. Switching service functions are synchronous
; therefore, no events are generated. When a switch object is shared between Natural Access clients, the clients must take responsibility for notifying peer clients of actions taken on the switch object that affect the peer's continued use of the switch. For example, when a switch object is destroyed within the service manager through a call to swiCloseSwitch, peer clients with open handles (SWIHD) to the destroyed switch need to be prepared to take appropriate action.

Each client sharing the switch object has local resources associated with the open switch handle that must be de-allocated when the switch object is closed. When a peer notifies a client that it has closed the switch, the client invokes ctaDetachObject to de-allocate these local resources. Refer to the Natural Access Developer's Reference Manual for suggested methods of notification between peer clients.

If peer clients choose not to notify one another when they close a switch, there are other ways for the switch handle de-allocation to occur. When a client makes a call with the invalid switch handle, CTAERR_INVALID_HANDLE is returned from the Switching service manager. Upon receiving this error, the client invokes ctaDetachObject to de-allocate the local resources.

When swiCloseSwitch is called with an invalid switch handle, it automatically destroys the local handle upon being returned CTAERR_INVALID_HANDLE by the service manager. The caller does not need to make an explicit call to ctaDetachObject in this situation.