Uploaded image for project: 'Axis-C++'
  1. Axis-C++
  2. AXISCPP-318

Checking that pointers are valid before using

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.5 Alpha
    • 1.6 Alpha
    • Basic Architecture
    • None
    • n/a

    Description

      Pointer Improvements
      --------------------
      I would like the following pointer improvements. This is mainly to do with ensuring that a pointer will always be valid if it is not NULL.

      Rule1
      =====
      On creation, unless a pointer is assigned a value it must be set to NULL.

      example:
      Test * pTest = NULL;
      or
      Test * pTest = new Test();

      Rule2
      =====
      Before using a pointer, it is tested to check that it is not NULL.

      example:
      if( pTest != NULL)
      {
      bool bResult = pTest->method1( iValue);
      }
      or
      if( pTest != NULL)
      {
      bool bResult = pTest->method1( iValue);

      if( bResult == true)

      { pTest->method2( sValue); }

      }

      Rule3
      =====
      On deletion, the pointer is set to NULL.

      example:
      if( pTest != NULL)
      {
      delete pTest;

      pTest = NULL;
      }

      Attachments

        Activity

          Can we use a set of globle macros for this?

          samisa Don Samisa Abeysinghe added a comment - Can we use a set of globle macros for this?
          amra Nadir Amra added a comment -

          Just to make sure I understand, you are saying that:

          When code dynamically instantiates an object via new or mallocs storage via malloc, it needs to ensure that it was allocated and return some sort of error if it has not.

          And when dynamic storage is reclaimed via delete or free, it needs to set the pointer to NULL.

          And I agree that this can be done via macros, and maybe in the future calls a AXIS C++ memory manager?

          amra Nadir Amra added a comment - Just to make sure I understand, you are saying that: When code dynamically instantiates an object via new or mallocs storage via malloc, it needs to ensure that it was allocated and return some sort of error if it has not. And when dynamic storage is reclaimed via delete or free, it needs to set the pointer to NULL. And I agree that this can be done via macros, and maybe in the future calls a AXIS C++ memory manager?

          If we are to use macros to help with this task, I propose the use of following:

          //for delete
          #define DELETE(X) if (X)

          {delete X; X = NULL;}

          //for delete[]
          #define DELETEA(X) if (X)

          {delete [] X; X = NULL;}

          //for new
          #define NEW(X, TYPE) if (X)

          {delete X; X = new TYPE;}

          //for new[]
          #define NEWA(X, TYPE, SIZE) if (X)

          {delete [] X; X = new TYPE[SIZE];}

          However, I am not a big fan of macros - they do reduce code redability.
          So, shall we say we should write the proper code rather than use macros?

          samisa Don Samisa Abeysinghe added a comment - If we are to use macros to help with this task, I propose the use of following: //for delete #define DELETE(X) if (X) {delete X; X = NULL;} //for delete[] #define DELETEA(X) if (X) {delete [] X; X = NULL;} //for new #define NEW(X, TYPE) if (X) {delete X; X = new TYPE;} //for new[] #define NEWA(X, TYPE, SIZE) if (X) {delete [] X; X = new TYPE[SIZE];} However, I am not a big fan of macros - they do reduce code redability. So, shall we say we should write the proper code rather than use macros?

          Are these rules suggestions for future code, or are we going to fix the existing code as well?
          I propose fix the existing code as much as possible.

          I could help by fixing some sections.

          samisa Don Samisa Abeysinghe added a comment - Are these rules suggestions for future code, or are we going to fix the existing code as well? I propose fix the existing code as much as possible. I could help by fixing some sections.

          Me and Dushshantha has gone through the full code base some time before and found some little problems. And we able fixed some as well.

          cdinapala Chinthana Dinapala added a comment - Me and Dushshantha has gone through the full code base some time before and found some little problems. And we able fixed some as well.
          hawkeye John Hawkins added a comment -

          Is this still an issue - looks like we've done a code-trawl?

          hawkeye John Hawkins added a comment - Is this still an issue - looks like we've done a code-trawl?

          This does not seem to be an issue any more. However these rules must be kept in mind when doing future development .

          samisa Don Samisa Abeysinghe added a comment - This does not seem to be an issue any more. However these rules must be kept in mind when doing future development .

          People

            Unassigned Unassigned
            prestonf Fred Preston
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: