1

Hello I am having an issue where I seem to be leaking memory when I am handling my unique_ptr array variables. Now I have isolated where the issue is occurring, but I don't understand why the leak is occurring. I suspect I may be misusing the unique_ptr but researching the internet and this site I have not found what I am doing wrong. The problem occurs among two functions when they pass the unique_ptr between each other. I have pointed out the lines of code that are of interest/importance and omitted irrelevant code.

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

So am I misusing the unique_ptr type? or am I forgetting something crucial in its usage. I tried recreating the problem in a simpler program but it ran with no leaks. I have been checking for leaks using VLD and Microsoft's leak detector code.

Matthew
  • 9,851
  • 4
  • 46
  • 77

0 Answers0