Pointer problem??

Pointer problem??

Anonymous
Not applicable
352 Views
4 Replies
Message 1 of 5

Pointer problem??

Anonymous
Not applicable
I think I'm having a problem with a pointer but cannot put my finger on
it. Can someone explain why this function would cause a fatal error?

m_StartSectionVerts & m_EndSectionVerts are declared as a private member
variables like this:
AcGePoint3d *m_StartSectionVerts;
AcGePoint3d *m_EndSectionVerts;

here is the function:
void MyObject::SetCrossSection(AcGePoint3d* verts, Adesk::UInt32
numVerts)
{
m_numStartSectionVerts = numVerts;

m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
m_StartSectionVerts = verts;

m_numEndSectionVerts = numVerts;
m_EndSectionVerts = new AcGePoint3d[(m_numEndSectionVerts - 1)];

for (int vertexNumber = 0; vertexNumber <= (m_numEndSectionVerts - 1);
vertexNumber++)
{
m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
m_EndSectionVerts[vertexNumber] = 10.00;

acutPrintf("\nStart = %.3lf,%.3lf,%.3lf",
m_StartSectionVerts[vertexNumber],
m_StartSectionVerts[vertexNumber],
m_StartSectionVerts[vertexNumber] );
acutPrintf("\nEnd = %.3lf,%.3lf,%.3lf",
m_EndSectionVerts[vertexNumber], m_EndSectionVerts[vertexNumber],
m_EndSectionVerts[vertexNumber] );

}
}
0 Likes
353 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
Hi
You have to check these:
1-You allocate your member variable with new []. make sure if you delete
with delete []. ex: delete [] m_StartSectionVerts;
2-On your constructor set your member variables pointes to NULL;
3-On your destructor use delete[] on your member variable pointes
3-On your SetCrossSection function don´t forget to, before everything,
delete your member variable pointers

PS:check your SetCrossSection function:
m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
m_StartSectionVerts = verts;
This way, you have a lost pointer because you allocate your member variable
with new and after you set
this pointer to m_StartSectionVerts

Aragao

"Bill Wright" escreveu na mensagem
news:[email protected]...
> I think I'm having a problem with a pointer but cannot put my finger on
> it. Can someone explain why this function would cause a fatal error?
>
> m_StartSectionVerts & m_EndSectionVerts are declared as a private member
> variables like this:
> AcGePoint3d *m_StartSectionVerts;
> AcGePoint3d *m_EndSectionVerts;
>
> here is the function:
> void MyObject::SetCrossSection(AcGePoint3d* verts, Adesk::UInt32
> numVerts)
> {
> m_numStartSectionVerts = numVerts;
>
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
> m_StartSectionVerts = verts;
>
> m_numEndSectionVerts = numVerts;
> m_EndSectionVerts = new AcGePoint3d[(m_numEndSectionVerts - 1)];
>
> for (int vertexNumber = 0; vertexNumber <= (m_numEndSectionVerts - 1);
> vertexNumber++)
> {
> m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> m_EndSectionVerts[vertexNumber] = 10.00;
>
> acutPrintf("\nStart = %.3lf,%.3lf,%.3lf",
> m_StartSectionVerts[vertexNumber],
> m_StartSectionVerts[vertexNumber],
> m_StartSectionVerts[vertexNumber] );
> acutPrintf("\nEnd = %.3lf,%.3lf,%.3lf",
> m_EndSectionVerts[vertexNumber], m_EndSectionVerts[vertexNumber],
> m_EndSectionVerts[vertexNumber] );
>
> }
> }
>
0 Likes
Message 3 of 5

Anonymous
Not applicable
Don´t forget,
Instead of use AcGePoint3d[], you can use AcGePoint3dArray too.


"Bill Wright" escreveu na mensagem
news:[email protected]...
> I think I'm having a problem with a pointer but cannot put my finger on
> it. Can someone explain why this function would cause a fatal error?
>
> m_StartSectionVerts & m_EndSectionVerts are declared as a private member
> variables like this:
> AcGePoint3d *m_StartSectionVerts;
> AcGePoint3d *m_EndSectionVerts;
>
> here is the function:
> void MyObject::SetCrossSection(AcGePoint3d* verts, Adesk::UInt32
> numVerts)
> {
> m_numStartSectionVerts = numVerts;
>
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
> m_StartSectionVerts = verts;
>
> m_numEndSectionVerts = numVerts;
> m_EndSectionVerts = new AcGePoint3d[(m_numEndSectionVerts - 1)];
>
> for (int vertexNumber = 0; vertexNumber <= (m_numEndSectionVerts - 1);
> vertexNumber++)
> {
> m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> m_EndSectionVerts[vertexNumber] = 10.00;
>
> acutPrintf("\nStart = %.3lf,%.3lf,%.3lf",
> m_StartSectionVerts[vertexNumber],
> m_StartSectionVerts[vertexNumber],
> m_StartSectionVerts[vertexNumber] );
> acutPrintf("\nEnd = %.3lf,%.3lf,%.3lf",
> m_EndSectionVerts[vertexNumber], m_EndSectionVerts[vertexNumber],
> m_EndSectionVerts[vertexNumber] );
>
> }
> }
>
0 Likes
Message 4 of 5

Anonymous
Not applicable
Bill,

The acutPrintf function is very sensitive
so if you are trying to print past the scope of the array as in your case
it will crash!

I have looked at your code and have no idea what it all about but I suspect
that you are past the end of the array length;

This line : > for (int vertexNumber = 0; vertexNumber <=
(m_numEndSectionVerts - 1);
should read 😆 for (int vertexNumber = 0; vertexNumber <
(m_numEndSectionVerts);

and
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
probably shoud real
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts )];
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts)];

and stop using <= with a minus one
> for (int vertexNumber = 0; vertexNumber <= (m_numEndSectionVerts -
1); vertexNumber++)
should read
> for (int vertexNumber = 0; vertexNumber < (m_numEndSectionVert);
vertexNumber++)

Regards Gary
www.ltfactory.com


The
"Bill Wright" wrote in message
news:[email protected]...
> I think I'm having a problem with a pointer but cannot put my finger on
> it. Can someone explain why this function would cause a fatal error?
>
> m_StartSectionVerts & m_EndSectionVerts are declared as a private member
> variables like this:
> AcGePoint3d *m_StartSectionVerts;
> AcGePoint3d *m_EndSectionVerts;
>
> here is the function:
> void MyObject::SetCrossSection(AcGePoint3d* verts, Adesk::UInt32
> numVerts)
> {
> m_numStartSectionVerts = numVerts;
>
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
> m_StartSectionVerts = verts;
>
> m_numEndSectionVerts = numVerts;
> m_EndSectionVerts = new AcGePoint3d[(m_numEndSectionVerts - 1)];
>
> for (int vertexNumber = 0; vertexNumber <= (m_numEndSectionVerts - 1);
> vertexNumber++)
> {
> m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> m_EndSectionVerts[vertexNumber] = 10.00;
>
> acutPrintf("\nStart = %.3lf,%.3lf,%.3lf",
> m_StartSectionVerts[vertexNumber],
> m_StartSectionVerts[vertexNumber],
> m_StartSectionVerts[vertexNumber] );
> acutPrintf("\nEnd = %.3lf,%.3lf,%.3lf",
> m_EndSectionVerts[vertexNumber], m_EndSectionVerts[vertexNumber],
> m_EndSectionVerts[vertexNumber] );
>
> }
> }
>
0 Likes
Message 5 of 5

Anonymous
Not applicable
The problem seems to occur when trying to set the m_EndSectionVerts x,y, and z
coordinates within the loop. I checked my array length to see if I was going
beyond but I am not. I tried commenting out the for loop and had no crash. When
the for loop is active, the program has random crashes

Paulo Aragao wrote:

> Hi
> You have to check these:
> 1-You allocate your member variable with new []. make sure if you delete
> with delete []. ex: delete [] m_StartSectionVerts;
> 2-On your constructor set your member variables pointes to NULL;
> 3-On your destructor use delete[] on your member variable pointes
> 3-On your SetCrossSection function don´t forget to, before everything,
> delete your member variable pointers
>
> PS:check your SetCrossSection function:
> m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
> m_StartSectionVerts = verts;
> This way, you have a lost pointer because you allocate your member variable
> with new and after you set
> this pointer to m_StartSectionVerts
>
> Aragao
>
> "Bill Wright" escreveu na mensagem
> news:[email protected]...
> > I think I'm having a problem with a pointer but cannot put my finger on
> > it. Can someone explain why this function would cause a fatal error?
> >
> > m_StartSectionVerts & m_EndSectionVerts are declared as a private member
> > variables like this:
> > AcGePoint3d *m_StartSectionVerts;
> > AcGePoint3d *m_EndSectionVerts;
> >
> > here is the function:
> > void MyObject::SetCrossSection(AcGePoint3d* verts, Adesk::UInt32
> > numVerts)
> > {
> > m_numStartSectionVerts = numVerts;
> >
> > m_StartSectionVerts = new AcGePoint3d[(m_numStartSectionVerts - 1)];
> > m_StartSectionVerts = verts;
> >
> > m_numEndSectionVerts = numVerts;
> > m_EndSectionVerts = new AcGePoint3d[(m_numEndSectionVerts - 1)];
> >
> > for (int vertexNumber = 0; vertexNumber <= (m_numEndSectionVerts - 1);
> > vertexNumber++)
> > {
> > m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> > m_EndSectionVerts[vertexNumber] = verts[vertexNumber];
> > m_EndSectionVerts[vertexNumber] = 10.00;
> >
> > acutPrintf("\nStart = %.3lf,%.3lf,%.3lf",
> > m_StartSectionVerts[vertexNumber],
> > m_StartSectionVerts[vertexNumber],
> > m_StartSectionVerts[vertexNumber] );
> > acutPrintf("\nEnd = %.3lf,%.3lf,%.3lf",
> > m_EndSectionVerts[vertexNumber], m_EndSectionVerts[vertexNumber],
> > m_EndSectionVerts[vertexNumber] );
> >
> > }
> > }
> >
0 Likes