Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Eliminating Weak References In My App

Status
Not open for further replies.

aultmike

Programmer
Feb 9, 2009
9
Hello. I am trying to resolve a memory leak(s) in my app. I've downloaded and ran RedGate's ANTS Memory Profiler 5.0 and the memory profiler tells me the leak has to do with WeakReferences.

The problem I am running into is that I've never heard of a WeakReference nor are they explicitly declared in my app. From the reading I've done I believe weak reference's are created when you have an object/resource that is trying to be destroyed but cant because too many other objects keep referencing it. I assume much the same way that a file can't be deleted because it is still in use.

So my question is how do I determine where these weak references are coming from? I have suspicions that it might be the use of ByRef? Another collegue suggested hashtables.

Hoping to get some clarification on weakreference detection and elimination and some clarification on my suspicions.

Thanks.
 
First, I don't know what is meant by WeakReferences either. I read a little real quick and would actually term it another way. It isn't that it can't be destroyed, but that it isn't. It may seem like semantics, but not really. For instance you can't dispose of your program while using it because it is running. You can put things into a can't situation, but that isn't what makes them a weak reference. From what they are saying a global variable would be an instance of a weak reference. It is then part of the class it is in and isn't disposed of until that class is. Second, I don't know how much you know so if something I say seems obvious then ignore it.

So ByRef passes a reference to an object. You only want to use it if the object is persistent and you need to deal with the object directly like a control on a form, if you are going to return to that reference for instance when you passed it to a function that is going to modify it and return a value, or passed to a sub in something like a loop where the object is going to continue to be used. The latter two mean that once the calling sub/function is finished with the object it can then be disposed of. Any other time you only want to pass the value (ByVal) of something.

Could that be a problem? Yes. Is that the problem? That is harder to say. If you are already using ByRef in that manner then that shouldn't be the problem because the objects still exist and are needed. It might be helpful for you to say what your program actually does. For instance if you do a lot of data collection and use a lot of DataTable/DataSets/etc watch that you are closing the connection they use and allowing them to be disposed when you don't need them any more.

Really the best way I've found to find a memory leak is actually watch the memory usage when using the program. If for some reason you can't the I would suggest adding a log file that the program writes it's memory usage to and a keyword as to what part of the program is running at the time. At least that might help you narrow down the code you need to look at.

I hope something in all of that will help you (and make sense). If not like I said a little more info on the program itself may be helpful.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
For clarification "when you passed it to a function that is going to modify it and return a value" should read as "when you passed it to a function that is going to modify it and return a value to the calling sub/function" you use a byref so you don't have to return the value directly to the object.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top