@@ -12,6 +12,10 @@ module c_interface_poly
1212 integer (c_int), parameter :: STATUS_ZERO_DIMS = 3_c_int
1313 integer (c_int), parameter :: STATUS_ALLOC_FAIL = 4_c_int
1414 integer (c_int), parameter :: STATUS_BAD_OBJECT_PTR = 5_c_int
15+ integer (c_int), parameter :: STATUS_FORTDATA_PTR = 6_c_int
16+ integer (c_int), parameter :: STATUS_SET_DATA_DEALLOC_FAIL = 7_c_int
17+ integer (c_int), parameter :: STATUS_SET_DATA_ALLOC_FAIL = 8_c_int
18+ integer (c_int), parameter :: STATUS_DESTRUCT_DEALLOC_FAIL = 9_c_int
1519
1620contains
1721
@@ -27,6 +31,7 @@ integer(c_int) function objconstruct_C(objtype,cptr_f90obj,cptr_indata,lx,ly) bi
2731 type (data2), pointer :: tmpobj2 ! < ditto
2832 real (c_float), dimension (:,:), pointer :: fortdata
2933 integer :: alloc_status
34+ integer (c_int) :: data_status
3035
3136 ! > nullify for sake of clarity and good practice
3237 nullify(tmpobj1, tmpobj2)
@@ -69,7 +74,25 @@ integer(c_int) function objconstruct_C(objtype,cptr_f90obj,cptr_indata,lx,ly) bi
6974 ! > initialize some test data, and call methods to print
7075 print * , ' Initializing test data...'
7176 call c_f_pointer(cptr_indata,fortdata,shape= [lx,ly])
72- call obj% set_data(fortdata)
77+ if (.not. associated (fortdata)) then
78+ objconstruct_C = STATUS_FORTDATA_PTR
79+ return
80+ end if
81+
82+ data_status = obj% set_data(fortdata)
83+ select case (data_status)
84+ case (0_c_int )
85+ continue
86+ case (1_c_int )
87+ objconstruct_C = STATUS_SET_DATA_DEALLOC_FAIL
88+ return
89+ case (2_c_int )
90+ objconstruct_C = STATUS_SET_DATA_ALLOC_FAIL
91+ return
92+ case default
93+ objconstruct_C = STATUS_SET_DATA_ALLOC_FAIL
94+ return
95+ end select
7396
7497 ! ! note lack of deallocate and nullify here; we need memory allocated to persist past the return.
7598end function objconstruct_C
@@ -139,6 +162,7 @@ integer(c_int) function destruct_C(objtype, objC) bind(C, name='destruct_C')
139162
140163class(dataobj_poly),pointer :: obj
141164integer (c_int) :: status
165+ integer :: dealloc_status
142166
143167destruct_C = STATUS_SUCCESS
144168if (.not. c_associated(objC)) return
@@ -149,8 +173,20 @@ integer(c_int) function destruct_C(objtype, objC) bind(C, name='destruct_C')
149173 return
150174end if
151175
152- if (associated (obj% dataval)) deallocate (obj% dataval)
153- deallocate (obj)
176+ if (associated (obj% dataval)) then
177+ deallocate (obj% dataval, stat= dealloc_status)
178+ if (dealloc_status /= 0 ) then
179+ destruct_C = STATUS_DESTRUCT_DEALLOC_FAIL
180+ return
181+ end if
182+ end if
183+
184+ deallocate (obj, stat= dealloc_status)
185+ if (dealloc_status /= 0 ) then
186+ destruct_C = STATUS_DESTRUCT_DEALLOC_FAIL
187+ return
188+ end if
189+
154190objC = c_null_ptr
155191
156192end function destruct_C
0 commit comments