Home > Mobile >  Ask questions about memory release in QT
Ask questions about memory release in QT

Time:09-17

I created by means of new BYTE [szSize] pointer to struct, and then add them QList, when the entire object release to traverse the QList pointer to delete them one by one, but every time when released into the second pointer object are submitted to the following error:
Exe HEAP [XXX] : Invalid address specified to RtlValidateHeap (000000 fd6a3e0000, 000000 fd7367c330)
The code is as follows:
 
//XObject. H
Typedef struct _MEM_INFO
{
DWORD Index;
UINT Type;
Char Info [INFO_SIZE + 1];
.
} MEM_INFO, * P_MEM_INFO;
Typeof struct _STRUCT_TEST
{
P_MEM_INFO pMemInfo;
.
} STRUCT_TEST;
.
Class XObject//XObject not inherit from QObject!
{
Private:
QList.
};
//XObject. CPP
Void XObject: : Create ()
{
for(int i=0; i<10; I++) {
P_MEM_INFO pInfo=(P_MEM_INFO) new BYTE [sizeOfMeminfo];
.
STRUCT_TEST test;
Test. PMemInfo=pInfo;
InfoList. Append (test);
}
.
}
//destructor
XObject: : ~ XObject ()
{
for(int i=0; iSTRUCT_TEST * pTest=& amp; (infoList [I]);
If (pTest - & gt; PMemInfo) {
The delete pTest - & gt; PMemInfo;//an error occurred when I==1: HEAP [XXX. Exe] : Invalid address specified to RtlValidateHeap (000000 fd6a3e0000, 000000 fd7367c330)
PTest - & gt; PMemInfo=NULL;
}
}
}

The above situation, please. What should I do? Thank you very much!

CodePudding user response:

Supplement: use the delete [] release space is also the same error,

CodePudding user response:

P_MEM_INFO pInfo=(P_MEM_INFO) new BYTE [sizeOfMeminfo];
No, look at the difference between the new and the malloc

CodePudding user response:

Don't use qbytearray use byte

CodePudding user response:

refer to the second floor Inhibitory response:
P_MEM_INFO pInfo=(P_MEM_INFO) new BYTE [sizeOfMeminfo];
No, look at the difference between the new and the malloc

Thank you for your advice!
I according to your tips for new/delete [], malloc/free, HeapAlloc/HeapFree tests were carried out three ways, including new/delete with malloc/free are I==1 times the same Heap error, HeapFree at I==1 times "Critical error detected c0000374" error,
Could you help analysis it again? Thank you very much!

CodePudding user response:

The
reference 3 floor hidxl response:
don't use a byte a qbytearray

QByteArray can be directly converted into Struct pointer excuse me? How to use?

CodePudding user response:

QDeleteAll (infoList);
InfoList. The clear ();

CodePudding user response:

I think is a problem of screw to delete, if you list only two size, then you delete a list [0], [1] the original list becomes a list [0], that this time you I=1, crossing the line

CodePudding user response:

Use new MEM_INFO to try

CodePudding user response:

refer to 6th floor wd350355942 response:
qDeleteAll (infoList);
InfoList. The clear ();

QDeleteAll tried not, infoList element is a Struct is not within the Object

CodePudding user response:

refer to 7th floor qq_36177886 response:
I think is a problem of screw to delete, if you list only two size, then you delete a list [0], [1] the original list becomes a list [0], that this time you I=1, crossed the

I did not remove elements, just empty the elements within the struct pointer, is not the same as what you say

CodePudding user response:

Supplement:
If the destructor when using free memory in reverse chronological order, the first (length () - 1) will be an error!
 
//destructor
XObject: : ~ XObject ()
{
For (int I=infoList. Length () - 1; i>=0; - I) {
STRUCT_TEST * pTest=& amp; (infoList [I]);
If (pTest - & gt; PMemInfo) {
The delete pTest - & gt; PMemInfo;
//I==(infoList length () - 1) error:
//HEAP [XXX. Exe] : Invalid address specified to RtlValidateHeap (01200000, 0 bc2fb18)
PTest - & gt; PMemInfo=nullptr;
}
}
}

In addition, found an error tracking memory address 0 bc2fb18 and pTest - & gt; PMemInfo actual address 0 xbc2fb38 , feel very strange! ??

CodePudding user response:

Pointer to the problem

CodePudding user response:

reference 5 floor masuz1 reply:
Quote: refer to the third floor hidxl response:

Don't use byte use qbytearray

QByteArray can be directly converted into Struct pointer excuse me? How to use?

QByteArray essence is a char * plus size values, you extracted char * pointer to give you his assignment of Struct

CodePudding user response:

Tease than, the size of the list is dynamic in deleted!
Into len=list. The length ();
for(int i=0; iDelete a pointer

CodePudding user response:

 
//XObject. H
Typedef struct _MEM_INFO
{
DWORD Index;
UINT Type;
Char Info [INFO_SIZE + 1];
.
} MEM_INFO, * P_MEM_INFO;
Typeof struct _STRUCT_TEST
{
P_MEM_INFO pMemInfo;
.
} STRUCT_TEST;
.
Class XObject//XObject not inherit from QObject!
{
Private:
QList.
};
//XObject. CPP
Void XObject: : Create ()
{
Unsigned sizeOfMeminfo=sizeof (MEM_INFO);
for(int i=0; i<10; I++) {
STRUCT_TEST test;
Test. PMemInfo=(P_MEM_INFO) new BYTE [sizeOfMeminfo];
TestList. Append (test);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • Qt
  • Related