Global variable inside type exist when discarded.
BlitzMax Forums/BlitzMax Programming/Global variable inside type exist when discarded.
| ||
Are you suppose to be able to reference a Global variable in a type once the memory for that type has been release and then flushed? Now I may have misunderstood. But I thought it was Global just for that type. It looks like it is Global throughout the whole program. I would think since you could only access the global through the type specifier - then I would think it wouldn't be accessible if that type doesn't exist anymore. Type t Global a:Int Method New() a = a + 1 End Method End Type n:t=New t j:t=New t Release n Release j FlushMem t.a = 5 Print t.a |
| ||
How can a Type not exist? Globals are attached to the Type, not to the Type Objects. |
| ||
If a type is set to nothing or null, then everything that is associated with it doesn't exist anymore until it is instantiated again. In VB.Net, when I set c to Nothing, I don't have access to that object anymore because the object doesn't exist. It's Null Dim i As Integer Dim c As New Class1 i = c.a i = c.b c = Nothing i = c.a i = c.b Am I confused,ignorant of something or what? I thought this is what I was doing in BMax. Type t Global a:Int Method New() a = a + 1 End Method End Type n:t=New t j:t=New t Release n Release j FlushMem t.a = 5 Print t.a |
| ||
No. Globals inside a type are actually specific to an instance of the type, but the type itself. For instance:Type Class Field Blah Global GlobalVar=5 EndType Class.GlobalVar=3 'Yes, you can do this Print Class.GlobalVar Instance:Class=New Class Print Class.GlobalVar This is actually one thing the docs cover. |
| ||
@ Bot Builder Didn't you mean to say... Globals inside a type are actually NOT specific to an instance of the type, but the type itself. For instance: ...because if you did, OK, I understand that now, but how come when I release an object or make it null I can still reference the global through your 'Instance'. For instance: Type Class Field Blah Global GlobalVar=5 EndType Class.GlobalVar=3 'Yes, you can do this Print Class.GlobalVar Instance:Class=New Class instance=Null 'Here Release(instance:Class) 'Here again to make sure Print Class.GlobalVar Print instance.globalvar ' and still be able to do this |
| ||
Whoops, yeah, i meant that. I'm not sure why you can reference the global through a reference. I doubt that it actually is a reference anymore, and the compiler simply figures you meant to change the global of the type rather than the instance. And since this is a compiler decision rather than a run-time decision it need not actually still exist. |
| ||
JoJo, couple of things, not directly related. First, Types are NOT objects. This is important. When you go New, or such you make an INSTANCE of a TYPE. An Instance of a Type is an object. Second, do not call Release on objects. And regarding your question. Blitz knows that Instance was of type Class so when you go Instance.GlobalVar it works it all out. Aaron |
| ||
I think we need to lose the release command. |
| ||
I agree with John. And Aaron. Incidently you really shouldn't reference an instance once it's been dereferenced. It might work now, but I'm thinking that's more of a fluke than by design. |
| ||
First, Types are NOT objects. This is important. When you go New, or such you make an INSTANCE of a TYPE. An Instance of a Type is an object. You see, thats just it I know this. Just typing to quickly. I also need to remember that Bmax has OOP but with some new additions of its own. Second, do not call Release on objects. So I assume you kill an object with Null. And what am I to make of this in the blitz docs... Release references to a handle or object Incidently you really shouldn't reference an instance once it's been dereferenced. It might work now, but I'm thinking that's more of a fluke than by design. Yes, and I must not forget that this is the beta! |
| ||
So I assume you kill an object with Null. In the very rare situation where you actually do need to manually dereference an object, yes, you can use Null. Normally you'd just leave it be and let it disappear by itself when it's no longer needed. And what am I to make of this in the blitz docs... Release references to a handle or object It's for integer handles only. If you really want to you can get an integer that points to an instance of a Type, and for those handles Release is used in the same way that you would otherwise set MyType=Null. It's best to avoid using integer handles and the Release command at all. |
| ||
@teamonkey I see. Thanks! ...And thanks all! |