473,225 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

memory access violation with std::list

I have a list of pointers to structs as a private member of a class.
If I call begin() in the same function as I added the data then no
access violation occurs. However, if I try to call begin() in a member
function of the same class I get a memory access violation.

For example:

// this is fine, no error
void CBase::FuncA( void ) {
plugin_info_t *plugin_info = new plugin_info_t;
plugin_info->pPluginSend = ProcPluginSend;
plugin_info->pGetUserList = 0;
lstPlugins.push_back(plugin_info);

list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}

// this causes an access violation
void CBase::FuncB( void ) {
list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}
Jul 22 '05 #1
3 6760
"Kyle Teague" <si********@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
I have a list of pointers to structs as a private member of a class.
If I call begin() in the same function as I added the data then no
access violation occurs. However, if I try to call begin() in a member
function of the same class I get a memory access violation.

For example:

// this is fine, no error
Possibly fine, but not necessarily. The lack of an adverse reaction does not
always mean that all is well.
void CBase::FuncA( void ) {
No need for 'C' on your class names.
http://www.jelovic.com/articles/stupid_naming.htm
plugin_info_t *plugin_info = new plugin_info_t;
plugin_info->pPluginSend = ProcPluginSend;
plugin_info->pGetUserList = 0;
lstPlugins.push_back(plugin_info);

list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}
// this causes an access violation
void CBase::FuncB( void ) {
list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}


There's nothing obviously wrong. You'll need to give us the definition of
class CBase and the code that creates the CBase object and calls these
members. The symptom suggests that the CBase object is invalid (e.g., been
destroyed) at the time you call FuncB(), because a call to the list's
begin() should not crash, whether you have added any items to the list or
not.

DW

Jul 22 '05 #2
"David White" <no@email.provided> wrote in message news:<Xp******************@nasal.pacific.net.au>.. .
"Kyle Teague" <si********@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
I have a list of pointers to structs as a private member of a class.
If I call begin() in the same function as I added the data then no
access violation occurs. However, if I try to call begin() in a member
function of the same class I get a memory access violation.

For example:

// this is fine, no error


Possibly fine, but not necessarily. The lack of an adverse reaction does not
always mean that all is well.
void CBase::FuncA( void ) {


No need for 'C' on your class names.
http://www.jelovic.com/articles/stupid_naming.htm
plugin_info_t *plugin_info = new plugin_info_t;
plugin_info->pPluginSend = ProcPluginSend;
plugin_info->pGetUserList = 0;
lstPlugins.push_back(plugin_info);

list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}


// this causes an access violation
void CBase::FuncB( void ) {
list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}


There's nothing obviously wrong. You'll need to give us the definition of
class CBase and the code that creates the CBase object and calls these
members. The symptom suggests that the CBase object is invalid (e.g., been
destroyed) at the time you call FuncB(), because a call to the list's
begin() should not crash, whether you have added any items to the list or
not.

DW


My actual class was not named CBase, sorry for the confusion. By the
way, it crashes for ALL the lists at the same point. I've included
what was needed for the class, it

struct plugin_info_t;
struct user_info_t;

typedef int (* PluginMainProc)(UINT event, WPARAM wParam, LPARAM
lParam);
typedef std::list<user_info_t*>& (* GetUserListProc)();
typedef int (* GetBattleSocketProc)();
typedef LRESULT (* PluginSendProc)(UINT id, WPARAM wParam, LPARAM
lParam);
typedef BOOL (* BattleIncomingProc)(BYTE id, WORD length, LPVOID
data);
typedef BOOL (* MessageIncomingProc)(DWORD id, DWORD ping, DWORD
flags, LPSTR name, LPSTR text);

struct plugin_info_t
{
DWORD cVersion;
char cTitle[256];

PluginSendProc pPluginSend;
GetUserListProc pGetUserList;

BattleIncomingProc cBattleIncoming;
MessageIncomingProc cMessageIncoming;
};

struct user_info_t {
char name[64];
char statstring[512];
unsigned long flags;
unsigned long ping;
};

using namespace std;

LRESULT ProcPluginSend(UINT id, WPARAM wParam, LPARAM lParam);

class PluginHandler
{
string plugdir;
list<HMODULE> lstInstances;
list<FARPROC> lstProcs;
list<plugin_info_t*> lstPlugins;
public:
PluginHandler(char *dir);
~PluginHandler();
void GetPlugins();
int LoadPlugin(char *file);

void SendBNCSData(DWORD id, DWORD ping, DWORD uflags, LPSTR name,
LPSTR text);

};

PluginHandler::PluginHandler(char *dir)
{
plugdir = dir;
}

//crashes here
PluginHandler::~PluginHandler()
{
list<plugin_info_t*>::iterator i;
for(i=lstPlugins.begin(); i!=lstPlugins.end(); i++)
delete *i;

list<HMODULE>::iterator j;
for(j=lstInstances.begin(); j!=lstInstances.end(); j++)
FreeLibrary(*j);
}

void PluginHandler::GetPlugins()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char *path = new char[plugdir.size() + 3];
strcpy(path, plugdir.c_str());
strcat(path, "\\*");
hFind=FindFirstFile(path, &FindFileData);

do {
if( FindFileData.dwFileAttributes == 0x20) {
char *p = FindFileData.cFileName;

// is this a dll? if it is, load it
if (!strnicmp(p + strlen(p) - 4, ".dll", 4))
LoadPlugin(p);
}
} while ( FindNextFile(hFind, &FindFileData) );

FindClose(hFind);
delete [] path;
}

//crashes here too
void PluginHandler::SendBNCSData(DWORD id, DWORD ping, DWORD uflags,
LPSTR name, LPSTR text)
{
list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}

LRESULT ProcPluginSend(UINT id, WPARAM wParam, LPARAM lParam)
{
/*switch(id) {
case ZBF_APPENDTEXT:
cout << (char *)lParam << endl;
//control->_richedit.AppendText((COLORREF)wParam, (char *)lParam);
return 0;
case ZBF_TIMESTAMP:
cout << "[TIMESTAMP] ";
return 0;
case ZBF_SENDTEXT:
pBot->SendText((char *)lParam);
//BnetSend((char *)lParam);
//control->_sendtext.Clear();
return 0;
} */

return -1;
}
Jul 22 '05 #3
"Kyle Teague" <si********@hotmail.com> wrote in message
news:d4**************************@posting.google.c om...
My actual class was not named CBase, sorry for the confusion. By the
way, it crashes for ALL the lists at the same point. I've included
what was needed for the class, it

struct plugin_info_t;
struct user_info_t;

typedef int (* PluginMainProc)(UINT event, WPARAM wParam, LPARAM
lParam);
typedef std::list<user_info_t*>& (* GetUserListProc)();
typedef int (* GetBattleSocketProc)();
typedef LRESULT (* PluginSendProc)(UINT id, WPARAM wParam, LPARAM
lParam);
typedef BOOL (* BattleIncomingProc)(BYTE id, WORD length, LPVOID
data);
typedef BOOL (* MessageIncomingProc)(DWORD id, DWORD ping, DWORD
flags, LPSTR name, LPSTR text);

struct plugin_info_t
{
DWORD cVersion;
char cTitle[256];

PluginSendProc pPluginSend;
GetUserListProc pGetUserList;

BattleIncomingProc cBattleIncoming;
MessageIncomingProc cMessageIncoming;
};

struct user_info_t {
char name[64];
char statstring[512];
unsigned long flags;
unsigned long ping;
};

using namespace std;

LRESULT ProcPluginSend(UINT id, WPARAM wParam, LPARAM lParam);

class PluginHandler
{
string plugdir;
list<HMODULE> lstInstances;
list<FARPROC> lstProcs;
list<plugin_info_t*> lstPlugins;
public:
PluginHandler(char *dir);
~PluginHandler();
void GetPlugins();
int LoadPlugin(char *file);

void SendBNCSData(DWORD id, DWORD ping, DWORD uflags, LPSTR name,
LPSTR text);

};

PluginHandler::PluginHandler(char *dir)
{
plugdir = dir;
}

//crashes here
PluginHandler::~PluginHandler()
{
list<plugin_info_t*>::iterator i;
for(i=lstPlugins.begin(); i!=lstPlugins.end(); i++)
delete *i;

list<HMODULE>::iterator j;
for(j=lstInstances.begin(); j!=lstInstances.end(); j++)
FreeLibrary(*j);
}

void PluginHandler::GetPlugins()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
char *path = new char[plugdir.size() + 3];
strcpy(path, plugdir.c_str());
strcat(path, "\\*");
hFind=FindFirstFile(path, &FindFileData);

do {
if( FindFileData.dwFileAttributes == 0x20) {
char *p = FindFileData.cFileName;

// is this a dll? if it is, load it
if (!strnicmp(p + strlen(p) - 4, ".dll", 4))
LoadPlugin(p);
}
} while ( FindNextFile(hFind, &FindFileData) );

FindClose(hFind);
delete [] path;
}

//crashes here too
void PluginHandler::SendBNCSData(DWORD id, DWORD ping, DWORD uflags,
LPSTR name, LPSTR text)
{
list<plugin_info_t*>::iterator i;
i = lstPlugins.begin();
}


Given the plethora of types used, and the fact that this is obviously part
of a much bigger application, I can't determine the cause of the problem
from this code. However, I'm sure that the reason your program is crashing
is that the PluginHandler object has been destroyed or corrupted. In the
above function and in the destructor, the list is the first member you are
accessing and begin() is the first member function of it that you are
calling. If the 'this' pointer passed to a member function doesn't point to
a valid object, then the program will likely crash when you try to access
any member. The crash is merely a symptom of another problem that occurred
earlier. I suggest that you use a debugger to trace your program and see
what's going on, or add a whole lot of debug code to display various values
at various points to find out where the PluginHandler is becoming corrupted.

Note that this NG covers standard C++ only. It could be that your problem is
caused by not using the MS Windows API correctly, in which case you will get
a lot more help at a Windows programming newsgroup, such as
comp.os.ms-windows.programmer.win32.

DW

Jul 22 '05 #4

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

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.