Memory Leak When Copying Charts

C

Claus Zimmermann

Hi

I have a BAD memory leak when I copy a chart... even a blank chart to
another worksheet. It eventually causes excel to crash.

In my example, the sequence to crash excel was as follows:
- Copy Chart 5000 times (loop) - less times would work too, and you'd
see the increase in private bytes as you go along
- Copy method of range class failed
- Clicked End button on debug message box
- Closed Excel
- Message: The picture is too large and will be truncated
- Excel.exe - Application error. The instruction at "0x300dc756"
referenced memory at "0x000000008". The memory could not be read.

I'm using Excel 2000 sp3

To test memory I'm using perfmon, and am testing private bytes in Excel.

Any suggestions or a workaround?

Sample macro available illustrating this on request.

Claus
 
T

Tom Ogilvy

Perhaps turn off font scaling. This was the source of the problem in
earlier versions of excel.
 
C

Claus Zimmermann

- Have turned off font scaling,
- Do not use the clipboard for copying
- Text works fine, pictures work fine, but not charts

I also read a MS Knowledge Base Article 264986 for XL97 which said there was
an error in copying worksheets containing charts.

In in my actual application, I add a set amount of graphs, copy the
worksheet to another workbook, and then I destroy the worksheet. Same
problem

Interestingly, I tried copying the graphs to the same sheet as the graph,
and there was no crash although there was a climb in private bytes, which
might be confounded by file size?? I'm not well clued up in terms of the
inter-relationships of memory.

If you want to try out what I do:
1. Open new excel sheet
2. Create a user form
3. Add a button on the form (Command1)
Paste following code:
'***************************************************************************
****

Option Explicit

'holds the table descriptors as a custom data type
Private Type TableCoords
nTopRow As Integer
nNoOfRows As Integer
nFirstColumn As Integer
nNoOfColumns As Integer
End Type

Dim m_nCurrentWorkSheetLinePosition As Integer
'##################################################################
'Method : Copy_Lines
'Purpose : Copies rows of text from the template to the report
sheet
'Parameters : ByVal nTopRow As Integer - the top row to be copied,
_
ByVal nBottomRow As Integer - the bottom row, _
ByVal sFromWorkSheet As String - the sheet from
where the data comes, _
Optional ByVal sToWorkSheet As String =
g_sWorkSheet_ReportPage - the data where the data is going to be placed
'Return :
'##################################################################
Private Sub Copy_Lines(ByVal nTopRow As Integer, _
ByVal nBottomRow As Integer, _
ByVal sFromWorkSheet As String, _
Optional ByVal sToWorkSheet As String)

On Error GoTo errHandler 'If any error occurs, go to the line
entitled ErrHandler 'catches any errors that may occur

Dim nOldPosition As Integer 'holds the previous row where last copied

nOldPosition = m_nCurrentWorkSheetLinePosition 'get the last line
position before it gets reset

Dim oFromSheet As Worksheet 'holds the sheet from where the data is
being taken
Set oFromSheet = Sheets(sFromWorkSheet) 'assign the variable to the from
sheet


Dim RightSideOfTemplateDimensions As TableCoords 'holds the coordinates
of the right side of the template

RightSideOfTemplateDimensions.nFirstColumn = oFromSheet.Cells(41,
6).Value 'get the first column

RightSideOfTemplateDimensions.nNoOfColumns = oFromSheet.Cells(41,
7).Value 'get the number of columns

Dim nLastcolumn As Integer 'holds the last column where data needs to be
pasted to
nLastcolumn = RightSideOfTemplateDimensions.nFirstColumn +
RightSideOfTemplateDimensions.nNoOfColumns 'get the last column

Dim oToSheet As Worksheet 'holds the sheet where data will be pasted
Set oToSheet = Sheets(sToWorkSheet) 'assign the variable to the to sheet


oFromSheet.Activate 'activate the from sheet


oFromSheet.Range(Cells(nTopRow, 1), Cells(nBottomRow,
10)).EntireRow.Copy
Destination:=oToSheet.Range(Cells(m_nCurrentWorkSheetLinePosition,
1).Address) 'copy the data in the selected area


m_nCurrentWorkSheetLinePosition = m_nCurrentWorkSheetLinePosition +
nBottomRow - nTopRow + 1 'set the current worksheet line position

' 'control flow: if the position has not changed, increment the worksheet
position
' If m_nCurrentWorkSheetLinePosition = nOldPosition Then
' m_nCurrentWorkSheetLinePosition = m_nCurrentWorkSheetLinePosition +
1
' End If


Set oToSheet = Nothing 'set the variable to nothing
Set oFromSheet = Nothing 'set the variable to nothing


Exit Sub
errHandler:
Set oToSheet = Nothing
Set oFromSheet = Nothing

Err.Raise Err.Number, Err.Source, Err.Description 'raise the error

End Sub


Private Sub CommandButton1_Click()
Dim nLoop As Integer
For nLoop = 1 To 5000
Copy_Lines 1, 5, "Sheet1", "Sheet2"
Next
End Sub

Private Sub UserForm_Activate()
m_nCurrentWorkSheetLinePosition = 1
End Sub



'***************************************************************************
*******
 
B

Bob Flanagan

Claus, try changing your printer to a Brothers Laser, even if you do not
have one installed. Some print drivers cause memory problems, especially HP
print drivers.

Bob Flanagan
Macro Systems
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top