-2

i am the "proud" heir of an old VC++ DLL and try to understand a problem with it. My predecessor used some union/struct constructions for dataprocessing. Now i debugged it to a point he put the data in the struct, but the whole app crashes, printing a memory dump and no try-catch works.

A small CodeExample.

MyCode.h:

union
    {
        struct                                  
        {   double _dm;
        };
        struct                                  
        {   double _dm;
            bool _links;
        };
        struct  
        {   double _dm;
            double _dummy;
            double _angle;
        };
        struct                                  
        {   double _dm;
            double _angle;
            double _dummy1;
            string _name;
            string _descr;
            double _param_d1, _param_d2, _param_d5;
            double _dummy2;
            string _dummy3;
            int _epuzae;
            int _param_i2;
            string _sob, _snr2, _param_s3, _param_s4;
            void *_data;
        };
        struct
        {   void *_data;
        };
    }

MyCode.cpp

... Rest of the method...
_dm = 100; // Will be set
_angle = 0; // Will be set
_dummy1 = 0; // Will be set
_name = "Unittest"; // Here it crashes the whole app
_descr = "This is a test";
_param_d1 = 1;
_param_d2 = 2;
_param_d5 = 5;
_dummy2 = 0;
_dummy3 = "";
_epuzae = 99;
_param_i2 = 101;
...

Is there a method to test what struct constructor is called or what i am doing wrong here?

Best regards

Lord_Pinhead

Lord_Pinhead
  • 127
  • 1
  • 10
  • 1
    Please add a [mcve]. A union with a non trivial type in it (std::string in this case) is not trivial and requires a bunch of extra work on your part. You basically need to implement a tagged union. See: https://stackoverflow.com/questions/40106941/is-a-union-members-destructor-called/40107282#40107282 – NathanOliver Mar 18 '19 at 15:09
  • what is string is it std::string? – agent_bean Mar 18 '19 at 15:14
  • How is this union initialized? – Michael Kenzel Mar 18 '19 at 15:16
  • There are a huge number of ways a person can go horribly wrong with a `union`. We need to see how the `union` is being used to see how (or if) it is being abused. – user4581301 Mar 18 '19 at 15:17
  • Rafael, yes it is a std::string Nathan I will try a tagged version thanks. I never used structs in c++, I have classes for that. Michael I kid you not, the second part is everything, no initialization. In Prod it somehow works. The guy was no programmer, he learned it with this app, so that is all I have for now. I make a unittest tomorrow and post a status. – Lord_Pinhead Mar 18 '19 at 15:31
  • A `std::string` is a complex object. Using one in a `union` is very tricky. They used to be outright banned, but that ban was lifted in C++11. [See this question, particularly answer 2, for details.](https://stackoverflow.com/questions/3521914/why-compiler-doesnt-allow-stdstring-inside-union). If you are using this `union` primarily for [type punning](https://en.wikipedia.org/wiki/Type_punning), you definitely do not want to use a `std::string`. It doesn't pun.. – user4581301 Mar 18 '19 at 20:42
  • After so many problems, and follow up errors, i am refactoring the whole structure to classes. Your answers shows me, that it is a bad design to use a struct with std::string, so i stop bothering with it. I put an answer for someone else in here tomorrow. Thanks for the answers and links. – Lord_Pinhead Mar 19 '19 at 15:02

1 Answers1

0

After to much trouble and helpfull links why not use struct with strings, i refactored it yesterday to a class, what runs like a charme:

 class DPic {
public:
    DPic();
    ~DPic();

    double DM;
    double Angle;
    std::string Name;
    std::string Descr;
    double Param_d1, Param_d2, Param_d5;
    int Epuzae;
    int Param_i2;
    std::string Sob, Snr2, Param_s3, Param_s4;
    void *_data;
};

So if anybody ever find this in his code, just throw it out and use proper classes or membervariables.

Lord_Pinhead
  • 127
  • 1
  • 10