MATLAB: Do I receive an error “Invalid or deleted object” when I access curs.Data after having used close(curs) in Database Toolbox R2017b

MATLAB

The following code used to work fine in MATLAB R2017a:
conn = database('MSSQLNative','','')
curs = fetch(exec(conn,'SELECT * FROM myTable'))
close(curs)
myData = curs.Data
But after upgrading to release R2017b this throws:
Invalid or deleted object.

Best Answer

To resolve a memory leak issue in previous releases, closing a cursor now also automatically deletes the cursor, after which curs.Data is indeed no longer accessible. To make your code work correctly in release R2017b again, change the order:
conn = database('MSSQLNative','','')
curs = fetch(exec(conn,'SELECT * FROM myTable'))
myData = curs.Data
close(curs)
Such that you do not close the cursor before you are done with accessing curs.Data.