Results 1 to 5 of 5

Thread: *some* help please

  1. #1

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908

    *some* help please

    I've posted this before, got an answer in the com/active x forum, but non-helpful as it pertained very little to my problem. Here's the situation:

    An object that I've created will not terminate properly. The program does not terminate abnormally (ie end, an error, etc.) and in actuality after I set the object to nothing it continues to use subs and functions that are located within it. When the system timer checks to see if the socket's status is connected, (the system timer is shared by all objects in game) it sends a signal to the object to shut down. The annoying part is: This object is the one that doesn't terminate properly.

    Now, I store all active player objects in a dictionary. This is great for the sheer ease of checking if a name is already in use. Anyway, I am using a wrapper class around the dictionary, which is great because I've added quite a bit of extra functionality to it. When I send the player object to be added to the dictionary, should I set it as ByRef or ByVal? I understand the difference between the two methods. (ByRef more or less sends a pointer to the actual object, ByVal sends a copy of the object.) How would I go about the release & destruction sequence? IE: Should I set the object to nothing and then remove it from the dictionary? or visa versa?

    I did some testing and found that if I don't set a player object to nothing but just remove it from the dictionary the class_terminate will go off. (IE initial listen object gets set then added then removed in that order and the terminate event will fire.)

    If anyone can help me sort this out, please let me know. Thanks.
    -Excalibur

  2. #2
    Zaei
    Guest
    VB wont let you pass an object ByVal. When you pass ByRef, you are just passing the objects starting location in memory. When you "Set" something, you are just saying "Set this variable to be a location in memory to this object". When you set the Object to "Nothing", you are just saying to the system "Hey! This memory is free!", so it can be used again. If you add the object ByRef, you are just storing a "Long" value. If you try to use methods in that object, you should get an error.

    Probably the best way for you to do it would be to have a method in the class called "Cleanup" that is called when you remove it form the dictionary, then remove the pointer to the object.

    Z.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    How do you initialiaze the object? If you initialize it with new keyword then VB should be able to terminate it at the end of execution or when it runs out of scope or when you have derefenced it from all your variables. if you've returned it from intializing it in an activeX component that might be in use after the application is executed or when your variable runs out of scope it might still be in memory if the activeX component has a reference to it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    How do you initialiaze the object? If you initialize it with new keyword then VB should be able to terminate it at the end of execution or when it runs out of scope or when you have derefenced it from all your variables. if you've returned it from intializing it in an activeX component that might be in use after the application is executed or when your variable runs out of scope it might still be in memory if the activeX component has a reference to it.
    I initialize the variable thusly:
    Code:
    Public Player As Persona
    
    Public Sub Main()
        Set Player = New Persona
        '.... assign data
        PlayerList.Add PName, Player
        'PName = players logon. PlayerList is a wrapper for a Dictionary
        Set Player = Nothing
    End Sub
    After reading some of the responces I got, I did this in the Socket_Close event of the Persona Object:
    Code:
    Private Sub Socket_Close()
        SocketInfo = DISCONNECTED
        ehPlayer.EventGlobal SysText(164) & FileOps.GetFile _
            (System.DataDir, "logoff.txt", vbCrLf & SysText(124)), _
            ParseAction(SetMsg.SetQOut, PName, "", ""), PName, Level, _
            VisLevel, True, False, True, 0
    
        Socket.Close
        If PlayerList.Remove(PName) <> False Then
            Debug.Print PName & " has logged off."
            Else
            Debug.Print "There was a problem removing " & PName & "."
        End If
        Class_Terminate
        SubMain.KillPlayer Me
    End Sub
    This sets all references to the internal objects in the Persona class to nothing and then sets the object itself to nothing in the SubMain module.

    I logged on and quit several times and waited between each attempt to see what would happen and the bug seems to have been squished
    Thanks for your help!
    -Excalibur

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Objects can't be set to nothing. In fact this is the term that confuses you and a lot of vb programmers that haven't been in touch with c++ or the concept of pointers in general.

    The COM objects differs from the ones in c++ though, it stores how many times you've refered to it (or at least what i think would be the only solution to the phenomenon) and when the counter drops to 0, it destroys the object. This doesn't happen between ActiveX components which makes it more complicated.

    Anyway the variables store a pointer to the objects, so at the point you set Player to nothing, Player will be a null pointer and the object is still intact, in fact the counter in the object is 1 since PlayerList has a reference in it, so to terminate it you would need to remove it from PlayerList.

    Also Variables declared in procedures runs out of scope when the program exits the procedure, which also dereferences Player from the object making that line with set nothing unnessesary.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width