Home > Software engineering >  Vb how to release the class module created object
Vb how to release the class module created object

Time:11-02

Set obj=nothing this sentence did not release, if after created obj set obj=new class again before that has not been unloaded, but just have one more obj, previous obj and obj objects created still exist, how to release the class to create real obj

CodePudding user response:

The set obj=nothing this sentence did not release
//
How do you know that there is no release?

CodePudding user response:

Set obj=nothing to reduce the object's reference count, only when the end of the reference object after a variable is nothing will truly release, such as:
Dim o1 as class
Dim o2 as class
The set o1=new class
The set of o2=o1

Set=nothing 'o1 object will not be released here, just reduce the reference count
The set here will truly release o2=nothing '

CodePudding user response:

Then you must be is circular references between the objects

CodePudding user response:

"Senior Visual Basic programming (Advanced Visual Basic)"
Chapter six circular reference

Whole chapter content says, the more complete than here piecemeal discussion,
Study hard!

CodePudding user response:

Senior Visual Basic programming

CodePudding user response:

refer to the second floor response:
set obj=nothing to reduce the object's reference count, only when the end of the reference object after a variable is nothing will truly release, such as:
Dim o1 as class
Dim o2 as class
The set o1=new class
The set of o2=o1

Set=nothing 'o1 object will not be released here, just reduce the reference count
The set here will truly release o2=nothing '

Dim o1 as class 'what will this do you think in the memory of a thing? A memory structure with code?
'it's nothing but a 4 bytes of memory address pointer, and there is no point to any address
Dim o2 as the class 'of course, this is no exception, also is just a null pointer

Set=new class 'o1 here to dynamically allocated memory, structure to create instances of classes
Set o2=o1 'here is just let two Pointers pointing to a memory address, that is the same class instance
'don't you think here splits out two class instances? One is the original,
'the other one is for assignment and come into a new instance of the new new?
Set o1=nothing 'if VB won't assignment statement led to new new instance object automatically, then here has been completed
'the release process, of course, also may not release out, probably because the class itself to use the API to dynamically create
'resources, then there is no release in the event of such release is clean, but it belongs to the kind of programming problems, rather than
'class application problems,
The set of o2=nothing 'this is completely unnecessary

CodePudding user response:

refer to 6th floor response:
refer to the second floor response:

Set obj=nothing to reduce the object's reference count, only when the end of the reference object after a variable is nothing will truly release, such as:
Dim o1 as class
Dim o2 as class
The set o1=new class
The set of o2=o1

Set=nothing 'o1 object will not be released here, just reduce the reference count
The set of o2=nothing 'here...


How to say in CSDN also saw you many times, how don't even know this, set obj=nothing and no object assignment is empty, only the pointer assignment is empty

CodePudding user response:

Specific appear to have I haven't find out the condition of circular reference, but won't appear object cancellation is certain

CodePudding user response:

refer to 7th floor response:
how said that also saw you many times in CSDN, how don't even know this, set obj=nothing and no object assignment is empty, only the pointer assignment is empty

Indeed, the problem is my neglect, I always thought Set Obj=Nothing of VB and VC in the delete (Obj); 是一样的效果了,但经过测试,的确不一样,不过有一点你说错了,set obj=nothing 并不只是把指针赋值为空,而是触发了某个过程来处理类实例内存里的数据,我通过观察内存得知,VB 当 new 一个类实例的时候,这个类实例内存中有一个引用对象基数,而这个基数初始值是1,如果在程序里用了类似 Set o2=o1 这种类赋值的用法,这个类实例的引用对象基数会加1,同时,这个 o2 的地址也会被记录到类实例的引用列表内存中,当然,如果还有更多的类似 Set o3=o1,Set o4=o1 这种用法也会让这个基数继续加1,且同样会记录引用对象地址到实例内存区,之后,如果要释放这个类实例,必须对每一个引用对象做一次set obj=nothing操作,因为只有这样才能对应上实例里记录的每一个引用对象列表项,只有地址对应了,VB才会对那个引用对象基数进行减1操作,直到那个基数值为1时,相应还在引用对象列表的对象再做 set obj=nothing 操作时,才会最后触发类里的Terminate 过程和 VB 内部的资源释放过程,而这一系列的操作,并不只是像你说的设置某个指针地址那么简单,而是通过 set obj=nothing 这种 VB 上的写法,被 VB 转译成了一个复杂的执行过程,

CodePudding user response:

references 9 floor response:
reference 7 floor response:
How to say in CSDN also saw you many times, how don't even know this, set obj=nothing and no object assignment is empty, only the pointer assignment is empty

Indeed, the problem is my neglect, I always thought Set Obj=Nothing of VB and VC in the delete (Obj); Effect is the same, but after the test, it is really not the same, but one thing you said is wrong, the set obj=nothing is not just the pointer assignment is empty...

Good job! But this Msdn on the set obj=nothing for that

CodePudding user response:

VB resources, will be released automatically after more than scope, this special let me miss, unlike c #, VB.NET provides automatic shit garbage collection mechanism (GC in advance can be used safely recycling).

CodePudding user response:

VB object is managed by VB, new in VB and VC in the new in behavior is different, although the surface looks like, this is the VB not delete (obj), VB will automatically go to do such a thing, is that the benefits of VB programmers don't use too so concerned about the problem of memory leaks, and so on;
Vb set obj=nothing more like a kind of application, the request vb release object, but does not release is the object manager depends on vb, vb object manager use the reference count to determine the object's left, when the reference count is zero when it is released, the opposite will always exist,
If cannot release object, there are only two reasons, either cyclic references such as said earlier, or this object is not VB object manager created (new to create useless VB)

CodePudding user response:

Thank you, I compare the food, or a little now and can't release the CLS

CodePudding user response:

How do you know how much an object reference count is? Sometimes hope to release in counting is equal to 1, rather than automatically released by VB to 0
  •  Tags:  
  • API
  • Related