-2

Im trying to push back a pointer of a newly created object into a vector but for some reason I get a access violation with something related to scalar delete operator

Here is where the crash happens:

Exception thrown at 0x0F9366CB (ucrtbased.dll) in Battleship.exe: 0xC0000005: Access violation reading location 0xCDCDCDBD.

The file where the crash happens:

//
// delete_scalar.cpp
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>



void __CRTDECL operator delete(void* const block) noexcept
{
    #ifdef _DEBUG
    _free_dbg(block, _UNKNOWN_BLOCK); // *****Crashes here*****
    #else
    free(block);
    #endif
}

This is how I allocate memory:

NetSocket* newSocket_ptr = new NetSocket();
m_sockets.emplace_back(newSocket_ptr);
0xd34dc0de
  • 493
  • 4
  • 10
  • 4
    Please do not post images. – PaulMcKenzie Nov 21 '17 at 17:18
  • @PaulMcKenzie Why should I not ? – 0xd34dc0de Nov 21 '17 at 17:19
  • 2
    Post code, not images. Those links could go dead at anytime, making this question worthless for anyone linking to it. Second, it is impossible to duplicate your issue if we wish to do so, unless we type in everything ourselves. Third, when we give the answer, we have to type in your code to show you your error instead of simply copying and pasting it into our answer. BTW, the answer is your use of `malloc`. Why are you using `malloc`? It is not creating `NetSocket` objects. – PaulMcKenzie Nov 21 '17 at 17:20
  • @PaulMcKenzie thank you for clearing this out I will write everything down – 0xd34dc0de Nov 21 '17 at 17:21
  • Are you sure an object was actually created? I see a malloc but where do you construct the object? Also try to find a way to do it without malloc/free. – Carlos Nov 21 '17 at 17:21
  • 0xCDCDCDBD this is likely related to uninitialized heap memory. I expect you are using a pointer that was never initialized. Related: https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – drescherjm Nov 21 '17 at 17:29
  • https://stackoverflow.com/help/mcve – Benjamin Lindley Nov 21 '17 at 17:29
  • Does NetSocket have a destructor? Does it free any memory? – drescherjm Nov 21 '17 at 17:33
  • Yes it does but I think it is related to a structure inside my NetworkSocket Class wich seems like it failed to be allocated – 0xd34dc0de Nov 21 '17 at 17:35
  • So now you know what to fix. In the constructors for `NetSocket` you may want to just initialize all pointers to nullptr. Also be careful to follow the rule of 3 / 5 / 0. – drescherjm Nov 21 '17 at 17:41
  • 1
    I believe your `Network` object hasn't been initialised, possibly because you used `malloc` for that as well. Don't use `malloc`. Find the person who told you to use `malloc` and tell them that you will never speak to them about C++ again. – molbdnilo Nov 21 '17 at 18:30

3 Answers3

1

You are only calling malloc, never constructing the object itself. Try using new instead of malloc.

N00byEdge
  • 1,106
  • 7
  • 18
1

I think, you should write

NetSocket *newSocket_ptr = new NetSocket();

instead of

NetSocket *newSocket_ptr = (NetSocket*)malloc(sizeof(NetSocket));
1

Do not use malloc to create objects. One issue is that your NetSocket class has a user-defined constructor, thus your NetSocket objects require construction.

The malloc function knows nothing about C++ objects and construction. Thus you are placing invalid NetSocket objects into the vector, causing issues on down the line if you actually tried to use one of those ill-formed NetSocket objects.

You need to use new NetSocket or better, have a container of smart pointers.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45