[Shapelib] DBFReadStringAttribute broken?

Ian Wright ianw at graticule.com
Thu Mar 20 07:55:08 EDT 2008


I'm wondering if anyone can provide me with some assistance? I'm trying 
to use the DBFReadStringAttribute method to
read from my DBF fill but am getting some unusal problems. I'm fairly 
new to C/C++ development and am not sure if this
suggests a memory leak or not, and was wondering if anyone could just 
point me in the right direction.

Basicaly my variables in my program are becoming corrupt and containing 
garbage instead of what they should contain
at random points within my program. I've stripped it right down and 
believe that it is the DBFReadStringAttribute thats
causing the problem.

My code is as follows:

#ifdef WIN32
#include <windows.h>
#endif

#include <iostream>
#include "shapefil.h"
#include <string>

using namespace std ;

int main (int argc, char **argv)
{
    DBFHandle hDBF = DBFOpen( "c:/not_backed_up/RouteFinda/Unclassified 
Roads_polyline.dbf", "rb" );
    if( hDBF != NULL )
    {
        char* readName;
 
        for (int i = 0; i < 684; i++)
        {
            readName = (char*) DBFReadStringAttribute(hDBF, 0, 3) ;
            cout << i << ":" << readName << endl;
        }
    }
   
    return 0 ;
}

Obviously this should read the same record over and over, 684 times. The 
actual output I get however is:

0:ENG
1:ENG
2:ENG
3:ENG
4:ENG
5:ENG
6:ENG
7:ENG
8:ENG
9:ENG
10:ENG
11:ENG
12:ENG
13:ENG
14:ENG
15:ENG
273185792:ENG

If I remove the DBFReadStringAttribute its fine and will run right 
through to the end. In my previous program my char*s and arrays
which has used strcpy to keep the values were becoming corrupt at random 
points like calling atan2(0,0) for example. This problem
is only apparent on my windows machine, and seems to run fine in Unix. 
As a note I have tried changing readName to both a const char *
and a string, while removing the cast of the DBFReadStringAttribute 
result but this hasn't made any difference.

Version of shapefil.h : v 1.20 2001/07/20 13:06:02 warmerda Exp

Any help would be greatly appreciated.

Ian Wright

-- 
*Ian Wright*
/Software Developer
ianw at graticule.com /


More information about the Shapelib mailing list