AcdbWcs2Ucs overwrites memory

AcdbWcs2Ucs overwrites memory

Anonymous
Not applicable
661 Views
4 Replies
Message 1 of 5

AcdbWcs2Ucs overwrites memory

Anonymous
Not applicable

Hi!

 

I have an example where acdbWcs2Ucs seems to overwrite memory:

 

bool GetDwgViewExtents(AcGePoint2d &acadPtTL, AcGePoint2d &acadPtBR)

{

 // First retrieves the extents with GetWindowRect, ScreenToClient and acedCoordFromPixelToWorld into the following

acedDwgPoint dwgPtTL, dwgPtBR;

 

// I have tried using the acedDwgPoints directly, but the same happens, so this was an effort to try something else

AcGePoint2d acadPtTLorg, acadPtBRorg;
acadPtTLorg.set(dwgPtTL[0], dwgPtTL[1]);
acadPtBRorg.set(dwgPtBR[0], dwgPtBR[1]);
acdbWcs2Ucs(asDblArray(acadPtTLorg), asDblArray(acadPtTL), false); // acadPtTL correctly set to {x=-15058.342090026275 y=1177968.0167071891 }
acdbWcs2Ucs(asDblArray(acadPtBRorg), asDblArray(acadPtBR), false); // acadPtBR correctyl set to {x=-14905.579798326420 y=1177905.8325202183 }

 

// BUT: in the last call, acadPtTL[0] is unexpectedly changed to 0.0!

}

 

The really strange thing is, I added the following after this code, to compare in debug what happens:

 

ads_point tl, br;
tl[0] = dwgPtTL[0];
tl[1] = dwgPtTL[1];
br[0] = dwgPtBR[0];
br[1] = dwgPtBR[1];
acdbWcs2Ucs(tl, tl, false);
acdbWcs2Ucs(br, br, false);
acadPtTL.set(tl[0], tl[1]);
acadPtBR.set(br[0], br[1]);

 

this code works just fine, but when compiling with this code there, also the first version suddenly works fine!

 

Any tips on what is going on here are appreciated!

 

One concrete question is that the definition of asDblArray is:

 

inline double*
asDblArray(const AcGePoint2d& pnt)
{
return (double*)&pnt;
}

 

where as far as I can tell, AcGePoint2d has no explicit conversion to a double *. It has some functions and only the 2 member variables:

 

double x,y;

 

But is it really safe to assume that simply converting with a (double*) changes the pointer to an AcGePoint2d to a valid double[3] pointer?

 

Could this be the problem here, could it be a compiler error or am I missing something?

 

Best regards,

 

Knut

0 Likes
Accepted solutions (1)
662 Views
4 Replies
Replies (4)
Message 2 of 5

Alexander.Rivilis
Mentor
Mentor
Accepted solution

Please use AcGePoint3d instead of AcGePoint2d

acdbWcs2Ucs use double[3] and not double[2]. So it can overwrite you data.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 5

Alexander.Rivilis
Mentor
Mentor

@Anonymous wrote:
... But is it really safe to assume that simply converting with a (double*) changes the pointer to an AcGePoint2d to a valid double[3] pointer?...

It is not safe!!! 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 4 of 5

Anonymous
Not applicable

Thank you for a quick answer! I had just realized the exact same thing:

 

acdbWcs2Ucs expects double[3] pointers, while the (double*)AcGePoint2d obviously has only 2 doubles, so anything can happen when acdbWcs2Ucs writes the third double!

 

But as a prequel:

 

is the asDblArray safe if I _am_ using the result as a double[2]?

 

Best regards,

 

Knut

0 Likes
Message 5 of 5

Alexander.Rivilis
Mentor
Mentor

@Anonymous wrote:

 

But as a prequel:

 

is the asDblArray safe if I _am_ using the result as a double[2]?


Yes. In this situation asDblArray is safe.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes