Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/C++    [ Add a report in this area ]  
Report #:  120654   Status: Closed
[Win64] new[] initialization failure
Project:  C++Builder Build #:  XE3, ... XE5
Version:    19.2 Submitted By:   Jeff kahler
Report Type:  Basic functionality failure Date Reported:  11/21/2013 11:49:38 AM
Severity:    Serious / Highly visible problem Last Updated: 4/15/2014 6:17:41 PM
Platform:    All versions Internal Tracking #:   47169
Resolution: Fixed (Resolution Comments) Resolved in Build: : XE6
Duplicate of:  None
Voting and Rating
Overall Rating: No Ratings Yet
0.00 out of 5
Total Votes: 1
Description
in the 64 bit compiler only...

a new[] operation on a class with an internal initialization function only calls the function for the [0] indexed variable only.  indexes variables [1] through [n-1] are not initialized with the class initializer.

32 bit compiler is OK.  All indexed variables are initialized with the class internal initialization process.


Steps to Reproduce:
for example:
---------
class xxx{
public:
  xxx(){
    var = 0;
    //ShowMessage("Constructor");
  }
  ~xxx(void){};
  int var;
};

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  class xxx *vc = new class xxx[6];
}
---------

vc[0].var is (correctly) initialized to 0.

but, vc[1].var through vc[5].var are not initialized, and are loaded with random junk which can be destructive when used.
Workarounds
externally initialize all variables following a new[] operation.

for example:

for (int i=0;i<6;i++) vc.var = 0;

solves the problem.
Attachment
InitTests.zip
Comments

Tomohiro Takahashi at 11/22/2013 6:48:38 AM -
> Build No: C++Builder? XE
What version(build no) of C++Builder do you use, for example C++Builder XE5 Update1(19.0.13856.4978) etc...?

and, could you please attach sample project to reproduce/confirm your issue?

Jeff kahler at 2/8/2014 3:16:40 PM -
I cannot find any provisions to submit projects in Quality Central, so I just sent you two XE5 samples via Hightail; InitTest32 which works perfectly, and InitTest64 which fails.

Failures are echoed out in the Memo component when you push the button.  The number of executions of the initializer is also echoed out.  InitTest32 is seen to execute ALL initializations.  InitTest64 executes only 1, the index 0 initialization.

Note that in InitTest64 the destructor had to comment out the pointer destruction because the pointers for indexes 1 through n-1 do not (necessarily) initialize correctly.


Hope this Helps,

Jeff

Tomohiro Takahashi at 2/8/2014 4:21:54 PM -
> I cannot find any provisions to submit projects ...
Please use Windows Native QC client to attach a zip file to your existing reports.
The standalone client(QualityCentral.exe) is included in C++Builder.

Jeff kahler at 2/10/2014 12:40:04 PM -
What is a , " Windows Native QC client" and how is it used to submit zip files.  Please provide detailed, step by step instructions.

Tomohiro Takahashi at 2/10/2014 8:16:38 PM -
QualityCentral.exe is located in '..\12.0\QualityCentral' folder. and you can run it via IDe menu, [Tools] | [Quality Central].

How to (re)attach a zip file to your exisiting report.
1. launch QualityCentral.exe and login
2. lookup your existing report
3. activate [Attachments] tab
4. show popup menu
5. select [Add Attachment...]
    (note: you may need to remove previous files before re-attaching new .zip)
6. select a .zip file

Jeff kahler at 2/13/2014 2:55:58 PM -
I found a way to submit the sample projects in C++ builder.  A sample project is attached in both 32 bit and 64 bit mode, with 32 bit mode clearly seen to pass and 64 bit mode clearly seen to fail.

Jeff kahler at 2/8/2014 2:51:23 PM -
Also reproducible on C++ Builder XE5, Update 2

Jeff kahler at 2/2/2014 2:13:08 PM -
That would be: C++ Builder XE3, Update 1.

Jean-Marie Babet at 2/16/2014 11:16:46 AM -
FYI: I am looking at this issue and it seems related to the usage of "class" after new. IOW, the following works, but if you uncomment the "class", then the bug shows up:(.

{code}
extern "C" int printf(const char*, ...);

class xxx{
public:
  xxx() {
    var = 0;
    printf("%s\n", __func__);
  }
  ~xxx() {
    printf("%s\n", __func__);
  }
  int var;
};

int main() {
  xxx *p = new /*class*/ xxx[6]; // "class" here shows the bug!!
  for (int i=0; i<6; ++i) {
    printf("%d -> %d\n", i, p[i].var);
  }
  delete [] p;
}
{code}

I will relay my findings after stepping through a bit.

Server Response from: ETNACODE01