Link to home
Start Free TrialLog in
Avatar of frankd
frankdFlag for United States of America

asked on

Memory leak in my ocx

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
Avatar of roverm
roverm
Flag of Netherlands image

Interresting problem!

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

D'Mzzl!
RoverM
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

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
Avatar of frankd

ASKER

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
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
Avatar of frankd

ASKER

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
Avatar of Vinda
Vinda

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.

Avatar of frankd

ASKER

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
The sample code makes it appear the leak is in your OCX.
mind sending it so i can take a look?

Good Luck :/
Janus
Avatar of frankd

ASKER

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
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
janusfury@citlink.net
should be in my profile
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
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
I think you're doing something really wacky here.
are you using ANY API calls at all?
Avatar of frankd

ASKER

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
a lot of transparent controls and such can eat resources... still haven't gotten your ocx in email (janusfury@citlink.net)
Avatar of frankd

ASKER

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
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.
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 :(
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


Avatar of frankd

ASKER

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
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
Avatar of frankd

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of roverm
roverm
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of frankd

ASKER

Thanks for all the assistance.
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
Whoops! Frank, sorry, I see that you posted other questions for JanusFury and Vinda.

Avatar of frankd

ASKER

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.
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.
Bump! read this roverm! Problem solver!
You are right!
I downloaded SP5 and it worked fine!

Good one JanusFury!

D'Mzzl!
RoverM
Avatar of frankd

ASKER

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
Avatar of frankd

ASKER

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