1

您好,我遇到了一个问题,我在处理unique_ptr数组变量时似乎正在泄漏内存。现在我已经隔离了问题发生的位置,但我不明白为什么会发生泄漏。我怀疑我可能在滥用,unique_ptr但在研究互联网和这个网站时,我没有发现我做错了什么。当它们unique_ptr在彼此之间传递时,两个函数之间会出现问题。我已经指出了感兴趣/重要的代码行,并省略了不相关的代码。

int doInstruction(SerialPort^ sPort, unsigned char cmdCode, 
   unique_ptr<unsigned char[]> &param, int sizeOfParam, 
   unique_ptr<char[]> &strMsg,int sizeOfMsg, 
   unique_ptr<unsigned char[]> &outputData){<<-- I pass in a unique_ptr 
                                                 by reference in order
                                                 to return data

    unique_ptr<unsigned char[]> tempOutput; <<--This is where the 
                                                unique_ptr starts
    unsigned char ack[] = ACK;
    unsigned char nack[] = NACK;
    unsigned char appErr = ERROR_BYTE;
    int a = 0;
    int dataPacketLength = 7 + sizeOfParam + sizeOfMsg;
    int sizeOfReply, errMsg;
    int errTrckr = 0;
    array<Byte>^ instruction = generateIns(cmdCode,param,sizeOfParam,
                                                        strMsg,sizeOfMsg);
    bool ackCheckStage = true;
    bool hasResponse = false;
    bool ackCheck = true;
    bool nackRepeat = false;

    switch(cmdCode){
        case INS_READCARD:
            hasResponse = true;
        break;
        case INS_RETRANSMIT:
            hasResponse = false;
            ackCheckStage = false;
        break;   
    }//end switch

    tempOutput = move(outputData);

    sendInstruction(instruction,dataPacketLength,sPort);

    while(a < 1){
        a++;
        errMsg = 0;
        errMsg = receiveMessage(sPort,tempOutput); <<--passes the unique_ptr
                                   to the receiveMessage function, this line
                                   is called twice, the first time it runs 
                                   fine but on the second run memory leaks

        //extract length of reply..only sent if message received successfully
        if(errMsg > 9){ 
            sizeOfReply = errMsg/10;
            errMsg = 0;
        }//end if
        if(errMsg == 0){ //success
            if(ackCheckStage){ //looking for acknowledgment
                if(sizeOfReply == 2 && tempOutput[0] == ack[0] 
                                  && tempOutput[1] == ack[1]){//acknowledged

                    if(hasResponse){ <<--This section decides if the while loop
                                            repeats or not, the issues occurs
                                            when the loop repeats the 2nd time
                        a--;
                        ackCheckStage = false;
                    }else{
                        outputData = move(tempOutput);
                        return 0;
                    }//end if
                }else if(){
                    //irrellevant code
                }//end if
            }else{ //looking at a message
                outputData = move(tempOutput); <<--after all is said and done
                                                   I return the unique_ptr
                return 0;
            }//end if
        }else{
            //irrellevant code
        }//end else if
    }//end while
    return -1;
}//end doInstruction

////////////The second function//////////////////////////////
int receiveMessage(SerialPort^ sPort, unique_ptr<unsigned char[]> &outData){<<--the
                                                         unique_ptr being passed by
                                                         reference so that data may
                                                         returned
    int length;
    int stopChecking = 0;
    unique_ptr<unsigned char[]> mData; <<--I create a temporary unique_ptr
                                           that I will work with within this
                                           function
    String^ output = "";
    int insReceived = 0; //starts at 0 so if everything is working 
                           it will not change into a 1
    mData = unique_ptr<unsigned char[]>(new unsigned char[2]);
    ^^--initialize the unique_ptr to store 2 bytes

    while(stopChecking == 0){
        stopChecking = findStartChar(mData,sPort);
    }//end while

    if(stopChecking == 1){
        output = gatherOutput(mData,2);//generating console output
        mData = unique_ptr<unsigned char[]>(new unsigned char[3]);
        ^^--discard old data and resize to store 3 bytes of data

        length = readLength(mData,sPort);<<--this seems to be working fine
        if(length > 0){
            if(!getSum(mData,sPort,3)){
                output = output + "," + gatherOutput(mData,3);//console output
                mData = unique_ptr<unsigned char[]>(new unsigned char[length+1]);
                ^^--discard old data and resize to store a generated
                    number of bytes of data

                    if(readData(mData,sPort,length+1) == 1){
                        if(!getSum(mData,sPort,length+1)){
                            output = output+","+gatherOutput(mData,length+1);
                            Console::WriteLine(output);
                            mData[length+1] = length;
                            outData = move(mData);<<--memory leak occurs here
                                                      first when this function
                                                      is called a second time
                            insReceived = length*10;
                        }else{
                            cout << "parity error\n";
                            insReceived = 3;
                        }//end if
                    }else{
                    }//end if
            }else{
            }//end if
        }else{
        }//end if
    }else{
    }//end if
    return insReceived;
}//end receiveMessage

那么我是否在滥用unique_ptr类型?还是我忘记了它使用中的重要内容。我尝试在一个更简单的程序中重新创建问题,但它运行时没有泄漏。我一直在使用 VLD 和 Microsoft 的泄漏检测器代码检查泄漏。

4

0 回答 0