![]() 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. It’s clear that some limitations are present in the template but for most of the tasks this is a great addition to anyone’s code.I'm in the design phase for a project which will require support for any number of dynamically created objects (structures). Because overloading doesn’t exist in PB, the constructor will be called twice (once by the template and once by yourself) if you want to define the members using said ctor, just make certain that any dynamic allocations are performed only once or you’ll have a nasty memory leak in your hands. ![]() We’re not trying to mimic any language here but rather provide a way of dealing with objects without having to write redundant code all the time. There are certain limitations that forced me to design the template as it is right now but the usage is very simple. Macros in this case are used as an aid to define templates (like you would in C++ for instance). ![]() If we decide to unregister a class, all of the objects will be freed and their respective destructors will be called. Once a class has been registered, we can create new objects and delete them at will. The usage is fairly simple: classes can be registered and unregistered. The purpose of EasyObject is mainly to encapsulate trivial and redundant code usually regarded to allocation, deallocation and keeping track of the objects. With EasyObject you can define constructors and destructors as well as iterators with ease. It allows you to register any structure as a “class”. Ok, now let’s describe this thing a little more and leave the marketing speech in the past.ĮasyObject is an object factory template for PureBasic. Third, you’re free to post any comments regarding on how to enhance it if you dear to!. If you open your eyes you’ll see that this is an obvious pattern but what isn’t so obvious is how do you implement a template that deals with it in an efficient and clean way within the current constraints of the language.įirst, download it. ![]() code every time you incorporate a new “class” into your project. Why should we?, let’s avoid it all together and focus our efforts in the task at hand rather than in the mundane stuff. However in essence, you end up writing pretty much the same allocate / deallocate, list, etc. Think about it, you encapsulate your structure-related code in such a way that it becomes clean, nice and tidy - in other words it makes it easy to understand what it does. Whenever you deal with structures and what have you it’s obvious that there has to be a better way of doing it. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |