alogrep 0 Posted 18 hours ago HI. I have some stringgrids that use objects. For most of them the object is simply an integer (MySg.objects[c,r]:=Tobject(mynumber);; A few use complext object that I declare and assign a Create/destroy consctructor/desctuctor. Now when i try to delete an object of the "comples type" all works fine: the destructor takes care of it.. If i do if assigned(MySg.objects[c,r]) then MySg.objects[c,r].free On the Integer type, I get an AV exception. Perhaps the integer type object does not need to be freed. But then how do I inpect if the object is of integer type or not? MySG.objects[c,r].classname gives me a "not acessible" message. Any helop, please? Share this post Link to post
Dave Novo 56 Posted 18 hours ago What you are doing is very error prone, for the reasons you describe. If you are using low valued integers, you could inspect the value of the objects[x,y] and if the value is below some threshold, then do not free it. One way of doing this is 1. Make sure to set all objects[x,y] to zero 2. if setting it to an integer value, set it to the negative of the integer you want. Make sure to when using the value to negate it again 3. If setting to an object, store the pointer as a positive integer then, when freeing, only free where objects[x,y] is a positive value. I think that will work for 32 and 64 bit until you have set your 32 bit app to specifically use memory above 2GB block. Share this post Link to post
Remy Lebeau 1586 Posted 12 hours ago 5 hours ago, alogrep said: Perhaps the integer type object does not need to be freed. Correct. 5 hours ago, alogrep said: But then how do I inpect if the object is of integer type or not? You can't. So, either store all objects only, or all integers only. Don't mix types. But, if you must, then you'll need to either wrap the integers inside of objects, or add an extra header in front of each value to identify its type, etc. 2 Share this post Link to post
Roger Cigol 132 Posted 8 hours ago This strikes me as being a classic use case for inheritence. Define a base class of a type ObjectToStoreInStringGrid and then derive each of the types you need to store from this base class. Share this post Link to post
Remy Lebeau 1586 Posted 2 hours ago 15 hours ago, Dave Novo said: If you are using low valued integers, you could inspect the value of the objects[x,y] and if the value is below some threshold, then do not free it. Another option might be to use tagged pointers. Normally, objects are aligned in memory in such a way that there are certain bits in object pointers that are always unused/zero, which can be repurposed if you are careful. For instance, if you limit your integer values to 31 bits (x86) or 63 bits (x64), you can use an unused bit in a pointer to flag whether the pointer holds an integer value vs an object address, and just mask off the bit when extracting the value. 1 Share this post Link to post