Skip to content

Nullify the user-object pointer during smart-pointer construction #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module user_object_m

module function construct(user_object) result(user_object_ptr)
implicit none
type(user_object_t), intent(in), pointer:: user_object
type(user_object_t), intent(inout), pointer:: user_object
type(user_object_ptr_t) :: user_object_ptr
end function

Expand All @@ -44,6 +44,7 @@ module subroutine free(self)
module procedure construct
call assert(associated(user_object), "construct_from_pointer: associated(user_object)")
user_object_ptr%ref => user_object
nullify(user_object)
call user_object_ptr%start_counter
end procedure

Expand Down Expand Up @@ -71,15 +72,16 @@ program main

print *, "Defining smart_pointer_1."
smart_pointer_1 = user_object_ptr_t(user_object)
call assert(.not. associated(user_object), "main: .not. associated(user_object)")
print *, "Reference count = ", smart_pointer_1%reference_count()
print *, "Copying smart_pointer_1 into smart_pointer_2."

smart_pointer_2 = smart_pointer_1
print *, "Reference count = ", smart_pointer_1%reference_count()
call assert(smart_pointer_1%reference_count()==smart_pointer_2%reference_count(), "consistent counts")
call assert(smart_pointer_1%reference_count()==smart_pointer_2%reference_count(), "main: consistent counts")

call new_reference(smart_pointer_2)
call assert(smart_pointer_1%reference_count()==smart_pointer_2%reference_count(), "consistent counts")
call assert(smart_pointer_1%reference_count()==smart_pointer_2%reference_count(), "main: consistent counts")
print *, "Reference count = ", smart_pointer_1%reference_count()
print *, "smart_pointer_1 and smart_pointer_2 going out of scope"
end block
Expand Down