September 5, 2018

structure packing size has changed

"Borland C++ Builder 6.0" "W8059" "structure packing size has changed" - if you are reading this, there is a chance that you are experiencing the same problem, as I did.
These weeks were really bad, I had to rewrite my program from scratch. I suspected bugs within gdiplus.dll, and rewrote my program under OpenGL. But random access violation errors has returned...
After days of frustration, I found my problem. I hope that the same solution will also help you.

Some clues are there:

"...The headers pshpackX.h have a critical bug in them...."
http://edn.embarcadero.com/article/29009
"...Unfortunately, the pshpack header files that ship with BCB6 contain a flaw...."
https://www.experts-exchange.com/questions/20873011/BCB6-Warnings-about-structure-size-changed.html
And there:
"...The best solution is to change the default data alignment for the project...."
http://www.decompile.com/cpp/faq/W8059_Structure_Packing_Size.htm

In my case the problem was winsock2.h. There is #include pshpack4.h in the beginning of file, and #include poppack.h in the end. It seems that within winsock2 packing size or (data alignment) is changing on the go, and then failed to be restored back.

I have the same class CServer referenced from two different .cpp files. If I call sizeof(CServer) from .cpp where winsock2 is included, I get 72 bytes, if winsock2 is not included, I get 80 bytes... Of course, there is a reason of all these access violations errors.

According to Embarcadero website, all family of pshpackX.h including pshpack1, pshpack2, pshpack4 and pshpack8 is corrupted. I played a bit, but did not find any good solution to fix this issue gracefully. Instead, I changed Data Alignment to Double Word in my project options, and now the problem is gone, both tests show 72 bytes.

I am using Borland C++ Builder 6.0 Update #4.

No comments: