|
1 <?xml version="1.0" encoding="utf-8"?> |
|
2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. --> |
|
3 <!-- This component and the accompanying materials are made available under the terms of the License |
|
4 "Eclipse Public License v1.0" which accompanies this distribution, |
|
5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". --> |
|
6 <!-- Initial Contributors: |
|
7 Nokia Corporation - initial contribution. |
|
8 Contributors: |
|
9 --> |
|
10 <!DOCTYPE concept |
|
11 PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd"> |
|
12 <concept id="GUID-011D0974-CC37-5335-A8EB-7ECF4FC30F93" xml:lang="en"><title>Cleanup |
|
13 for heap arrays</title><shortdesc>C++ arrays are allocated on the heap and require their own particular |
|
14 cleanup.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody> |
|
15 <p>C++ arrays are allocated on the heap using <codeph>operator new[]</codeph>. |
|
16 The cleanup rules for such arrays are as follows:</p> |
|
17 <ul> |
|
18 <li id="GUID-41AA28AA-1D48-5B50-9065-A283CCAAD83F"><p>if it is not necessary |
|
19 to protect against leaves, such as when the array is a class member, or no |
|
20 leaves can occur in the lifetime of the array, then always delete the array |
|
21 using <codeph>operator delete[]</codeph> (not <codeph>operator delete</codeph>).</p> </li> |
|
22 <li id="GUID-B4EA79E7-EEDC-531D-90DB-7041F3650356"><p>if it is necessary to |
|
23 protect against leaves, push the array to the cleanup stack using the utility |
|
24 template function <codeph>CleanupArrayDeletePushL()</codeph>. This ensures |
|
25 that if a leave occurs, the array is deleted correctly using <codeph>operator |
|
26 delete[]</codeph>.</p> <p>Arrays should <i>not</i> be pushed to the cleanup |
|
27 stack using the standard <codeph>CleanupStack::PushL()</codeph>, as this will |
|
28 result in <codeph>operator delete</codeph> rather than <codeph>operator delete[]</codeph> being |
|
29 used for cleanup.</p> </li> |
|
30 </ul> |
|
31 <p><b>Note on compiler behaviour</b> </p> |
|
32 <p>Of the compilers used with Symbian platform, Metrowerks CodeWarrior is |
|
33 the most sensitive to the rules for array deletion. If an array is deleted |
|
34 with a simple <codeph>delete</codeph> rather than <codeph>delete[]</codeph>, |
|
35 then a USER 42 panic (invalid heap cell) can occur. </p> |
|
36 <p>This panic in fact occurs for arrays in which the class has either a constructor |
|
37 or a destructor. For such arrays, CodeWarrior reserves space at the start |
|
38 of the array storage to hold the number of elements in the array: this means |
|
39 the first heap cell is not the start of a deletable object. GCC and MSVC C++ |
|
40 only reserve such extra space if the class has a destructor.</p> |
|
41 </conbody></concept> |