Results 1 to 5 of 5

Thread: Shared members and Finalize

  1. #1

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Shared members and Finalize

    If I have a class that has a Shared SQLConnector in it where do I put my clean up code? Or do I not put it?

    I mean is there a way to tell if there are no more instances of the class left and then perform the clean up in an Overrides Finalize method? Or is it safe to assume the garbage collection will handle this good enough?

    The connection isn't left open so the main clean up is just to make sure its set back to nothing or maybe a backup check to make sure it IS closed and if not to close it (although like I said it shouldn't be open anyway).

    Or do you think I should just share a string instead and use it as the connection string instead of the whole connection object?

  2. #2
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    I think you should always close and clear up a connection esp. across a busy network. The garbage collector, although a great idea, doesn't have any routinue so you don't know how long before it will tidy up for you.
    Is using a shared connection the best idea?? How do you know if all objects have finished using it? I usually open a connection everytime i want a dataset/reader and then close it immediately. Maybe I'm doing it oldy style? Only my thoughts?!
    Wind and waves resolves all problems.

  3. #3

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I do only open and close it when needed but there are a series of items of the same class that will be using the same connection so I made it shared so they can automatically use the same one automatically. Where would you put your clean up code then? If I use Finalize then it is only the Finalize for that instance not all the instances thats kind of the problem. Thanks.

  4. #4
    Addicted Member
    Join Date
    Mar 2001
    Location
    Devon, UK
    Posts
    181
    Microsoft says

    There is a slight performance penalty for executing Sub Finalize, so you should define a Sub Finalize method only when you need to release objects explicitly.
    I have a closeconn sub which i call after i get my dataset etc.

    You imply that the shared connection is within the class
    series of items of the same class that will be using the same connection
    If you use the sub finalize that will kill off the shared connection. Creating a new instance of the class will then create a new instance of the shared connection - don't really see the problem?
    Wind and waves resolves all problems.

  5. #5

    Thread Starter
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Then I'd have to recreate it everytime I made a new instance defeating the purpose of having it shared.

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