Overview
Features
Download
Documentation
Community
Add-Ons & Services

_CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Please post support and help requests here.

_CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby tnarol » 11 Jun 2007, 21:51

Hi,

I'm linking against PocoUtil.dll to use the windows registry access methods. When returning from WinRegistryKey::getString I get the _CrtIsValidPointer assertion. I traced the execution in debug mode, everything seems to be fine when executing the last lines of the method :

if (size > 0)

{

char* buffer = new char[size + 1];

RegQueryValueEx(_hKey, name.c_str(), NULL, NULL, (BYTE*) buffer, &size);

buffer[size] = 0;

std::string result(buffer);

delete [] buffer;

return result;

}


"result" contains the expected string but the assertion is detected when its destructor is called after the value is copied to the destination variable.

It seems this is somehow related to a problem to deal with pointers coming from a DLL as explained on this page :

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1691706&SiteID=1

Do you know how to avoid this problem ?
Thanks
tnarol
 
Posts: 22
Joined: 21 Mar 2007, 18:56
Location: France

Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby alex » 11 Jun 2007, 22:20

> Do you know how to avoid this problem ?

AFAIK, the problem with pointers across DLL boundaries occurs only if you use different runtime libraries as described here:

http://msdn2.microsoft.com/en-us/library/ms235460(VS.80).aspx

Make sure that your executable and DLL are using the same runtime library.

Alex
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby tnarol » 12 Jun 2007, 09:41

> Make sure that your executable and DLL are using the same runtime library.

Thanks. Now I understand why it can happen. But I still don't understand why it should happen to me because I'm using the same version of Visual Studio to compile the Poco DLL and my project linking with the DLL. So it should be the same version of the CRT library that is used on both sides.
tnarol
 
Posts: 22
Joined: 21 Mar 2007, 18:56
Location: France

Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby alex » 12 Jun 2007, 13:15

> Do you know how to avoid this problem ?

It's hard to tell from the information you have supplied. What is the return type of your function? Are you returning a reference to stack-allocated storage?

Alex
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby tnarol » 12 Jun 2007, 13:44

> > Do you know how to avoid this problem ?
>
> It's hard to tell from the information you have supplied. What is the return type of your function? Are you returning a reference to stack-allocated storage?

I'm simply performation the affectation of the std::string result of the function call into another std::string locally declared variable.
tnarol
 
Posts: 22
Joined: 21 Mar 2007, 18:56
Location: France

Re: Re: Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby alex » 12 Jun 2007, 14:47

> I'm simply performation the affectation of the std:string result of the function call into another std::string locally declared variable.

I'm not sure how to interpret 'performation the affectation'. Can you say that in C++? I.e. paste a broader chunk of your code showing the whole function definition and how you call it.

Alex
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: Re: Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby tnarol » 14 Jun 2007, 14:18

> I'm not sure how to interpret 'performation the affectation'. Can you say that in C++? I.e. paste a broader chunk of your code showing the whole function definition and how you call

I meant that I'm performing the copy of the std:string result of the function call into another std::string locally declared variable.

In other words something like :

std::string directory;
WinRegistryKey myFileLocationKey("HKEY_LOCAL_MACHINE\SOFTWARE\MyProgram\FileLocation");
bool result = myFileLocationKey.exists();
if (result)
{result= myFileLocationKey.exists("Directory");
if (result) directory = myFileLocationKey.getString("Directory");}

The "affectation" which I was talking about is :

directory = myFileLocationKey.getString("Directory");
tnarol
 
Posts: 22
Joined: 21 Mar 2007, 18:56
Location: France

Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby alex » 14 Jun 2007, 19:56

> The "affectation" which I was talking about is :
>
> directory = myFileLocationKey.getString("Directory");

I could not reproduce your problem with code. It's probably some setting such as mixing debug/release or unicode/non unicode or something of sorts.

On my machine, this code:

Code: Select all

void WinRegistryTest::testTnarol()
{
   std::string directory;
   
   WinRegistryKey myFileLocationKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Direct3D\Drivers\Direct3D HAL");
   bool result = myFileLocationKey.exists();
   if (result)
   {
      result= myFileLocationKey.exists("Description");
      if (result)
      {
         directory = myFileLocationKey.getString("Description");
         std::cout << directory;
      }
   }
}


Executes cleanly and produces this in the console:
Code: Select all

Microsoft Direct3D Hardware acceleration through Direct3D HAL


I recommend you double check your project settings.

ALex
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby tnarol » 14 Jun 2007, 23:48

> I recommend you double check your project settings.

Ok thanks for trying to help. Which project settings are important ? What does the preprocessor definition POCO_DLL imply ? Should I define it when compiling the libraies ? Should I define it when compiling the code linking with the libraries ?
tnarol
 
Posts: 22
Joined: 21 Mar 2007, 18:56
Location: France

Re: Re: Re: _CrtIsValidPointer assertion when returning from WinRegistryKey::getString

Postby alex » 15 Jun 2007, 03:28

> Which project settings are important ? What does the preprocessor definition POCO_DLL imply ? Should I define it when compiling the libraies ? Should I define it when compiling the code linking with the libraries ?

POCO_DLL is a preprocessor definition that determines whether a library is built as dynamic (aka shared) library. POCO_STATIC, when defined, causes it to be built as static library. If none of the above is defined, POCO_DLL is used, defaulting to dynamic library.

Look into your compiler and linker command lines for the application and the library, those will tell you exactly what you are doing.

Alex
alex
 
Posts: 1114
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest