- 論壇徽章:
- 0
|
本帖最后由 banceleung 于 2010-03-15 09:50 編輯
先貼代碼后解釋 #include <pthread.h>
- #include <sys/epoll.h>
- class CNetMsgImp
- {
- public:
- struct epoll_event m_events[12];
- //char temp[1000]; //-------!!!!!!!!!!!!----------
- int m_kdpfd;
- CNetMsgImp();
- ~CNetMsgImp();
- };
- CNetMsgImp::CNetMsgImp()
- {
- m_kdpfd = epoll_create(11+11);
- m_events[11].events = 11;
- epoll_wait(m_kdpfd,m_events, 20, -1);
- }
- int main(int argc, char* argv[])
- {
- CNetMsgImp* netMsgImg = new CNetMsgImp;
- }
復(fù)制代碼 g++ -pthread -o test test.cpp
valgrind --tool=memcheck ./test
==11054== Memcheck, a memory error detector.
==11054== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==11054== Using LibVEX rev 1804, a library for dynamic binary translation.
==11054== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==11054== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==11054== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==11054== For more details, rerun with: -v
==11054==
==11054== Syscall param epoll_wait(events) points to unaddressable byte(s)
==11054== at 0x40007F2: (within /lib/ld-2.7.so)
==11054== by 0x8048623: main (in /mnt/hgfs/NetFrameL/test)
==11054== Address 0x42c10bc is 0 bytes after a block of size 148 alloc'd
==11054== at 0x4023294: operator new(unsigned) (vg_replace_malloc.c:224)
==11054== by 0x8048615: main (in /mnt/hgfs/NetFrameL/test) #include <pthread.h>
- #include <sys/epoll.h>
- class CNetMsgImp
- {
- public:
- struct epoll_event m_events[12];
- char temp[1000];
- int m_kdpfd;
- CNetMsgImp();
- ~CNetMsgImp();
- };
- CNetMsgImp::CNetMsgImp()
- {
- m_kdpfd = epoll_create(11+11);
- m_events[11].events = 11;
- epoll_wait(m_kdpfd,m_events, 20, -1);
- }
- int main(int argc, char* argv[])
- {
- CNetMsgImp* netMsgImg = new CNetMsgImp;
- }
復(fù)制代碼 g++ -pthread -o test test.cpp
valgrind --tool=memcheck ./test
==11092== Memcheck, a memory error detector.
==11092== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==11092== Using LibVEX rev 1804, a library for dynamic binary translation.
==11092== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==11092== Using valgrind-3.3.0-Debian, a dynamic binary instrumentation framework.
==11092== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==11092== For more details, rerun with: -v
==11092==
如上,如果在類里不加那個(gè)char temp[1000];則valgrind會(huì)報(bào)錯(cuò),運(yùn)行時(shí)程序會(huì)有棧錯(cuò)誤,明確的說會(huì)侵占對(duì)象內(nèi)存,改變現(xiàn)有對(duì)象的內(nèi)部數(shù)據(jù)。
以上代碼是原有程序的簡化,已能說明問題。
- #include <sys/epoll.h>
- class A
- {
- public:
- int a;
- struct epoll_event m_events[12];
- //char temp[1000];
- A();
- ~A();
- };
- A::A()
- {
- a = epoll_create(11+11);
- epoll_wait(a,m_events, 20, -1);
- }
- int main(void)
- {
- A* b = new A;
- }
復(fù)制代碼 來個(gè)更簡單的,問題照舊。 |
|