Showing results for 
Search instead for 
Did you mean: 

does UF_free handle arrays?

Valued Contributor
Valued Contributor

I've been told that I need to call UF_free upon all objects marked <OF> returned from Open C functions.  Does UF-free distinguish between arrays and non-arrays, though?  For example:


tag_t someTag; // pretend this is initialized to a valid value

tag_t* members; // this will be an array.

int n; // size of array

UF_GROUP_ask_group_data(someTag, &members, &n);


The second argument of UF_GROUP_ask_group_data is marked <OF>, so I know I'm responsible for freeing it.  I've been told I'm not supposed to call


delete [] members;


but rather




But this doesn't make sense to me, because I don't know how UF_free is going to be able to detect that this is an array.  It's receiving a tag_t*, so, for all it knows, it should be calling the equivalent of:


delete members;


Depending on your compiler, this would either result in a memory leak or heap corruption.  Did the UG team put special sauce into UF_free to look at the header info for any input pointers, and determine whether it's an array?


Re: does UF_free handle arrays?

Siemens Pioneer Siemens Pioneer
Siemens Pioneer

You are better calling UF_free primarily because the NXOpen version of freeing memeory could potentially be doing more things best known to NX than the regular delete[]. Here's some documentation about memory pools in the wiki.


As you can see UF_free expects a void*. It doesn't matter what you pass as long as the documentation mentions that this array needs to be freed.



Re: does UF_free handle arrays?

Gears Esteemed Contributor Gears Esteemed Contributor
Gears Esteemed Contributor


Many of the "struct"s that need to be freed have their own specific "UF_*_free_*" functions.

I believe the docs will steer you to the correct function for each one.

E.g. look at UF_DRF_ask_appended_text  - you need to use UF_DRF_free_appended_text



Ken Akerboom Sr CAx Systems Engr, Moog, Inc.
Production: NX10.0.3.5 MP16/TC11.2
I'd rather be e-steamed than e-diseaseled

Re: does UF_free handle arrays?

Siemens Phenom Siemens Phenom
Siemens Phenom


I can't think of a function offhand where we specify OF for anything unless we are expecting to allocate space for an array.  (There might be one, but I can't think of one.)  The general rule is, if we specify OF, then please use UF_free, or if there is a special freeing function for that type, use that, as Ken mentioned above.


But if you allocate it yourself, then you can use the appropriate freeing function that matches the method you are using for allocation. (malloc() and free(), for example.)


Re: does UF_free handle arrays?

Valued Contributor
Valued Contributor

OK, thanks.  So I'll assume for now that UF_free is meant only for array outputs.  If I see any non-array outputs marked <OF>, I'll know to look for a special deleter, and if I don't find one, maybe I'll post a comment in this thread.


Reason I'm asking is I'm maintaining a C++ wrapper someone wrote to add RAII behavior to the NXOpen C API.  (We apparently do this just because we have old code that used to be C and that we don't have time to refactor for the C++ API, then re-validate, etc.)