-3

Im using new to allocate memory and initialize a variable of a class.

Here's the code used:

New operator:

ptrIssuResrc = new IssuResource();
-----------------------------------

Class definition:

class IssuResource
{
 public:
    // constructor: create a IssueResource object
    IssuResource(void)
    {
        lastUpdatedResource = 0;
    }
    UINT16 lastUpdatedResource;
    UINT32 conn_list[MAX_CONNECTION];
    bool addConnResourceToList(UINT32);

Using ptrIssuRescr:

class IssuResource *issuResrcPtr = NULL;
issuResrcPtr = card->ptrIssuResrc;

class IssuResource *ptrIssuResrc = card->ptrIssuResrc;
ptrIssuResrc->addConnResourceToList(connection->getLcn());

I'm facing some memory corruption due to this code, have narrowed down. Please help me with whats wrong here? How can I ensure there is no memory coruuption? I have moved the new operator to another location in the code and it worked fine. But I still need to ensure that wont cause new issues.

TIA

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 1
    You are not using `new` anywhere in this code. – Gorpik Jan 24 '13 at 08:16
  • maybe out of boundry when access `conn_list`, post more code relative to 1conn_list1 access? – billz Jan 24 '13 at 08:18
  • 1
    conn_list remains undefined, MAX_CONNECTION is not specified. – Arjan Jan 24 '13 at 08:18
  • can constructors be declared to take 'void'? – Chubsdad Jan 24 '13 at 08:20
  • @Gorpik -- New is used at the ptrIssuResrc = new IssuResource(); – Fatema Merchant Jan 24 '13 at 08:20
  • are you doing new and delete in different modules e.g. new in dll and delete in exe, which are probably built using different CRT versions? – Chubsdad Jan 24 '13 at 08:21
  • @Chubsdad -- I dont have a delete.. will add it – Fatema Merchant Jan 24 '13 at 08:22
  • @Chubsdad Yes... Taking void means that they take no argument. `someFunction(void)` is the same as `someFunction()`. Same applies for constructors. – Alderath Jan 24 '13 at 08:22
  • How do you know you have memory corruption? There's a lot you're not telling us. How have you narrowed it down? – Peter Wood Jan 24 '13 at 08:24
  • Sorry, I'm afraid I had scrolled too low. – Gorpik Jan 24 '13 at 08:24
  • @PeterWood - The program crashes ! If I remove the new operator , then it works fine. So thats the narrowing down :) – Fatema Merchant Jan 24 '13 at 08:28
  • It's probably nothing to do with the class you're showing us then. What do you do with `ptrIssuResrc`? Is it stored in another object? Do you follow the [Rule of Three](http://stackoverflow.com/q/4172722/1084416) for that class? – Peter Wood Jan 24 '13 at 08:30
  • Yes, ptrIssuRescr is stored in another object "card". – Fatema Merchant Jan 24 '13 at 08:33
  • @FatemaMerchant you are not updating relative code, no one can help you in here. – billz Jan 24 '13 at 08:34
  • The problem is in `card` then. – Peter Wood Jan 24 '13 at 08:51
  • @billz -- Remaining code has been added above – Fatema Merchant Jan 24 '13 at 08:51
  • You're almost certainly corrupting the free space arena elsewhere. And it could be anywhere else; the error doesn't necessarily bear any relationship to where the symptom appears. – James Kanze Jan 24 '13 at 08:54
  • You need to think about what you're showing us. Where does `card->ptrIssuResrc` come from. Stop asking questions for a bit and follow our pointers and think about your problem in a new light. – Peter Wood Jan 24 '13 at 08:57
  • @JamesKanze -- Everything else was working fine, till I added the code snippet above. How do I debug this further ? – Fatema Merchant Jan 24 '13 at 08:58
  • 1
    Everything else is *not* working fine, or this would not be surfacing memory corruption. Again, consider your `card` class carefully (since we cannot, you *have* to). You have this line as the very first in your question: `ptrIssuResrc = new IssuResource();`. *Where* is that code in your **real** code? Is it in the constructor or a member function of `card`? Are `card`s ever copied? Does `card` implement the [Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) if it has live dynamic pointers?, etc. – WhozCraig Jan 24 '13 at 09:18
  • 1
    @FatemaMerchant You have undefined behavior elsewhere, which results in writing some memory which you shouldn't. You only see the error if the affected memory contains important data, and only then when you try to use the data. In this case, what you're writing is data needed to manage the heap, and `operator new` is the innocent victim who uses it. – James Kanze Jan 24 '13 at 10:15
  • @FatemaMerchant Don't debug. Write a unit test. – Peter Wood Jan 24 '13 at 12:43
  • @WhozCraig -- I'm still working on this and have finally found some more details. Would be glad if someone could help please.The class card has a nested class IssuResource. The new I have mentioned in the first line is called in the constructor of card class. Now the card class has a placement new used. My question is in case of nested class, do we have to add the size for placement new. The placement new is called like this: buf = (UINT32 *)sizeof(CardChop); card = new (buf) CardChop. CardChop is the base class of card class. – Fatema Merchant May 23 '13 at 08:52

2 Answers2

2

We can't really help you as we are not familiar with your code. Your class definition doesn't tell us everything - we should know what exactly happens with your object during executing a code. If you can't use Valgrind, I would recommend you using AppVerifier + DebugDiag, which are free and run on Windows OS.

Those applications helped me a lot with memory corruption. I was struggling for a really long time to find out what's wrong with my code, and thanks to them it only took a few minutes to do this.

Here is a link to good tutorial of how to use them: http://blogs.msdn.com/b/lagdas/archive/2008/06/24/debugging-heap-corruption-with-application-verifier-and-debugdiag.aspx

It will basically tell you in which place a heap corruption appears, so it will be much easier for you to find a problem.

Hope this will help you.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
0

Well, there isn't a lot of context in Your post, but You could try to run this code in Valgrind to see if there are any memory leeks, access to uninitialized data, etc.

Probably some more code that ilustrates the problem would help...

matekm
  • 5,990
  • 3
  • 26
  • 31