Pointer problem - tildelte strenge skifter placering
Hejsa..Jeg er støt ind i problem, hvor jeg under debuging kan se at tildelte strenge fint virker i funktionen hvor de
sættes, men når funktionen slutter og strukturen returneres så byttes der godt og grundigt rundt på pointerne til
strengene.
Min struktur har følgnede form:
struct WPGrabUrlResourceStruct
{
int errCode;
char* errMsg;
char* localFileName;
char* url;
WPGRAB_STATUS_ENUM status;
};
struct ResourcesFoundStruct
{
vector<UrlResourceStruct> webpages;
vector<UrlResourceStruct> others;
};
struct WPGrabResourceFoundStruct
{
int webpageCount;
int imagesCount;
int otherCount;
WPGrabUrlResourceStruct* webpagesList;
WPGrabUrlResourceStruct* imagesList;
WPGrabUrlResourceStruct* otherList;
};
og funktionen der initialiserer denne er:
WPGRABDLL_API WPGrabResourceFoundStruct* WPGrabGetFoundResources(int index)
{
...
ResourcesFoundStruct resources;
// Fetch found resources...
if(grabDB->getFoundResourceByIndex(resources, index))
{
WPGrabResourceFoundStruct* wpResources = new WPGrabResourceFoundStruct;
// Copy webpages
wpResources->webpageCount = (int) resources.webpages.size();
...
if(wpResources->webpageCount > 0)
{
// allocate room for webpages...
wpResources->webpagesList = new
WPGrabUrlResourceStruct[wpResources->webpageCount*sizeof(WPGrabUrlResourceStruct)];
if(wpResources->webpagesList == NULL)
return NULL; // cannot allocate memory...
for(int i=0;i<wpResources->webpageCount;i++)
{
wpResources->webpagesList[i].errMsg = "fisk";
wpResources->webpagesList[i].errCode = 0; // no one exists for the moment...
wpResources->webpagesList[i].localFileName = "hund";
wpResources->webpagesList[i].url = "hval";
wpResources->webpagesList[i].status = (WPGRAB_STATUS_ENUM) resources.webpages.at(i).pType;
}
}
else
...
return wpResources;
}
else
{
return NULL;
}
}
og udskrivningen:
WPGrabResourceFoundStruct* data = WPGrabGetFoundResources(0);
if(data != NULL)
{
for(int i=0;i<data->otherCount;i++)
{
cout << "Item: " << data->otherList[i].url << endl;
cout << " file: " << data->otherList[i].localFileName << endl;
cout << " errmsg: " << data->otherList[i].errMsg << endl;
cout << " errcode: " << data->otherList[i].errCode << endl;
}
Når jeg kører programmet får jeg følgende output nåt WPGrabGetFoundResources er færdig (inden den returnerer):
- wpResources 0x003ba240 {webpageCount=1 imagesCount=0 otherCount=0 ...} WPGrabResourceFoundStruct *
- webpagesList 0x003bafe8 {errCode=0 errMsg=0x10063914 "fisk" localFileName=0x1006390c "hund" ...}
WPGrabUrlResourceStruct *
- errCode 0 int
- errMsg 0x10063914 "fisk" char *
- localFileName 0x1006390c "hund" char *
- url 0x10063904 "hval" char *
- status WPGRAB_PROC_ERROR WPGRAB_STATUS_ENUM
Dvs. alt er som det skal være... Men hvis vi så ser på outputtet fra data variablen:
- data 0x003ba240 {webpageCount=1 imagesCount=0 otherCount=0 ...} WPGrabResourceFoundStruct *
- webpagesList 0x003bafe8 {localFileName=0x00000000 <Bad Ptr> url=0x10063914 "fisk" errCode=268843276 ...}
WPGrabUrlResourceStruct *
- localFileName 0x00000000 <Bad Ptr> char *
- url 0x10063914 "fisk" char *
- errCode 268843276 int
- errMsg 0x10063904 "hval" char *
- status WPGRAB_PROC_ERROR WPGRAB_STATUS_ENUM
Sa er indholdet af url og errmsg byttet rundt og localfilename er nu en bad pointer???
Nogen der kan gennemskue hvorfor det sker??? Hvis jeg rykker rundt i erklæringen WPGrabUrlResourceStruct til
struct WPGrabUrlResourceStruct
{
char* localFileName;
char* url;
int errCode;
char* errMsg;
WPGRAB_STATUS_ENUM status;
};
Som er outputtet som det skal??? Jeg bruger Visual Studio 2005 SP1