0

The Problem is the following:

There is the call of a constructor, where a pointer is an argument

 m_pszBuf= new char[260];
//Still valid , to prove i'm printing the address
    std::cout <<"Address "<< (void*)m_pszBuf <<std::endl;
    device = new Device(m_pszBuf);


Device::Device(char* _ptr){
strcpy(dest,_ptr);
}

Interesting is, before the call of the constructor, the pointer is still valid and has an address and value, but as soon as it entered the ctor, it becomes a bad pointer (0x0000005c). In addition, it is working in the debug mode but not in the release mode.

okaerin
  • 789
  • 5
  • 23
  • 2
    Well that first line isn't valid (assuming you can't convert a `Foo*` to `Foo`). We'll need to see more code. Where does `ptr` come from? – Joseph Mansfield Feb 28 '13 at 10:47
  • 1
    I hope you mean `Foo* f = new Foo(ptr)` – UmNyobe Feb 28 '13 at 10:47
  • What does `ptr` point to? – juanchopanza Feb 28 '13 at 10:47
  • 2
    post some real code please.By oversimplifying it, you/we are missing the problematic bits – user18428 Feb 28 '13 at 10:54
  • just edited the code to the real content what its doing – okaerin Feb 28 '13 at 11:04
  • 1
    Unless "the real code" doesn't compile due to "undeclared type: Device", etc.. its *not* "the real code". Further, since this is a dynamic member, (and Device takes it for seemingly equally evil nefarious purposes), read [The Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)) and ensure all pointer-owned resources (of which you should strive to have **none**) are properly managed and accounted for in copy constructors, assignment operators, and destructors. – WhozCraig Feb 28 '13 at 11:16
  • 1
    Post minimal code that demonstrates your problem and still compiles. If you knew what was unimportant and safe to eliminate you would not need to ask for help. This does not mean post lots of code: rather trim everything you think is irrelevant, periodically confirming the problem still occurs, until you have something short yet complete. Or, write a short program you think will exhibit the problem, confirm it, then post it. – Yakk - Adam Nevraumont Feb 28 '13 at 12:28

2 Answers2

4

You initialize m_pszBuf like this:

m_pszBuf = new char[260];

then you call Device constructor like this:

device = new Device(m_pszBuf);

Inside Device constructor, there is a strcpy call from m_pszBuf source:

Device::Device(char* _ptr) // _ptr == m_pszBuf
{
    strcpy(dest, _ptr);
}

But if m_pszBuf is not NUL-terminated, strcpy doesn't stop at the end of the allocated buffer, and it can copy garbage from out-of-bounds memory, and you can overrun the dest buffer.

So, before passing m_pszBuf to Device constructor, make sure that it is NUL-terminated and that strcpy destination pointer is big enough.

Note: This analysis is based just on the code snippet you showed. (I don't know if in your actual code that you omitted to show there are other problems.)

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • 2
    +1 this is likely spot-on. The debug-allocator commonly zero-initializes memory (which I detest as it makes finding issues like this much more difficult in debug-builds). Nice catch. – WhozCraig Feb 28 '13 at 11:18
  • it is not about the destination, it is the source that becomes an invalid pointer inside of the constructor. It stops to point to anything. – okaerin Feb 28 '13 at 11:39
  • @thebaconing: probably because `strcpy` (or something else?) overwrites it? It's hard to do _"psychic debugging"_ :) You may want to provide a compilable code snippet that shows the problem. Consider also @WhozCraig's comment about the _Rule of Three_ (or if the `Device` class doesn't have a copy semantics, declare `private` copy constructor and `operator=`). But have you tried following my suggestions and NUL-terminating `m_pszBuf`? (Or just use `memcpy` with proper size instead of `strcpy`...). – Mr.C64 Feb 28 '13 at 11:43
  • +1 @thebaconing if you ain't sure about that, replace strcpy with a char per char copy in a while/for loop and see if the problem still happens. – user18428 Feb 28 '13 at 13:38
0

Great input but i have solved it with something else. I had some Header files "out of sync" which were used for a library. i just needed to update them. Strange how this affected something else.

okaerin
  • 789
  • 5
  • 23