got error "this mock object (used in test configspi.spi_init) should be deleted but never is. Its address is @0x80b2a80"

3,414 views
Skip to first unread message

Rakesh navaneethakrishnan

unread,
Feb 3, 2013, 3:50:57 AM2/3/13
to googl...@googlegroups.com
Actually my project is C Project and i am declaring mock object in heap, but declared as a global variable.but still i got this error.
below one is my coed snippet. i dont think i can delete this object and it is not giving any memory leakage. Any idea guys?

//Mock class
class system_mock{
public:
    MOCK_METHOD2(open, int(const char *filename, int flags));
    MOCK_METHOD3(ioctl, int(int filedes, long unsigned int command,unsigned char* p));
    MOCK_METHOD3(ioctl, int(int filedes, long unsigned int command, spi_ioc_transfer* mesg));
};
system_mock mock;
//System call whihc have been mocked
int open (const char *filename, int flags)
{
    return mock.open(filename, flags);
}
int ioctl (int filedes, long unsigned int command,unsigned char* p){
    return mock.ioctl(filedes,command,p);
}
int ioctl (int filedes, long unsigned int command, spi_ioc_transfer* mesg)
{
    return mock.ioctl(filedes,command,mesg);
}

TEST(configspi, spi_init) {
  EXPECT_CALL(mock,open(An<const char*>(),_))
              .WillRepeatedly(Return(1));
  EXPECT_CALL(mock,ioctl(An<int>(),An<unsigned long int>(),An<unsigned char*>()))
              .Times(18)
              .WillRepeatedly(Return(0));
  EXPECT_EQ(0, spi_init());
}

Billy Donahue

unread,
Feb 3, 2013, 9:42:25 AM2/3/13
to Rakesh navaneethakrishnan, googl...@googlegroups.com


On Sunday, February 3, 2013, Rakesh navaneethakrishnan wrote:
Actually my project is C Project and i am declaring mock object in heap, but declared as a global variable.but still i got this error.
below one is my coed snippet. i dont think i can delete this object and it is not giving any memory leakage. Any idea guys?

//Mock class
class system_mock{
public:
    MOCK_METHOD2(open, int(const char *filename, int flags));
    MOCK_METHOD3(ioctl, int(int filedes, long unsigned int command,unsigned char* p));
    MOCK_METHOD3(ioctl, int(int filedes, long unsigned int command, spi_ioc_transfer* mesg));
};
system_mock mock;

This system_mock is not heap allocated.

Try system_mock* mock = new system_mock;

You should really be making a fresh one per TEST, deleting it at the end of each test.
The destructor is important, and it is where unmet expectations are detected.
 
//System call whihc have been mocked
int open (const char *filename, int flags)
{
    return mock.open(filename, flags);
}
int ioctl (int filedes, long unsigned int command,unsigned char* p){
    return mock.ioctl(filedes,command,p);
}
int ioctl (int filedes, long unsigned int command, spi_ioc_transfer* mesg)
{
    return mock.ioctl(filedes,command,mesg);
}

Note that these aren't going to actually replace the C system calls, which are declared 'extern "C"' in system headers.
 

TEST(configspi, spi_init) {
  EXPECT_CALL(mock,open(An<const char*>(),_))
              .WillRepeatedly(Return(1));
  EXPECT_CALL(mock,ioctl(An<int>(),An<unsigned long int>(),An<unsigned char*>()))
              .Times(18)
              .WillRepeatedly(Return(0));
  EXPECT_EQ(0, spi_init());
}

--
 
---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
 
 
 

Rakesh navaneethakrishnan

unread,
Feb 4, 2013, 8:17:29 AM2/4/13
to googl...@googlegroups.com
Hello thanks for your reply..i made one typo error which changes the meaning..
my actual statement is,
Actually my project is C Project and i am declaring mock object not in heap, but declared as a global variable.but still i got this error .
so definitely it will destructed when it exits scope.so there wont be any memory leakage.then why am i getting any issue.

Billy Donahue

unread,
Feb 4, 2013, 10:14:50 AM2/4/13
to Rakesh navaneethakrishnan, googl...@googlegroups.com
You have to delete the mock object before the TEST ends.


--

Billy Donahue

unread,
Feb 4, 2013, 10:16:03 AM2/4/13
to Rakesh navaneethakrishnan, googl...@googlegroups.com
but again, your mocking of C API functions (ioctl, etc) isn't going to work unless you're doing something very magical that I can't see.
Reply all
Reply to author
Forward
0 new messages