0

I have a program that is using HeapAlloc() to allocate a struct that contains a QString and a few QByteArray structs.

Struct code:

struct Environment {
    QString internalLinksDomain;
    QByteArray aboutTelegram;
    QByteArray aboutContacts;
    QByteArray aboutFrequent;
    QByteArray aboutSessions;
    QByteArray aboutWebSessions;
    QByteArray aboutChats;
    QByteArray aboutLeftChats;
};

My code:

Environment* e = (Environment*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Environment));

e->internalLinksDomain = "Test data";

I have also tried..

e->internalLinksDomain.append("Test data");

Both send me to the atomic file in Visual Studio. I have attached a screen shot of the error and its position in the function causing the error.

VS Error

xf9000
  • 11
  • 2
  • `Environment* e = (Environment*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Environment));` is not going to call the constructor on any of the `QString` or `QByteArray` members. – drescherjm Aug 19 '22 at 22:14
  • Related: [https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new](https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new) – drescherjm Aug 19 '22 at 22:15
  • If I created the struct normally like ```Environment e``` and then did ```e.internalLinksDomain = "test";``` would that call the constructor? – xf9000 Aug 19 '22 at 22:25
  • Yes without HeapAlloc() the constructor would be called correctly. With my second comment you can fix it to work with HeapAlloc() but I question the why you want to use this over using new. – drescherjm Aug 19 '22 at 22:35
  • I am not sure how to do it using new. I am learning C++ and the link mentioned above is beyond my current knowledge. – xf9000 Aug 19 '22 at 22:43
  • 2
    Only fix for that is to go back a to earlier material until you understand what's going on and work your way forward again. C++ is not a good language to jump around in while learning. Boobytraps galore. – user4581301 Aug 19 '22 at 22:57
  • Actually I think I figured it out. I just need to create a constructor in the struct that accepts arguments that I set to the struct members. – xf9000 Aug 19 '22 at 23:21

0 Answers0