Hi,
I wrote a small class to enumerate available networks on a smartphone :
class CNetwork
{
public:
CNetwork() {};
CNetwork(CStrin g& netName, GUID netguid):
_netname(netNam e), _netguid(netgui d) {}
~CNetwork() {}
CString& getName() { return _netname; }
GUID getGuid() { return _netguid; }
private:
CString _netname;
GUID _netguid;
};
class CNetworkList
{
public:
typedef std::list<CNetw ork*>::iterator NetworkListIt;
CNetworkList() {}
~CNetworkList()
{
Clear();
}
CNetworkList::C NetworkList(con st CNetworkList& rhs) {
CopyObj(rhs);
}
CNetworkList& CNetworkList::o perator=(const CNetworkList& rhs)
{
CopyObj(rhs);
return *this;
}
void CopyObj(const CNetworkList& rhs)
{
_netlist = rhs._netlist;
}
void Clear()
{
for_each( _netlist.begin( ), _netlist.end(), DeletePointer ());
}
void Add(CNetwork* network)
{
_netlist.push_b ack(network);
}
const CNetwork* getNetwork(CStr ing netNameOrGuid)
{
if ((netNameOrGuid .GetAt(0) == '{') &&
netNameOrGuid.G etLength() == 39)
{
CLSID guid;
if
(SUCCEEDED(CLSI DFromString(net NameOrGuid.GetB uffer(),&guid)) )
return getNetwork(guid );
}
else
{
NetworkListIt it;
for (it = _netlist.begin( ); it != _netlist.end(); ++it)
if (!(*it)->getName().Comp areNoCase(netNa meOrGuid))
return (*it);
}
return NULL;
}
const CNetwork* getNetwork(CLSI D guid)
{
if (!_netlist.empt y())
Clear();
NetworkListIt it;
for (it = _netlist.begin( ); it != _netlist.end(); ++it)
if ((*it)->getGuid() == guid)
return (*it);
return NULL;
}
private:
std::list<CNetw ork*_netlist;
};
CNetworkList getNetworkList( )
{
int i = 0;
HRESULT hResult;
CNetworkList netList;
while( ConnMgrEnumDest inations( i, &connInfo ) == 0 )
{
CNetwork* pNetWork = new
CNetwork(CStrin g(connInfo.szDe scription), connInfo.guid);
if (pNetWork)
{
netList.Add(pNe tWork);
}
i++;
}
}
When I call this code :
m_NetworkList = getNetworkList( );
I got an assert in a Cstring desctructor so I suppose my class is doing
wrong...
When I debug in step by step I don't really understand the calls, it
seems Clear() is called why it shoudn't.
I wrote a small class to enumerate available networks on a smartphone :
class CNetwork
{
public:
CNetwork() {};
CNetwork(CStrin g& netName, GUID netguid):
_netname(netNam e), _netguid(netgui d) {}
~CNetwork() {}
CString& getName() { return _netname; }
GUID getGuid() { return _netguid; }
private:
CString _netname;
GUID _netguid;
};
class CNetworkList
{
public:
typedef std::list<CNetw ork*>::iterator NetworkListIt;
CNetworkList() {}
~CNetworkList()
{
Clear();
}
CNetworkList::C NetworkList(con st CNetworkList& rhs) {
CopyObj(rhs);
}
CNetworkList& CNetworkList::o perator=(const CNetworkList& rhs)
{
CopyObj(rhs);
return *this;
}
void CopyObj(const CNetworkList& rhs)
{
_netlist = rhs._netlist;
}
void Clear()
{
for_each( _netlist.begin( ), _netlist.end(), DeletePointer ());
}
void Add(CNetwork* network)
{
_netlist.push_b ack(network);
}
const CNetwork* getNetwork(CStr ing netNameOrGuid)
{
if ((netNameOrGuid .GetAt(0) == '{') &&
netNameOrGuid.G etLength() == 39)
{
CLSID guid;
if
(SUCCEEDED(CLSI DFromString(net NameOrGuid.GetB uffer(),&guid)) )
return getNetwork(guid );
}
else
{
NetworkListIt it;
for (it = _netlist.begin( ); it != _netlist.end(); ++it)
if (!(*it)->getName().Comp areNoCase(netNa meOrGuid))
return (*it);
}
return NULL;
}
const CNetwork* getNetwork(CLSI D guid)
{
if (!_netlist.empt y())
Clear();
NetworkListIt it;
for (it = _netlist.begin( ); it != _netlist.end(); ++it)
if ((*it)->getGuid() == guid)
return (*it);
return NULL;
}
private:
std::list<CNetw ork*_netlist;
};
CNetworkList getNetworkList( )
{
int i = 0;
HRESULT hResult;
CNetworkList netList;
while( ConnMgrEnumDest inations( i, &connInfo ) == 0 )
{
CNetwork* pNetWork = new
CNetwork(CStrin g(connInfo.szDe scription), connInfo.guid);
if (pNetWork)
{
netList.Add(pNe tWork);
}
i++;
}
}
When I call this code :
m_NetworkList = getNetworkList( );
I got an assert in a Cstring desctructor so I suppose my class is doing
wrong...
When I debug in step by step I don't really understand the calls, it
seems Clear() is called why it shoudn't.
Comment