AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Purebasic image memory allocation9/25/2023 and, well, naturally your code brings a few more questions to mind.! It would indeed be useful for other purposes to be able to manually free a string, so I would like to see if it's possible to get this hack working.Ĭode: Select all Procedure FreeString(*free_me.STR) Thank you very much for your help though tinman I really have to learn ASM sometime so I can take advantage of it in languages such as PB which allow you to inline it. It seems my best bet will be to just use PB's linked lists, and design around the inherant flaws they present. allocate structure using PB's linked list instead of AllocateMemory Anyhow, fortunately it seems that the latest PB update's bug fix to do with strings in structures in linked lists, is perfectly relevant, as this code seems to prove:Ĭode: Select all manual structure memory allocation/memory leak test #2 I had to rename the "STRING" structure from your example to "STR", as it seems "STRING" is already defined. I will probably write a similar test prog later to find outĬode: Select all manual structure memory allocation/memory leak test #3ĭebug "Memory difference after allocating 10 strings of 60,000 bytes: " + Str(debugmemory "Memory reclaimed by freeing strings and de-allocating structure: " + Str(debugmemory ()) so maybe this is the exact issue they were addressing. I remember seeing something in the version history about strings in linked lists being fixed. Or try Linked Lists again, and see if they exhibit similar behaviour, or maybe are a bit more well-behaved. I guess this means to do what I want, I will have to do something similar to c++ and manually allocate arrays of characters, and basically end up rolling my own string functions. since memory use is something that fluctuates quite often in windows!) If you uncomment the lines which reset the strings, they seem to free up around 573,440 bytes, which is close, but obviously not all :/ (I had to run the code several times to see which were the lowest numbers being reported. As you can see by running the code, the strings seem to take up roughly 606,208 bytes after being created, and if not reset to "" still remain in memory after the struct is freed. The program allocates a struct with 10 strings, makes the strings fairly large (60,000 bytes each), and then tries to free the structure. Thanks in advance for any help you PB gurus can offerĭebug "size of structure: " + Str(SizeOf (mystruct))ĭebug "Initial Memory free: " + Str(debugmemory ())ĭebug "Memory difference after allocating 10 strings of 60,000 bytes: " + Str(debugmemory ())ĭebug "Memory reclaimed by freeing strings and de-allocating structure: " + Str(debugmemory ())īasically it contains a simple function to use the winAPI's GlobalMemoryStatus to check available system memory, and keep a running tally of the changes happening in memory. ie a memory leak)Īnd finally, is there any reason why PB's memory functions would not suffice for this? Would the WinAPI's heap functions offer any difference/advantage? What I am wondering is if FreeMemory (*newStruct) will therefore take this into account, and de-allocate the string's buffer as well? Or will it simply de-allocate the amount of memory equal to the size of the structure? (Leaving the string's contents somewhere in memory. Namely, what will this do to structures that have strings in them? The size of a struct that has a string is constant (since regardless of the string's contents, the struct itself will contain a pointer for the string buffer), however the amount of memory actually being used by the string is prone to change (ie if the string is modified, etc) However this brings some questions to mind. *newStruct = AllocateMemory (SizeOf(myStruct)) I figure it can be done by doing something like: So this brings about some questions about rolling my own code to dynamically allocate an ever-changing amount of structures. I sort of like the linked list idea, but with my recent discovery of the bug described here, I am hesitant to use them (are there any other bugs w/ linked lists that are known.?) I also may need to do nested linked lists (ie lists inside of lists), and AFAIK this is currently unsupported with native PB commands. as far as I see it my options are a) PB's linked lists commands, and b) manual memory allocation (using either PB's AllocateMemory, or the WinAPI's heap functions). I'm trying to think of the best way to do this in PB. I'm in the design phase for a project which will require support for any number of dynamically created objects (structures).
0 Comments
Read More
Leave a Reply. |