Settings Things to Nil

I’ve been studying all of the posts on memory leaks to educate myself and try to locate and clean up a leak in a web app I have.

So, here’s a general question:

When you declare a new object, say a web dialog or a database connection, is closing them when you’re done sufficient or do you need to set them to Nil to free them up?

For example, if I use

dim mydb as new MySQLCommunityServer //connect to the database and do work mydb.close

Will that finalize it or should I put something like

mydb = Nil

after I close it?

mydb will be automatically set to Nil at the end of the method. If you do not call Close before the end of the method, Close will be called for you before mydb is set to Nil.

Every object will be freed as soon as it goes out of scope, i.e., nothing in your code is referencing it, no need to explicitly set anything to nil. You just have to avoid circular references, i.e., object A stores object B which stores object A.

Got it. I must have a circular reference somewhere and just don’t realize it. I’ll keep looking!

Remember that assigning an object to an array, pair, or Dictionary (as value or key) counts as a reference to that object. Assigning an object to itself is possible and would be an instant circular reference.

I’m pretty sure I only have strings and integers used in arrays or Dictionaries and the only time I use the term Pair is when using web radio buttons.

If anyone thinks of any other things that caused a memory leak, I would appreciate it–I’m sure it’s some bad doing technique I’ve done that causes it.

The Dev Center has some thoughts on memory leaks, particularly circular references:

[code]

  • App has references to Sessions
  • Sessions have references to Views (Pages and non-implicit dialogs)
  • Views have references to Controls (and implicit dialogs and Containers)
  • Implicit dialogs and Containers have references to Controls (and other implicit dialogs and Containers)[/code]

http://developer.xojo.com/userguide/web-app-optimization$memory-leaks

[quote=329964:@Peter Stallo]I’m pretty sure I only have strings and integers used in arrays or Dictionaries and the only time I use the term Pair is when using web radio buttons.

If anyone thinks of any other things that caused a memory leak, I would appreciate it–I’m sure it’s some bad doing technique I’ve done that causes it.[/quote]

Who exactly have you identified the leak? Web apps are a bit different and things don’t necessarily happen in the order you expect.