Page 1 of 1

ClearStructure(..., ..., #PB_Structure_ClearNested)

Posted: Mon Jul 26, 2010 12:05 pm
by srod
Hi,

just updating some code to use some of the new features (PB 4.5), in particular discarding some code for clearing dynamic structures in favour of the new ClearStructure() etc.

Came across some nested structure pointers in which it would be nice ( :) ) if ClearStructure() could be persuaded to do it all for me in one swoop!

Take the following structure for example :

Code: Select all

Structure test
  a$
  *ptrString.STRING
EndStructure
And the following code which allocates two chunks of memory, one for a 'test' structure and one for a STRING structure :

Code: Select all

*ptr.test = AllocateMemory(SizeOf(test))
If *ptr
  *ptr\a$ = "Hello matey!"
  *ptr\ptrString = AllocateMemory(SizeOf(STRING))
  If *ptr\ptrString
    *ptr\ptrString\s = "Hello again!"
  EndIf
EndIf
Now as things stand, the only way of tidying this memory up is via two calls to ClearStructure() as follows :

Code: Select all

ClearStructure(*ptr\ptrString, STRING)
ClearStructure(*ptr, test)
No great hardship I admit. :)

I was just wondering, however, if it would be viable to add some optional parameter to ClearStructure() so that it could run through and clear both structures in one go? I would then be able to replace my two previous calls to ClearStructure() with something like :

Code: Select all

ClearStructure(*ptr, test, #PB_Structure_ClearNested)
With this optional parameter specified, all pointers to structures (of explicit types) could be tidied up by PB in some recursive manner. Course this would have to be used with great care and would only be useable if all such structure pointers pointed to some dynamically allocated memory rather than some global/static structure etc, but in my case that is invariably the case anyhow! :)

Thanks.

Re: ClearStructure(..., ..., #PB_Structure_ClearNested)

Posted: Mon Jul 26, 2010 12:37 pm
by helpy
Hi srod,

and who will free the allocated memory of the STRING structure? To free all of the structure this should be done:

Code: Select all

ClearStructure( *ptr\ptrString, STRING )
FreeMemory( *ptr\ptrString )
ClearStructure( *ptr, test )
FreeMemory( *ptr )
But with ...

Code: Select all

ClearStructure( *ptr, test, #PB_Structure_ClearNested )
... *ptr/ptrString would be set to #Null ... so you would have to store the memory pointer in a temporary variable, therfore you can free the allocated memory!

See:

Code: Select all

Structure test
  a$
  *ptrString.STRING
EndStructure

*ptr.test = AllocateMemory(SizeOf(test))
If *ptr
  *ptr\a$ = "Hello matey!"
  *ptr\ptrString = AllocateMemory(SizeOf(STRING))
  If *ptr\ptrString
    *ptr\ptrString\s = "Hello again!"
  EndIf
EndIf

ClearStructure( *ptr, test )
Debug *ptr\ptrString   ; ==> Result is #Null !!!
cu,
guido

Re: ClearStructure(..., ..., #PB_Structure_ClearNested)

Posted: Mon Jul 26, 2010 12:47 pm
by srod
Good point, I knew there was something I was overlooking! Something was nagging away at me! :)

Oh well, teach me to be so lazy! :wink: