Do my types meet container requirements?

D

Dave

Hello all,

In the code below, can anybody see that I am violating any of the rules that
types must abide by to be held in containers? I get a memory access problem
when a ZoneSetsType destructs...

Thanks,
Dave

struct OnePolygonType
{
unsigned int ModTime;
std::string Pts;
std::string Type;

unsigned int Id; // Unused in comparing objects

// Implement a strict weak ordering
bool operator<(const OnePolygonType &rhs) const
{
if (ModTime < rhs.ModTime)
return true;
else if (ModTime > rhs.ModTime)
return false;

if (Pts < rhs.Pts)
return true;
else if (Pts > rhs.Pts)
return false;

if (Type < rhs.Type)
return true;
else if (Type > rhs.Type)
return false;

return false;
}

bool operator==(const OnePolygonType &rhs) const
{
return (
(ModTime == rhs.ModTime) &&
(Pts == rhs.Pts) &&
(Type == rhs.Type)
);
}
};

typedef std::set<OnePolygonType> PolygonSetType;

struct ZoneSetType
{
PolygonSetType SafeZones;
PolygonSetType HotZones;

// Implement a strict weak ordering
bool operator<(const ZoneSetType &rhs) const
{
if (SafeZones < rhs.SafeZones)
return true;
else if (SafeZones > rhs.SafeZones)
return false;

if (HotZones < rhs.HotZones)
return true;
else if (HotZones > rhs.HotZones)
return false;

return false;
}

bool operator==(const ZoneSetType &rhs) const
{
return (
(SafeZones == rhs.SafeZones) &&
(HotZones == rhs.HotZones)
);
}
};

typedef std::map<std::string, ZoneSetType> ZoneSetsType;
 
K

Karthik Kumar

Dave said:
Hello all,

In the code below, can anybody see that I am violating any of the rules that
types must abide by to be held in containers? I get a memory access problem
when a ZoneSetsType destructs...

Thanks,
Dave
[code snipped]

In the code you had shown you are not creating any instance of
ZoneSetsType anywhere (statically / dynamically ) .
Also your classes do not seem to have any pointers / files as its
members. (and hence the default copy ctor. / assignment operator /
destructor would be fine). So the problem does not seem to be with the
class definitions but with the client code of the class. Please post the
precise code that you were working on, to understand the problem better.
 
D

Dave

Karthik Kumar said:
Dave said:
Hello all,

In the code below, can anybody see that I am violating any of the rules that
types must abide by to be held in containers? I get a memory access problem
when a ZoneSetsType destructs...

Thanks,
Dave
[code snipped]

In the code you had shown you are not creating any instance of
ZoneSetsType anywhere (statically / dynamically ) .
Also your classes do not seem to have any pointers / files as its
members. (and hence the default copy ctor. / assignment operator /
destructor would be fine). So the problem does not seem to be with the
class definitions but with the client code of the class. Please post the
precise code that you were working on, to understand the problem better.

This will be out of context, but here's what the client code is so far (I've
just started developing this function).
As you can see, I'm simply locally declaring and initializing an instance of
the problem type. When ZoneSetsFromMessage goes out of scope and destructs
upon function exit, I get the memory access problem. Please note that this
happens even if I do not initialize ZoneSetsFromMessage. Default
constructing it and letting it destruct naturally reproduces the problem.

I tried compiling the code from my original post on Comeau online and it
complained about some concept checks failing. For example:

"concept_checks.h", line 405: error: no operator "!=" matches these operands
operand types are: OnePolygonType != OnePolygonType
if (__a == __b || __a != __b) return __a;
^
detected during:
instantiation of "_Type

_STL_ERROR::__equality_comparable_requirement_violation(_
Type, _Type) [with _Type=OnePolygonType]" at line 555
instantiation of "void
[error snipped]

There were 5 similar errors. I don't understand what C++ rule this is
telling me I'm violating.

Thanks,
Dave

Here's the client code:

void PsrsClient::HandleUnitZones(const unsigned char *&p, size_t &s)
{
UnitZonesIbMessage0 Msg(p, s);
PolygonTransferVector_t Polygons = Msg.GetPolygons();
ZoneSetsType ZoneSetsFromMessage = CreateZoneSetsTypeFromMessage(Polygons);

// assert(ZoneSetsFromMessage.size() == 1);
// ZoneSetType ZoneSet = ZoneSetsFromMessage.begin()->second;
}
 
A

Andre Kostur

[snippage]
"concept_checks.h", line 405: error: no operator "!=" matches these
operands
operand types are: OnePolygonType != OnePolygonType
if (__a == __b || __a != __b) return __a;
^
detected during:
instantiation of "_Type

OK... what part of this error message is bad? It's telling you that it
can't find an operator!= that matches the parameters that you're using with
it. Note that in the code you provided earlier I don't see you providing
an operator!=() anywhere.... (I _do_ see an operator==(), but that's a
whole different story....)
_STL_ERROR::__equality_comparable_requirement_violation(_
Type, _Type) [with _Type=OnePolygonType]" at
line 555
instantiation of "void
[error snipped]

Umm... line 555 of what?
 
D

Dave

Andre Kostur said:
[snippage]
"concept_checks.h", line 405: error: no operator "!=" matches these
operands
operand types are: OnePolygonType != OnePolygonType
if (__a == __b || __a != __b) return __a;
^
detected during:
instantiation of "_Type

OK... what part of this error message is bad? It's telling you that it
can't find an operator!= that matches the parameters that you're using with
it. Note that in the code you provided earlier I don't see you providing
an operator!=() anywhere.... (I _do_ see an operator==(), but that's a
whole different story....)
Indeed, but nor do I ever use operator!= (my code *does* compile) and nor is
it a requirement of a type storable in an STL container!
_STL_ERROR::__equality_comparable_requirement_violation(_
Type, _Type) [with _Type=OnePolygonType]" at
line 555
instantiation of "void
[error snipped]

Umm... line 555 of what?
 
A

Andre Kostur

Andre Kostur said:
[snippage]
"concept_checks.h", line 405: error: no operator "!=" matches these
operands
operand types are: OnePolygonType != OnePolygonType
if (__a == __b || __a != __b) return __a;
^
detected during:
instantiation of "_Type

OK... what part of this error message is bad? It's telling you that
it can't find an operator!= that matches the parameters that you're
using with
it. Note that in the code you provided earlier I don't see you
providing an operator!=() anywhere.... (I _do_ see an operator==(),
but that's a whole different story....)
Indeed, but nor do I ever use operator!= (my code *does* compile) and
nor is it a requirement of a type storable in an STL container!

Huh? What's this error message about line 405 of "concept_checks.h" ?
 
K

Karthik Kumar

Dave said:
This will be out of context, but here's what the client code is so far (I've
just started developing this function).
As you can see, I'm simply locally declaring and initializing an instance of
the problem type. When ZoneSetsFromMessage goes out of scope and destructs
upon function exit, I get the memory access problem. Please note that this
happens even if I do not initialize ZoneSetsFromMessage. Default
constructing it and letting it destruct naturally reproduces the problem.

I tried compiling the code from my original post on Comeau online and it
complained about some concept checks failing. For example:

"concept_checks.h", line 405: error: no operator "!=" matches these operands
operand types are: OnePolygonType != OnePolygonType
if (__a == __b || __a != __b) return __a;
^
detected during:
instantiation of "_Type

_STL_ERROR::__equality_comparable_requirement_violation(_
Type, _Type) [with _Type=OnePolygonType]" at line 555
instantiation of "void
[error snipped]

There were 5 similar errors. I don't understand what C++ rule this is
telling me I'm violating.

Thanks,
Dave

Here's the client code:

void PsrsClient::HandleUnitZones(const unsigned char *&p, size_t &s)
{
UnitZonesIbMessage0 Msg(p, s);
PolygonTransferVector_t Polygons = Msg.GetPolygons();
ZoneSetsType ZoneSetsFromMessage = CreateZoneSetsTypeFromMessage(Polygons);

// assert(ZoneSetsFromMessage.size() == 1);
// ZoneSetType ZoneSet = ZoneSetsFromMessage.begin()->second;
}

There apppears to be two more objects in the local function.
Are you sure it is the ZoneSetsType that is causing the problem
and not the other objects when go out of scope, (say, Msg of type
UnitZonesIbMessage0 ).
 
D

Dave

Karthik Kumar said:
Dave said:
This will be out of context, but here's what the client code is so far (I've
just started developing this function).
As you can see, I'm simply locally declaring and initializing an instance of
the problem type. When ZoneSetsFromMessage goes out of scope and destructs
upon function exit, I get the memory access problem. Please note that this
happens even if I do not initialize ZoneSetsFromMessage. Default
constructing it and letting it destruct naturally reproduces the problem.

I tried compiling the code from my original post on Comeau online and it
complained about some concept checks failing. For example:

"concept_checks.h", line 405: error: no operator "!=" matches these operands
operand types are: OnePolygonType != OnePolygonType
if (__a == __b || __a != __b) return __a;
^
detected during:
instantiation of "_Type

_STL_ERROR::__equality_comparable_requirement_violation(_
Type, _Type) [with _Type=OnePolygonType]" at line 555
instantiation of "void
[error snipped]

There were 5 similar errors. I don't understand what C++ rule this is
telling me I'm violating.

Thanks,
Dave

Here's the client code:

void PsrsClient::HandleUnitZones(const unsigned char *&p, size_t &s)
{
UnitZonesIbMessage0 Msg(p, s);
PolygonTransferVector_t Polygons = Msg.GetPolygons();
ZoneSetsType ZoneSetsFromMessage = CreateZoneSetsTypeFromMessage(Polygons);

// assert(ZoneSetsFromMessage.size() == 1);
// ZoneSetType ZoneSet = ZoneSetsFromMessage.begin()->second;
}

There apppears to be two more objects in the local function.
Are you sure it is the ZoneSetsType that is causing the problem
and not the other objects when go out of scope, (say, Msg of type
UnitZonesIbMessage0 ).

Actually, I believe I've now found out where the problem lies.

It's a VC++ 6.0 thing. The call to CreateZoneSetsTypeFromMessage() is
across a DLL boundary, and each DLL has its own heap in this environment.
So, memory is allocated in one heap and deallocated in another. Ouch!

I guess I'll have to terminate this thread now since we've gotten into a
particular implementation's quirks...

Thanks for the help!

Dave
 
P

Pete Becker

Dave said:
It's a VC++ 6.0 thing. The call to CreateZoneSetsTypeFromMessage() is
across a DLL boundary, and each DLL has its own heap in this environment.
So, memory is allocated in one heap and deallocated in another.

Only if you build it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top