Changeset 52 in pacpussensors for trunk/Sick


Ignore:
Timestamp:
06/16/14 16:04:34 (10 years ago)
Author:
cfougera
Message:

Memory leak bug resolved (dynamic memory allocation is no longer used).

Location:
trunk/Sick
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Sick/SickComponent.h

    r42 r52  
    4949 */
    5050class SICK_API SickComponent
    51         : public QThread
    52         , public ComponentBase
     51        : /*public QThread
     52        , */
     53        public QObject, public ComponentBase
    5354{
    5455    Q_OBJECT
     
    6162    ~SickComponent();
    6263
    63     void run() {}
     64   // void run() {}
    6465
    6566    virtual void stopActivity(); /*!< To stop the processing thread */
  • trunk/Sick/SickLDMRSData.h

    r43 r52  
    5454    u_int64_t ntpTime;              //!< Time of the sensor when the message is created
    5555};
    56    
     56
     57
    5758/*!
    5859 * \brief The ScanHeader struct
  • trunk/Sick/SickLDMRSSensor.cpp

    r42 r52  
    320320        }
    321321
     322        LOG_TRACE("(Packet reconstitution) Message complete ! ");
    322323        // we have a complete message available that we can add to the list
    323          MessageLDMRS msg;
     324        MessageLDMRS msg;
    324325
    325326        // we copy the bytes in the body message
    326         char* messageData = (char*)malloc(msgSize);
    327         memcpy(messageData, pendingBytes.data.c_str() + index, msgSize);
    328 
    329         msg.body = messageData;
     327//        char* messageData = (char*)malloc(msgSize);
     328//        if(messageData == NULL){
     329//            LOG_FATAL("(Packet reconstitution) Malloc FAILED. Packet lost.");
     330//            return;
     331//        }
     332        memcpy(msg.body, pendingBytes.data.c_str() + index, msgSize);
     333
     334//        msg->body = messageData;
    330335
    331336        // we set the timestamp of the message
     
    358363
    359364        int index = 24 + 44; // data header + scan header
    360         ScanPoint* scanPoints = (ScanPoint*) malloc(sizeof(ScanPoint) * msg.hScan.numPoints);
    361         int position = 0;
    362 
     365
     366        if(sizeof(ScanPoint) * msg.hScan.numPoints > BODY_MAX_SIZE){
     367            LOG_FATAL("Size of the message is too long !");
     368            return 0;
     369        }
     370
     371        ScanPoint scanPoints[msg.hScan.numPoints];
     372
     373        // replace memory with structured data
    363374        for (int i = 0; i < msg.hScan.numPoints; ++i) {
    364             ((ScanPoint*)(scanPoints + position))->layerEcho = *((uchar*)(msg.body + index));
    365             ((ScanPoint*)(scanPoints + position))->flags = *((uchar*)(msg.body + index + 1));
    366             ((ScanPoint*)(scanPoints + position))->angle = *((u_int16_t*)(msg.body + index + 2));
    367             ((ScanPoint*)(scanPoints + position))->distance = *((u_int16_t*)(msg.body + index + 4));
    368             ((ScanPoint*)(scanPoints + position))->echoPulseWidth = *((u_int16_t*)(msg.body + index + 6));
    369             index += 10;
    370             position += sizeof(ScanPoint);
    371         }
    372 
    373         // raw data (message) no longer needed, free memory and replace with structured data
    374         free(msg.body);
    375         msg.body = (char*) scanPoints;
     375            scanPoints[i].layerEcho = *((uchar*)(msg.body + index));
     376            scanPoints[i].flags = *((uchar*)(msg.body + index + 1));
     377            scanPoints[i].angle = *((u_int16_t*)(msg.body + index + 2));
     378            scanPoints[i].distance = *((u_int16_t*)(msg.body + index + 4));
     379            scanPoints[i].echoPulseWidth = *((u_int16_t*)(msg.body + index + 6));
     380        }
     381
     382        memcpy(msg.body, scanPoints, sizeof(ScanPoint) * msg.hScan.numPoints);
    376383    }
    377384    else if (msg.hData.dataType == SICKLDMRS_OBJECTDATA_TYPE){
    378385        LOG_TRACE("(Process Message) Object Data Type!");
    379386
    380 
    381 
    382387        // TODO
    383 
    384         // raw message no longer needed, free memory
    385         free(msg.body);
    386         // msg.body = (char*) scanObjects;
    387     }
    388     else // irrelevant data type
    389         free(msg.body); // free raw data
     388    }
     389    else {// irrelevant data type
     390        // TODO
     391    }
     392
    390393
    391394    return msg.hData.dataType;
     
    405408//    entry.timerange = msg.timerange;
    406409
     410    LOG_TRACE("Writing into DBT + UTC files..");
     411
    407412    // write DBT
    408413    try {
     
    421426    dataFile_.write(reinterpret_cast<char*>(&(utcMagicWord)), sizeof(int32_t));
    422427
     428    LOG_TRACE("Writing done !");
     429
    423430}
    424431
     
    446453    while ( !msgList.empty() )
    447454    {
     455        LOG_TRACE("Message waiting");
    448456        // get the first (the eldest) message and process it
    449457        MessageLDMRS msgToProcess = msgList.front();
    450458        unsigned long type = processMessage(msgToProcess);
     459        LOG_TRACE("Message processed !");
    451460
    452461        if (type == SICKLDMRS_SCANDATA_TYPE)
     
    480489
    481490        // removes the processed item of the list
     491        // free(msgList.front().body);
    482492        msgList.pop_front();
    483493    }
  • trunk/Sick/SickLDMRSSensor.h

    r42 r52  
    2525#include <fstream>
    2626#include <string>
     27
     28#define BODY_MAX_SIZE   10000
    2729
    2830// Export macro for SickLDMRS DLL for Windows only
     
    7981    //! An array of characters : raw data then array of points or objects, depending on data type.
    8082    /** This array pointer points to allocated in memory (basically, in heap (malloc)) and then must be freed (free) when the whole message is decoded and stored. */
    81     char* body;
     83    char body[BODY_MAX_SIZE];
    8284
    8385    //! Time when the message is received.
Note: See TracChangeset for help on using the changeset viewer.