Skip to content
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

c++ instances allocated via MemoryManager do not call destructors when deleted #1529

Closed
excaliburtb opened this issue Jul 3, 2023 · 7 comments
Assignees

Comments

@excaliburtb
Copy link
Contributor

Similar to #1301 but for a different reason. I found that if I declare a c++ class using memory manager (i.e. TMM_declare_var_s for my specific test), the alloc_info instance that gets allocated to track it always has the allocation set for TRICK_ALLOC_MALLOC. Since that enum TRICK_ALLOC_MALLOC = 0, it's either a mistake and is just resulting to the default.. OR, allocations are intentionally tracked as MALLOC instead of TRICK_ALLOC_NEW for some reason. git grepping through the code, I see no attempt to set the alloc_type to anything except for the DataRecordGroup. So, it doesn't matter which API I use, Trick will never treat it as anything other than a malloc.

@sharmeye
Copy link
Contributor

Thanks, we'll look into this and let you know.

@jmpenn
Copy link
Contributor

jmpenn commented Jul 17, 2023

It looks like we might have just overlooked adding "alloc_type = TRICK_ALLOC_NEW"
in the two occurrences where we set "language = Language_CPP" in :

void* Trick::MemoryManager::declare_var(TRICK_TYPE type,std::string user_type_name, int n_stars, std::string var_name, int n_cdims, int *cdims);

,in MemoryManager_declare_var.cpp.

I'll add this, and test to ensure that it works as intended.

@jmpenn
Copy link
Contributor

jmpenn commented Jul 17, 2023

Specifically, my proposed fix is:
In MemoryManager_declare_var.cpp

After line 48, add:
TRICK_ALLOC_TYPE alloc_type;

After lines 128 & 141, add:
alloc_type = TRICK_ALLOC_NEW;
144: alloc_type = TRICK_ALLOC_NEW;

After line 158, add:
new_alloc->alloc_type = alloc_type;

@excaliburtb
Copy link
Contributor Author

that looks good to me

@jmpenn
Copy link
Contributor

jmpenn commented Jul 19, 2023

Complication: It looks like the generated io_src allocation routines sometimes call calloc() [even when marked CPP], and sometimes call new(). Need to do more digging.

@excaliburtb
Copy link
Contributor Author

hmm.. isn't there a io_src_*_destruct routine that gets generated? can't we use those?

jmpenn added a commit that referenced this issue Jul 25, 2023
declare_var() should indicate in ALLOC_INFO::alloc_type, how
it has allocated the new object. From this we know how to delete it.

declare_var() can allocate an object in one of two ways:
1) using  malloc or calloc, or
2) using io_src_alloc_<CLASS>, which is generated by ICG.

PROBLEM 1 :
ALLOC_INFO::alloc_type was not properly set when allocating a composite type.
It should have been set to TRICK_ALLOC_NEW to indicate that it was allocated
by io_src_alloc_<CLASS>.

PROBLEM 2:
The enumeration value TRICK_ALLOC_NEW is mis-named and is misleading. The io_src_alloc_<CLASS>
routines do not necessarily allocate memory using new. If the object is a POD type, it uses calloc.

SOLUTION:
So, THIS commit:

1) Renames TRICK_ALLOC_NEW to TRICK_ALLOC_IOSRC so as not be misleading.

2) In declare_var(), sets alloc_type = TRICK_ALLOC_IOSRC when allocating a composite type (TRICK_STRUCTURED).
jmpenn added a commit that referenced this issue Jul 25, 2023
@jmpenn
Copy link
Contributor

jmpenn commented Jan 24, 2024

After interruptions, and complications, and mistakes, I believe I've finally got it.
After call to io_src_allocate_class, allocation_type should instead be set to TRICK_ALLOC_MALLOC.
io_src_allocate_class always uses calloc().

I've made a new pull-request (#1639) for which all of the unit tests pass. These include a new test ( TEST_F(MM_delete_var_unittest, CXX_object_constructor_destructor)) that specifically verifies that the destructor is being called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants