DBFReadStringAttribute broken?

View: New views
3 Messages — Rating Filter:   Alert me  

DBFReadStringAttribute broken?

by Ian Wright :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@... /
_______________________________________________
Shapelib mailing list
Shapelib@...
http://lists.maptools.org/mailman/listinfo/shapelib

Re: DBFReadStringAttribute broken?

by Frank Warmerdam-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ian Wright wrote:

> 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.

Ian,

I don't see why this is happening.  I would however suggest you update to the
current shapelib code.  There is code that is likely more recent in:

   http://download.osgeo.org/shapelib/shapelib_1_2_10.zip

However, I have also taken the liberty of packaging the latest development
code and posting it as:

   http://download.osgeo.org/shapelib/shapelib-1.3.0a1.zip

Try with updated code and see if the problem persists.   If it does, then you
should learn to use your debugger, build the dbfopen.c code with debug, and
step through the time "i" becomes corrupted to see what is going on.

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam@...
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org

_______________________________________________
Shapelib mailing list
Shapelib@...
http://lists.maptools.org/mailman/listinfo/shapelib

Re: DBFReadStringAttribute broken?

by Ian Wright :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Frank,

Thanks for the reply. I have actually downloaded and tested that version
now and the same problem
arises when using the lib/dll. I used the makefile to generate these and
then tried replacing the ones that
I was currently using, and it has exactly the same problem.

So I tried including just the shapefil.h and dbfopen.c files as they
were the only functions that I was
interested in within the project and thought stepping into these
functions would help identify the
problem, however it ran fine right to the end. I've tried it a couple of
times and as yet haven't had
the same problem or any corruption of variables whatsoever when running
it just from those source
files.

I'm not sure exactly what that suggests. Now that I've got it working in
windows also I'm planning
not to use the DLL etc. and stick with what works for now, so I'm
guessing if there haven't been
any other reported problems then it may not be worth worrying about?

If you do want to investigate further and theres anything I can be
assistance of then let me know.

Ian

> Ian,
>
> I don't see why this is happening.  I would however suggest you update
> to the
> current shapelib code.  There is code that is likely more recent in:
>
>   http://download.osgeo.org/shapelib/shapelib_1_2_10.zip
>
> However, I have also taken the liberty of packaging the latest
> development
> code and posting it as:
>
>   http://download.osgeo.org/shapelib/shapelib-1.3.0a1.zip
>
> Try with updated code and see if the problem persists.   If it does,
> then you
> should learn to use your debugger, build the dbfopen.c code with
> debug, and
> step through the time "i" becomes corrupted to see what is going on.
>
> Best regards,

--
*Ian Wright*
/Software Developer
ianw@... /

* Graticule <http://www.graticule.com>
* 01132 344000

_______________________________________________
Shapelib mailing list
Shapelib@...
http://lists.maptools.org/mailman/listinfo/shapelib