RAD Studio
ContentsIndex
PreviousUpNext
Resource Errors

Resources are memory blocks (allocated with functions like malloc, GlobalAlloc) and object arrays, such as file handles, stream handles, modules, and items returned by new[]

The following runtime error examples illustrate how CodeGuard reports improper use of resources:

  • Bad parameter
  • Reference to freed resource
  • Resource type mismatch
  • Resource leaks
  • Resource from different RTL

When a resource is passed to a function, CodeGuard checks the runtime arguments. CodeGuard notifies you if it detects a bad parameter.

Error 00017. 0x310000 (Thread 0xFFF87283):
Bad parameter: A bad file handle (0xEA) has been passed to the function.
close(0xEA [234])
| lang.cpp line 170:
|    // using a bad handle //
|    //--------------------//
|>   close(234);
| 
|    //----------------------//
Call Tree:
   0x00401456(=LANG.EXE:0x01:000456) lang.cpp#170
   0x00407EE5(=LANG.EXE:0x01:006EE5)

In the following example, CodeGuard reports an attempt to read from a file that has already been closed. The CodeGuard log shows where the file was opened and subsequently closed.

Error 00020. 0x310030 (Thread 0xFFF840F1):
Reference to freed resource:
read(0x3 [3], 0x0072FCC4, 0x5 [5])
| lang.cpp line 177:
|    int i = open("lang.cpp", 0);
|    close(i);
|>   read (i, buffer, 5);
| 
|    //--------------//
Call Tree:
   0x00401487(=LANG.EXE:0x01:000487) lang.cpp#177
   0x00407EED(=LANG.EXE:0x01:006EED)

The file handle (0x00000003) [name: 'lang.cpp'] was opened with open
| lang.cpp line 175:
|    // using a freed handle //
|    //----------------------//
|>   int i = open("lang.cpp", 0);
|    close(i);
|    read (i, buffer, 5);
Call Tree:
   0x0040146C(=LANG.EXE:0x01:00046C) lang.cpp#175
   0x00407EED(=LANG.EXE:0x01:006EED)

The file handle (0x00000003) was closed with close
| lang.cpp line 176:
|    //----------------------//
|    int i = open("lang.cpp", 0);
|>   close(i);
|    read (i, buffer, 5);
| 
Call Tree:
   0x00401477(=LANG.EXE:0x01:000477) lang.cpp#176
   0x00407EED(=LANG.EXE:0x01:006EED)

In the following example, a memory block that was allocated with the new[] operator, and should therefore be released with the delete[] operator, is instead released with a call to the free function.

Error 00024. 0x350010 (Thread 0xFFF840F1):
Resource type mismatch: a(n) memory block was expected.
free(0x00B42464)
| lang.cpp line 188:
|    //---------------//
|    char * ss = new char[21];
|>   free(ss);
| 
| #ifdef    __WIN32__
Call Tree:
   0x0040149F(=LANG.EXE:0x01:00049F) lang.cpp#188
   0x00407EED(=LANG.EXE:0x01:006EED)

The object array (0x00B42464) [size: 21 bytes] was created with new[]
| lang.cpp line 187:
|    // type mismatch //
|    //---------------//
|>   char * ss = new char[21];
|    free(ss);
| 
Call Tree:
   0x00401498(=LANG.EXE:0x01:000498) lang.cpp#187
   0x00407EED(=LANG.EXE:0x01:006EED)

In the following example, memory has been allocated but is never freed.

The memory block (0x00B42310) [size: 200 bytes] was allocated with malloc
| lang.cpp line 78:
|    // An array on the stack.
|    char buf_s[21];
|>   char * pad = (char *) malloc(200);
|    // An array in the RTL heap.
|    char * buf_h = (char *) malloc(21);
Call Tree:
   0x00401199(=LANG.EXE:0x01:000199) lang.cpp#78
   0x00407EE5(=LANG.EXE:0x01:006EE5)

CodeGuard reports an error if your application allocates, uses, or releases resources in different versions of the runtime library. This can happen, as the following example illustrates, if you link with a static runtime library but call a DLL.

Note: CodeGuard detects resource type mismatches before it detects mixed versions of the RTL. When the two kinds of error are combined, CodeGuard will not report the mixed RTLs until you correct the resource type mismatch.

Error 00001. 0x340010 (Thread 0x0062):
Resource from different RTL:
close(0x3 [3])
| testdll.cpp line 23:
| {¬
|    MessageBox(NULL,"RTLMixHandle: DLL closing EXE handle", "TESTDLL.CPP", MB_OK );
|>   close( handle );
|    return 1;
| }
Call Tree:
   0x0032115A(=testdll.dll:0x01:00015A) testdll.cpp#23
   0x00401660(=WINAPI.EXE:0x01:000660) filescg.cpp#33
   0x00401271(=WINAPI.EXE:0x01:000271) winapi.cpp#122
   0x77EA15B3
   0x00408B9A(=WINAPI.EXE:0x01:007B9A)

The file handle (0x00000003) [name: 'test2.dat'] was opened with open
| filescg.cpp line 32:
| 
|  MessageBox(NULL,"FilesMixCG: Mixing RTL file handles", "FILESCG.CPP", MB_OK );
|> i = open("test2.dat", O_CREAT, S_IREAD | S_IWRITE );
|  RTLMixHandle( i );
| }
Call Tree:
   0x00401657(=WINAPI.EXE:0x01:000657) filescg.cpp#32
   0x00401271(=WINAPI.EXE:0x01:000271) winapi.cpp#122
   0x77EA15B3
   0x00408B9A(=WINAPI.EXE:0x01:007B9A)
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!