Any way to get a Type Count?

BlitzMax Forums/BlitzMax Beginners Area/Any way to get a Type Count?

Hardcoal(Posted 2014) [#1]
Hi.

Lets say I have a Type

Type Test
End Type

Is there a way to get the totaly amount of created Types of this Type?

Something Like Test.Count ?

tnx..


degac(Posted 2014) [#2]
Question: do you want to count the objects created or how many classes (type) you have in the source code?

In the first case just use a var to get track of created object (but not in the New method I think)

In the second case I think reflection can offer a solution ( I m not at the computer atm and I can't remember the right methods/function)
Sure reflection can be used to find the number of instanced object of a certain type also.


Hardcoal(Posted 2014) [#3]
degac.. I want to know in general on the whole process how many of a specific type exists.

This is so i can track whether my application has too many of a specific Type.

because is see memory increase while app is running
and I want to make sure that my app doesn't have overload of a specific type.


Brucey(Posted 2014) [#4]
Create a global counter for the type.
In New(), increment the counter.
In Delete(), decrement the counter.

Of course, types that you don't currently reference are still "counted" until they are GC'd.

Type TMyType
  Global count:Int

  Method New()
    count :+ 1
  End Method

  Method Delete()
    count :- 1
  End Method
End Type


However, I'd suggest you have a more serious problem if you don't understand why you would have "too many" objects, and should consider revisiting your design.


Derron(Posted 2014) [#5]
@Brucey
Consider Elements not getting correctly dereferenced and therefore referencing their own and individual imageCache or other "memory intensive data".

While this of course is a flaw in your design, it is a neat way to detect such things.


@Hardcoal
Pay attention that "new" is run for ancestors too

SuperStrict


Type A
	Method New()
		print "A"
	End Method
End Type

Type B extends A
End Type

Type C extends A
	Method New()
		print "C"
	End Method
End Type

new A
new B
new C


Output is
A
A
A
C

So this means: if you create "C" that custom "New" is called but also the "New" from Type A.


So take care not to increase the counter in each "New" but only the basic one.


bye
Ron


Henri(Posted 2014) [#6]
Any way to get a Type Count?

No easy way of getting the internal reference count of a type AFAIK, but you can get any objects reference count with:
print byte ptr(obj)[-4]


-Henri


Brucey(Posted 2014) [#7]
but you can get any objects reference count with..

I really don't recommend using undocumented functionality, even if it does appear to work.


Henri(Posted 2014) [#8]
Only way I see a need for this is for debugging purposes. But as said, theoratically the behaviour could change, allthough not likely.

-Henri


Brucey(Posted 2014) [#9]
But as said, theoratically the behaviour could change, allthough not likely.


Well, for example, in my WIP project (which allows for 64-bit BlitzMax apps, amongst other things), I dropped the ref counter on the object because my GC tracks it differently.

As I say, it's better to stick with documented functionality :o)


Hardcoal(Posted 2014) [#10]
Offcourse i was about to use it for debug pupose and for my final version


Henri(Posted 2014) [#11]
What I meant with "debugging purposes" was to use it to find memory leaks and such.


As I say, it's better to stick with documented functionality :o)

I'm not arquing against that :-). Don't know what the current status of Blitzmax documentation is, but it used to be a bit lacking.

-Henri