AcArray problem

AcArray problem

Anonymous
Not applicable
467 Views
3 Replies
Message 1 of 4

AcArray problem

Anonymous
Not applicable
Hello! I have any problem with AcArray class: Here is a code for example: template AcArray segArray; segArray lineSegArray; for(;....) { AcGeLineSeg2d segObj; ...................................... lineSegArray.append(&segObj); } ........................................... int iLen = lineSegArray.length(); for(i=0; i
0 Likes
468 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Benjamin, I haven't tried this myself, but I think this is a simple scoping problem. The address of the original segObj objects are only valid within the for ... loop, i.e. when you try to recover the pointers, the AcArray contains a list of pointers to objects that no longer exist. What you need to do is something like for(;....) { AcGeLineSeg2d *segObj = new AcGeLineSeg2d(...); ...................................... lineSegArray.append(segObj); } "Benjamin" wrote in message news:3ff91da9$1_5@statler... > Hello! > I have any problem with AcArray class: > Here is a code for example: > template AcArray segArray; > segArray lineSegArray; > for(;....) > { > AcGeLineSeg2d segObj; > ...................................... > lineSegArray.append(&segObj); > } > ........................................... > int iLen = lineSegArray.length(); > for(i=0; i { > AcGeLineSeg2d *segObj, segObj1; //for example > segObj = lineSegArray.at(i); //ok > segObj1 = *segObj; // "Unhandled exception C0000005(Access Violation.......) > hereabout error? > > Thanks > Andy > > > > >
0 Likes
Message 3 of 4

Anonymous
Not applicable
> AcGeLineSeg2d *segObj, *segObj1; //for example instead > AcGeLineSeg2d *segObj, segObj1; //for example Regards, Petra. Benjamin wrote: > Hello! > I have any problem with AcArray class: > Here is a code for example: > template AcArray segArray; > segArray lineSegArray; > for(;....) > { > AcGeLineSeg2d segObj; > ...................................... > lineSegArray.append(&segObj); > } > ........................................... > int iLen = lineSegArray.length(); > for(i=0; i { > AcGeLineSeg2d *segObj, segObj1; //for example > segObj = lineSegArray.at(i); //ok > segObj1 = *segObj; // "Unhandled exception C0000005(Access Violation.......) > hereabout error? > > Thanks > Andy > > > > >
0 Likes
Message 4 of 4

Anonymous
Not applicable
"Benjamin" schrieb: > Hello! > I have any problem with AcArray class: > Here is a code for example: > template AcArray segArray; > segArray lineSegArray; > for(;....) > { Here you create a lines segment on the stack > AcGeLineSeg2d segObj; > ...................................... Here you copy the adress of the stacke base object into the array > lineSegArray.append(&segObj); > }After this brace, segObj goes out of scope and will get destroyed > ........................................... > int iLen = lineSegArray.length(); > for(i=0; i { > AcGeLineSeg2d *segObj, segObj1; //for example Hery you extract an address from the array and copy it into segObj. Note that the actualobject that the adresses points to is dead, as it was destroyed after leaving ts scope. However, as you see you can still use the address of it > segObj = lineSegArray.at(i); //ok here you dereference segObj, an adress that is pointing where the allready destroyed linesegment was. This is undefined behavior :-( > segObj1 = *segObj; // "Unhandled exception C0000005(Access > Violation.......) hereabout error? > > Thanks > Andy > So there are several ways to solve this: 1. use dynamic allocation for(;....) { AcGeLineSeg2d * segObj = new AcGeLineSeg2d; lineSegArray.append(segObj); } However, you must delete the allocated lines after using them: for(;....) delete lineSegArray; 2. Better, but a bit more expensive: Use copy by value typedef AcArray > AcGeLineSeg2dArray;//note the use of the copy //realocator AcGeLineSeg2dArray lineSegArray; for(;....) { AcGeLineSeg2d segObj; lineSegArray.append(segObj); } Note this only works if the objects have a copy ctor and a assignment operator defined, but as far as I remember all acGe entities do that 3. Use dynamic allocation in combination with a smart pointer. Do not use std::auto_ptr here, as it does not fullfill the requierements for AcArray and are illegal and dangerous to use in containers according to the C++ standard. If you wnat to know the details, take a look here: http://www.codeguru.com/cpp_mfc/COAP.html I would recommend boost::shared_ptr: http://www.boost.org/libs/smart_ptr/shared_ptr.htm . The smart pointer will automatically delete the allocated memory for you: typedef boost::shared_ptr AcGeLineSeg2dSharedPtr; typedef AcArray > AcGeLineSeg2dSharedPtrArray;//note the use of //the copy realocator again AcGeLineSeg2dArray lineSegArray; for(;....) { AcGeLineSeg2dSharedPtr segObj(new AcGeLineSeg2d); lineSegArray.append(segObj); } usage: for(;....) { AcGeLineSeg2dSharedPtr tmp = lineSegArray; tmp->length();// do something just as if you use a ptr } HTH, Andy
0 Likes