473,839 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Funny problem with virtual method

Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponen t
{
public:
// This method implements an abstract method in NcTypedComponen t
virtual NcType type() const;
private:
int dim_to_index(Nc Dim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That's right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What's going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.

Nov 24 '06 #1
10 2359

Markus Svilans napsal:
Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponen t
{
public:
// This method implements an abstract method in NcTypedComponen t
virtual NcType type() const;
private:
int dim_to_index(Nc Dim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That's right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What's going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.
I think you are somewhere overwriting memory.

Nov 24 '06 #2
"Markus Svilans" <ms******@gmail .comwrote in message
news:11******** **************@ l39g2000cwd.goo glegroups.com.. .
Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponen t
{
public:
// This method implements an abstract method in NcTypedComponen t
virtual NcType type() const;
private:
int dim_to_index(Nc Dim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That's right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What's going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.
This type of error, you get an access violatoin, but you do something
totally unrelated and it goes away, means you have a buffer overflow
somewhere. Adding that new variable shuffled the variables around a bit,
and it just means you're overwriting a variable that isn't immediately
causing an access violation, but your problem is still there.

Show a complete compilable program that causes the problem. I'll bet that
you won't even send it to this newsgroup, however, as the act of doing this
will show you where the problem is. If not, send the code.
Nov 24 '06 #3

Markus Svilans wrote in message
<11************ **********@l39g 2000cwd.googleg roups.com>...
>Hi,
I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!
The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponen t { public:
// This method implements an abstract method in NcTypedComponen t
virtual NcType type() const;
private:
int dim_to_index( NcDim* rdim );
int the_id;
};

Original implementation of type():

NcType NcVar::type() const {
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
What is 'nc_type'?
nc_inq_vartype( the_file->id(), the_id, &typ );
What is 'the_file' pointing to?
// return (NcType)typ;

What is 'NcType'?

Pick one (try all).
return NcType( typ );
return static_cast<NcT ype>( typ );
return dynamic_cast<Nc Type>( typ );
return reinterpret_cas t<NcType>( typ );

Remove the cast and see what your compiler is trying to do!
The 'C' cast may be covering up an error.

return typ;
>}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.
Show the call that works.

--
Bob R
POVrookie
Nov 24 '06 #4

Jim Langston wrote:
>
This type of error, you get an access violatoin, but you do something
totally unrelated and it goes away, means you have a buffer overflow
somewhere. Adding that new variable shuffled the variables around a bit,
and it just means you're overwriting a variable that isn't immediately
causing an access violation, but your problem is still there.

Show a complete compilable program that causes the problem. I'll bet that
you won't even send it to this newsgroup, however, as the act of doing this
will show you where the problem is. If not, send the code.
Hi Jim,

Thanks for the reply. I'll put together a compilable program that
should demonstrate the problem, assuming you have the NetCDF library
installed (http://www.unidata.ucar.edu/software/netcdf/).

The funny thing is, that access violation pops up when I try to compile
and run one of the demo programs that comes with the library.

Regards,
Markus.

Nov 24 '06 #5
Markus Svilans wrote:
Hi,

I have a weird problem in a virtual method. The original method code
raises an access violation when it is run. The solution to the problem
is to declare a dummy integer inside the virtual method. Then the
access violation no longer occurs!

The following class (condensed from the NetCDF C++ interface):

class NcVar : public NcTypedComponen t
{
public:
// This method implements an abstract method in NcTypedComponen t
virtual NcType type() const;
private:
int dim_to_index(Nc Dim* rdim);
int the_id;
};

Original implementation of type():

NcType NcVar::type() const
{
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Running this code raises an access violation. Calling the exact same
code outside of the method works perfectly.

Modified implementation of type():

NcType NcVar::type() const
{
int x; // That's right, adding this makes it work

nc_type typ = 0;
nc_inq_vartype( the_file->id(), the_id, &typ);
return (NcType)typ;
}

Adding the "int x" makes the code run, without any access violations!
A few other of the virtual methods in other classes have a similar
problem.

What's going on here?

FYI, this code is from the NetCDF C++ interface. I am trying to use it
with Borland C++ Builder 6. I did not write the NetCDF C++ interface,
but I am trying to use it as-is, and the access violation problem has
been driving me nuts.

Any help would be appreciated.

Regards,
Markus.
when you "return (NcType)type;" you are copying the NcType object back to
the caller. NcType is an object, I don't know what nc_type is, but it could
be that Borland is just being way too liberal in its type casting. Try
getting rid of the "int x;" and replace the return with:

NcType ncType(nc_type) ; // or however one usually converts an nc_type to
NcType
return ncType;

If the problem goes away, it means that NcType is bigger than nc_type and
you tried to copy junk outside of your stack frame.

Nov 28 '06 #6

Jim Langston wrote:
This type of error, you get an access violatoin, but you do something
totally unrelated and it goes away, means you have a buffer overflow
somewhere. Adding that new variable shuffled the variables around a bit,
and it just means you're overwriting a variable that isn't immediately
causing an access violation, but your problem is still there.

Show a complete compilable program that causes the problem. I'll bet that
you won't even send it to this newsgroup, however, as the act of doing this
will show you where the problem is. If not, send the code.
Hi Jim,

The program I am trying to run is here:
http://www.unidata.ucar.edu/software...temp_4D_wr.cpp

I am using Borland C++ Builder 6 Personal, with the Windows DLL version
of the netCDF 3.6.1 library. In order to compile the program, you will
have to install the netCDF library somehow.

I am assuming that my problem lies somewhere with the C++ interface to
the library, and not the library itself, because I can use the C
function calls without any access violations.

If you would like me to send you a copy of the C++ interface of the
library, please let me know.

Regards,
Markus.

Nov 28 '06 #7

BobR wrote:
NcType NcVar::type() const {
// nc_inq_vartype is a library function that works normally
// outside of this method
nc_type typ;

What is 'nc_type'?
nc_type is an enumerated type.
nc_inq_vartype( the_file->id(), the_id, &typ );

What is 'the_file' pointing to?
the_file is an NcFile object, and the id() method just returns an
integer representing the unique identifier of the NcFile object.

the_id is another integer (a member of the NcVar class) that is passed
to nc_inq_vartype( ).
// return (NcType)typ;

What is 'NcType'?
NcType is an enumerated type, that mirrors the nc_type enumerator
member by member. (This is why directly casting from one to another
works.)
Pick one (try all).
return NcType( typ );
return static_cast<NcT ype>( typ );
return dynamic_cast<Nc Type>( typ );
return reinterpret_cas t<NcType>( typ );
I tried all of these, and those that compiled did not aleviate the
problem...

Regards,
Markus.

Nov 28 '06 #8

Tim D wrote:
when you "return (NcType)type;" you are copying the NcType object back to
the caller. NcType is an object, I don't know what nc_type is, but it could
be that Borland is just being way too liberal in its type casting. Try
getting rid of the "int x;" and replace the return with:

NcType ncType(nc_type) ; // or however one usually converts an nc_type to
NcType
return ncType;

If the problem goes away, it means that NcType is bigger than nc_type and
you tried to copy junk outside of your stack frame.

NcType and nc_type are both enumerators, whose members have a 1:1
correspondence to each other. In effect NcType is an alias for nc_type.
The two types exist because the nc_type enumerator is used in the pure
C library function calls, and NcType is used for the C++ interface.

Regards,
Markus.

Nov 28 '06 #9

Markus Svilans wrote in message ...
>
BobR wrote:
>Pick one (try all).
return NcType( typ );
return static_cast<NcT ype>( typ );
return dynamic_cast<Nc Type>( typ );
return reinterpret_cas t<NcType>( typ );

I tried all of these, and those that compiled did not aleviate the
problem...
Well, duh. It wasn't meant to 'aleviate the problem', but, to show what type
the function was returning and what was expected. ( I should have explicitely
asked you to post the errors on each.). But, if you have to cast, use one of
those (probably 'static_cast').

So, what is the error on:
return typ;

( remove the line number and pathname, just post the error part.)

--
Bob R
POVrookie
Nov 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2863
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
8
2467
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
7
1416
by: al | last post by:
class Base { public: virtual void method(); }; class Derive : public Base { public: void method();
5
3011
by: eScrewDotCom | last post by:
www.eScrew.com eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing
0
2466
by: eScrewDotCom | last post by:
eScrew Welcome to eScrew! eScrew is eScrew and this is eScrew story. eScrew will tell you eScrew story if you promise eScrew to consider eScrew story as joke. eScrew story is very funny. eScrew story is so funny that eScrew will have to take break from time to time because eScrew needs some rest from laughing. Oh boy, here it comes... eScrew funny laugh laughing screaming crying must stop can not take any more this is killing eScrew...
0
3947
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
12
4497
by: Daniel Earwicker | last post by:
I wrote two trivial test programs that do a billion iterations of a virtual method call, first in C# (Visual Studio 2005): Thing t = new DerivedThing(); for (System.Int64 n = 0; n < 10000000000; n++) t.method(); Then in C++ (using Visual C++ 2005): Thing *t = new DerivedThing();
0
1809
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or overridden by the type. The reason is because the CLR can just call these methods nonvirtually and System.ValueType overrides all of these virtual methods and expects the value in the this argument to refer to an unboxed value type instance. Remember, a...
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10914
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10597
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10656
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7834
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4071
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.