Check if the context is valid to avoid crash

master
acozma 2014-07-22 16:23:33 +03:00
parent f0a5eab5f7
commit f4962df6d6
2 changed files with 11 additions and 10 deletions

10
iio.h
View File

@ -27,12 +27,6 @@ extern "C" {
#endif
#include <stdint.h>
#ifdef EXTRA_H
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stddef.h>
#endif
#ifdef _MSC_BUILD
/* Come on Microsoft, time to get some C99... */
@ -116,6 +110,10 @@ __api struct iio_context * iio_create_network_context(const char *host);
* <b>NOTE:</b> After that function, the iio_context pointer shall be invalid. */
__api void iio_context_destroy(struct iio_context *ctx);
/** @brief Check if the given context is valid
* @param ctx A pointer to an iio_context structure
* @return if the context is not valid a negative error code is returned */
__api int iio_context_valid(struct iio_context *ctx);
/** @brief Get the version of the backend in use
* @param ctx A pointer to an iio_context structure

View File

@ -63,15 +63,18 @@ classdef iio_sys_obj < matlab.System & matlab.system.mixin.Propagates ...
% Create network context
obj.iio_ctx = calllib(obj.libname, 'iio_create_network_context', obj.ip_address);
% Get the number of devices
try
nb_devices = calllib(obj.libname, 'iio_context_get_devices_count', obj.iio_ctx);
catch
% Check if the network context is valid
ctx_valid = calllib(obj.libname, 'iio_context_valid', obj.iio_ctx);
if(ctx_valid < 0)
obj.iio_ctx = {};
unloadlibrary(obj.libname);
msgbox('Could not connect to the IIO server!', 'Error','error');
return;
end
% Get the number of devices
nb_devices = calllib(obj.libname, 'iio_context_get_devices_count', obj.iio_ctx);
% If no devices are present unload the library and exit
if(nb_devices == 0)
calllib(obj.libname, 'iio_context_destroy', obj.iio_ctx);