Solved

Memory leak in my ocx

Posted on 2001-08-12
35
357 Views
Last Modified: 2006-11-17
I have written an ocx made up of multiple shapes.  Based on the function I want (at design time) different shapes are visible and others are not.

During run time I move these shapes to different locations.  Right now I have a program that uses a timer to cycle through the range of movements possible for three of these controls.  The problem I am having is that if I just let it run, it eventually starts a memory leak that continues until it crashes.  With the timer set at 100ms the memory leak starts between 3600 and 3700 loops through the timer code (Shown below). By the time it reaches 4500 loops it's very close to crash time.    I have been trying to use Heap Walker to try and find the leak but I have been unable to so far.  I have been struggling with this for over a week and I'm now at a loss as to what to try.

I am using VB 6.0 SP3 on a Windows 98 machine.  Celeron 366 processor with 160 MB Ram.  If the following code does not suggest anything, I have the entire project zipped up (78K) and could email it to anyone interested.




Dim Test As SystemItems           '   SystemItems is the ocx
   
If XYZMoveEnb = True Then
    TestCount = TestCount + 1
    Text1.Text = Str(TestCount)
    TestIndex = 0
   
    If XYZMove = True Then
        Set Test = SystemItems(TestIndex)
        Test.XCoord = Test.XCoord + 200
        If Test.XCoord > Test.Width - Test.CraneWidth Then
            Test.XCoord = Test.XCoord - 200
            XYZMove = False
        End If
        Set Test = Nothing
       
        Set Test = SystemItems(TestIndex)
        Test.YCoord = Test.YCoord + 200
        If Test.YCoord > Test.Height - Test.CraneHeight Then
            Test.YCoord = Test.YCoord - 200
            XYZMove = False
        End If
        Set Test = Nothing
    Else
        Set Test = SystemItems(TestIndex)
        Test.XCoord = Test.XCoord - 200
        If Test.XCoord < 0 Then
            Test.XCoord = Test.XCoord + 200
            XYZMove = True
        End If
        Set Test = Nothing
           
        Set Test = SystemItems(TestIndex)
        Test.YCoord = Test.YCoord - 200
        If Test.YCoord < 0 Then
            Test.YCoord = Test.YCoord + 200
            XYZMove = True
        End If
        Set Test = Nothing
    End If
End If

If ZMoveEnb = True Then
    TestIndex = 1
    Set Test = SystemItems(TestIndex)
    If ZMove = True Then
        Test.ZHeight = Test.ZHeight - 50
        Test.ZWidth = Test.ZWidth - 50
        If Test.ZWidth < Test.Width * 0.3 Then
            ZMove = False
        End If
    End If
   
    If ZMove = False Then
        Test.ZHeight = Test.ZHeight + 50
        Test.ZWidth = Test.ZWidth + 50
        If Test.ZWidth >= Test.Width Then
            ZMove = True
        End If
    End If
    Set Test = Nothing
End If

If YShuttleEnb = True Then
    TestIndex = 2
    Set Test = SystemItems(TestIndex)
    If YShuttle = True Then
        Test.ShuttleTop = Test.ShuttleTop + 100
        If Test.ShuttleTop > Test.Height - Test.ShuttleLength Then
            Test.ShuttleTop = Test.ShuttleTop - 100
            YShuttle = False
        End If
    End If
   
    If YShuttle = False Then
        Test.ShuttleTop = Test.ShuttleTop - 100
            If Test.ShuttleTop < 0 Then
                Test.ShuttleTop = Test.ShuttleTop + 100
                YShuttle = True
            End If
    End If
    Set Test = Nothing
End If

Any suggestions would be appreciated.

Thanks,

Frank
0
Comment
Question by:frankd
  • 12
  • 12
  • 9
  • +1
35 Comments
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Interresting problem!

If you mail a copy at derover@mail.com I'll take a look!

D'Mzzl!
RoverM
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Hi Frank!

I received your code and tested it:
I placed an OCX on a new form (Form1...) in a new project which I added to yours.
Then placed a Timer on Form1 with an interval of 100ms, at every interval running your code from your first comment.

Then placed another Timer(2) on Form1 with an interval of 80ms, moving your ocx every interval plus or minus 100 twips.

But nothing happens according to my TaskManager.

I.e. : I don't get any errors!

Is this the way to test it ?

D'Mzzl!
RoverM

0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Whoops: Didn't see it before trying to save the project group: You did include a testproject... sorry.

But, when I try to load this, I get an error message "missing Class Rect (CRECT)".

Can you send that class also ?

D'Mzzl!
RoverM
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
RoverM,

The class that you were missing is extra junk that I should have deleted.  I'm sending you the same stuff but with the extra junk removed.

I get the same results with all the class stuff removed.  One interesting thing, once my system resources start going down, I can stop my application re-start it and I start losing system resources immediately unlike when I first start it takes several 1000's of cycles before I lose resources.

Hope the new file works for you.

Thanks,

Frank
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Hi Frank!

Tried your program (again ;-)), but everything seems to be working fine! I ran it for 5 minutes but my system resources allways went down to zero.

I am using a PIII-650 with 256MB, W2K-SP2.

Any suggestion how to test it more ?

D'Mzzl!
RoverM
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
RoverM,

Thanks for the update.  

With my timer at 100ms, my screen would start showing signs of memory loss between 4000 and 5000 iterations (about 8-1/2 minutes).  This happened when my system resources got in the 20% to 30% range.

Maybe it's not a problem with my program, but with my system, although I've shut down all programs except the one you have.

I'll have to give it some more thought this evening and post another comment then.

Thanks,

Frank
0
 
LVL 1

Expert Comment

by:Vinda
Comment Utility
Frank,

Would u send me the same code on v_divekar@hotmail.com. Even i had faced the same problem and would be possible if the same problem exists in ur code by lookin at it.

0
 
LVL 4

Author Comment

by:frankd
Comment Utility
RoverM,

I worked on it some more last night with the same results. I tried it on three different computers (OS's), Win98, Win2000, and Win95B.

I'm not sure what to do next, I'll give it more thought today.

Vinda,

I'll email it tonight when I get home.  Hope you can help.

Thanks,

Frank
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
The sample code makes it appear the leak is in your OCX.
mind sending it so i can take a look?

Good Luck :/
Janus
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
JanusFury,

Thanks for the offer, unfortunately I do not have a site that my project can be downloaded from.  So, I would need your email address to get the project to you.

For anyone interested, I've continued to work on this project and now have the following observations.

1 - I removed all programs from the system tray
2 - Started my test app and started three independant motions. At the beginning, I had about 74% free system resources.
3 - After about 4000 moves I started to loose system resources.
4 - I let it run all night (284,400 moves) and I was down to 33% free system resources.
5 - The front most item that is being moved does not repaint properly.
6 -Title bar has lost its default color, now white.
7 - My XY position text went to bold and the mouse move event no longer works.
8 - stopped my test app and the title bar default color came back.

I realize that without my test app all my observations probably mean little or nothing.

I will continue to plug away at this in the hopes that someone (myself included) will have a brain storm.

Thanks

Frank
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Hi Frank!

I understand from your latest comment that you have created a test app. If you send it to me, I will let it run also !

D'Mzzl!
RoverM
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
janusfury@citlink.net
should be in my profile
0
 
LVL 1

Expert Comment

by:Vinda
Comment Utility
even i could reproduce the problem on my machine. After about 4500+ iterations the screen repaint nearly stopped. I could see soem movements on the screen but the system was really in a inconsistent state.

I also noticed some really weird behavior. Even after i closed the running application, the system menus and other app menus were all corrupt. RMB would be all black and as we traverse the menus each menu would come back but then with black border. I had to reboot the machine.

I am still debugging the app. Will put in more inputs later
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Ok, now I could test it !
The problem was that I didn't see the buttons since my screen resolution was in 1024x768...

Got the same result as Vinda.
Going to do the same as Vinda. ;-)

D'Mzzl!
RoverM
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
I think you're doing something really wacky here.
are you using ANY API calls at all?
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
Hi,

I was looking at it again last night and I made an observation.

The test app is made up of the same ocx but some items are on top of others.  The ones that are on top have there background property set to transparent so that the control behind them are visible.  When the problem occurs, it seems that the transparancey is turned off.

JanusFury,

I'm not using any API's

Thanks

Frank
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
a lot of transparent controls and such can eat resources... still haven't gotten your ocx in email (janusfury@citlink.net)
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 4

Author Comment

by:frankd
Comment Utility
Hi,

The cause of my problem is the following line:

UserControl.BackStyle = 0

In the UserControl Resize event.

I now need a work around because I need some controls to be on top of other controls.

JanusFury, I missed your email earlier.  Source code is on the way.

Thanks,

Frank
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
Well, I'm running your EXE right now and there seem to be no problems... It just hit 6500.

It might be because I have windows xp, but your app is using 2% cpu, and 4mb of ram. It's been that way the whole time it's been running.
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
Well, It's now at 22100, so I have a feeling my machine isn't going to be any help in debugging it.

I tried the VBP, but I had trouble getting it to work... So I tried the EXE instead. What does this program do, anyway?

Sorry :(
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Hi Frank.

I ran it and encountered the problem at approx. 3500.

Then I disabled the AutoRepaint option from frmWhatIf and then it crashed at 5200.

So it has to do something with that!
In every situation my resources maximum is at 10%.

I remarked the lines you think it is. And now every runs fine (I just hit 50000 cycles...). And the resources are down to less than 2%.

The be continued...

D'Mzzl!
RoverM


0
 
LVL 4

Author Comment

by:frankd
Comment Utility
JanusFry,

Eventually I hope this program will simulate an overhead crane, showing it's movements etc.  That is if I ever get by this first little hurdle.

Roverm,

Thanks for the update, I will also continue to look into different possibilities.

Thanks

Frank
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Frank,

Any progress ?
I still can't find the reason.
I let it run for 24 hours, and no problem at all!

D'Mzzl!
RoverM
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
RoverM,

No luck yet, based on what JanusFry had to say I'm working on getting a copy of Windows XP.  If it doesn't happen with that then it must be a Windows 95,98,NT,2000 problem.

I even got a copy of VB net to see if that would fix it.  Right now it looks like to much conversion (VB net doesn't support shapes).

When you say you let it run for 24 hrs. was that with the UserControl.BackStyle=0?

For now, I'm going to leave this question open.  Even if I get an workable answer, I will spread some points around for the assistance that everyone has given me.

I'll keep you all posted.

Thanks

Frank
0
 
LVL 12

Accepted Solution

by:
roverm earned 100 total points
Comment Utility
Hi Frank!

I let it run with the lines remarked.

If it's alright with you can you please close the question instead of leaving it open.
You can always post another comment after closing it and we all get an email again!

You can close it by grading it or by asking CS to PAQ it and refund your question points.

D'Mzzl!
RoverM
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
Thanks for all the assistance.
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Hi Frank!
Thanks for the points, but your problem wasn't solved by me ! If you like you can ask CS to split the points.

D'Mzzl!
RoverM
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
Whoops! Frank, sorry, I see that you posted other questions for JanusFury and Vinda.

0
 
LVL 4

Author Comment

by:frankd
Comment Utility
I don't ask a lot of questions so I have points to burn so to speak.  

Thanks again,

Frank

P.S. If I ever get a definitive answer I'll add it as a comment here.
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
I FOUND YOUR ANSWER! :D

In the Visual Basic SP5 Info page, under fixes:
User controls with transparent BackStyle and fixed single BorderStyle caused excessive memory usage.

Download and install the Visual Basic SP5 or Visual Studio SP5 update and everything should be fine! (Which explains why it worked fine on my PC.)

:) Sorry I took so long, this was an accidental discovery.
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
Bump! read this roverm! Problem solver!
0
 
LVL 2

Expert Comment

by:JanusFury
Comment Utility
Oh yes, here's a link to download SP5:
http://msdn.microsoft.com/vstudio/sp/vs6sp5/default.asp
0
 
LVL 12

Expert Comment

by:roverm
Comment Utility
You are right!
I downloaded SP5 and it worked fine!

Good one JanusFury!

D'Mzzl!
RoverM
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
I have a copy of SP 5, I'll install it and check it out.  I've already posted another question awarding points to JanusFury, if this works, I'll increase the points.

Thanks,

Frank
0
 
LVL 4

Author Comment

by:frankd
Comment Utility
You're right, I loaded SP5 and no more problems.  You're a life saver.  I feel kind of stupid right now, I should have checked that first off.

Since I've already awarded points to RoverM for assistance, I posted another question for you.  As soon as I post this comment I'll increase the points on that question for you.

Thanks to all,

Frank
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now