Visual C++ :: Memory Leak In Constructor?

Apr 29, 2013

I know memory leak checking can sometimes have false positives. I turned on memory leak checking by adding

Code:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

Then calling

Code:
_CrtDumpMemoryLeaks();
in the App's constructor.

I traced the issue back to the constructor for a variable included in the app class. The variable is a class I declare in the project.

Code:
//Class declaration
#define NUM_MT_COLORS 16
struct clrTable {
COLORREF clrCode;
CString clrName;

[Code] ....

I get a memory leak error for every instance where a CString is set. I tried moving this to the MainFrame just to see if there was something with this being in the app, and saw the same memory leak errors.

I didn't include the functions in this class that manipulate this table. I didn't put it in the document because this is a multi-doc application and this table is universal to the program.

Is this an example of a false positive in the memory leak checker, or did I do something wrong?

View 7 Replies


ADVERTISEMENT

C++ :: How To Delete A Memory Leak

Apr 9, 2014

is there a way to delete a memory leak ? like when the pointer is no longer pointing to that memory address, and that allocated memory is not accessible, how can i delete that ?

sample:

int *p = new int;
*p = 5;
//if i dont't delete p here, is that a way to deallocate the dynamically allocated variable above ?
p = new int; //since p is no longer pointing to 5, and there's no way of accessing it
*p = 10
delete p; //i know this only deallocates the memory address which holds 10, not the one before

View 6 Replies View Related

C/C++ :: Passing Vector As Reference Memory Leak?

Nov 30, 2014

I asked a few questions a few weeks ago about vectors and the fact that their data is stored on the heap. When a function closes, anything in its scope is destroyed, if it's passed by reference it won't be destroyed since it's outside the scope.

I have a program where I create a vector in one function, then pass it by reference to another. When I test for memory leaks, I get told I have 1 memory leak in in my start() function, and one memory leak in my save() function.

It's just a simple program that creates a vector, populates it with some numbers, then saves the numbers in a file. If I'm passing my vector by reference to another function, do I need to manually do something to avoid memory leaks? I'll post the code below.

#include <vector>
#include <iostream>
#include <iomanip>
#include <cmath>

[Code].....

View 2 Replies View Related

C++ :: Shallow Copy Of A COM Pointer (memory Leak)

Aug 25, 2012

Code:

HRESULT CAllocMeshHierarchy::CreateMeshContainer(PCTSTR Name,
const D3DXMESHDATA* pMeshData, const D3DXMATERIAL* pMaterials,
const D3DXEFFECTINSTANCE* pEffectInstances, DWORD NumMaterials,
const DWORD *pAdjacency, ID3DXSkinInfo* pSkinInfo,
D3DXMESHCONTAINER** ppNewMeshContainer

[Code] ....

When I make a shallow copy of pMeshData to OriginalMesh (for static mesh rendering, 2 objects pointing to one COM interface), memory leaks start to occur.When I don't, everything is fine.How to solve it? I addref() to it once (with pMeshData) and ReleaseCOM it in destroyMeshContainer

Remarks **** MeshData.pMesh is 0x00000000 while OriginalMesh is a pointer of some value whose values are 0xfeeefeee

View 1 Replies View Related

C++ :: Plant Tree Program Memory Leak

Mar 2, 2012

Write a C++ program that had a base class PLANT with a sub class of that of TREE. The Program works exactly to her specifications except there is a memory leak.

--Header Files --
Plant.h
Tree.h
--Resource Files--
plant.txt - For Input
tree.txt - For Input
plantReport.txt - Output of Program. No cout is really allowed by her.
--Source Files--
Plant.cpp - Code for plant.h class
Tree.cpp - Code for tree.h class
MainDriver.cpp - Contains int main()

View 14 Replies View Related

Visual C++ :: Pointing To CButtons In The Constructor?

Feb 4, 2014

I have 22 CButtons and I'm trying to set each one to the corresponding position in a CButton array of size 22 in the constructor.

Code:
*buttons[0] = m_button1;

This code gives me an error in the compiler.

error C2248: 'CObject:perator =' : cannot access private member declared in class 'CObject'

View 2 Replies View Related

Visual C++ :: How To Stop Executing Constructor

Jun 15, 2013

PHP Code:

using namespace System;
ref class A {
public:
    A(){Console::WriteLine(L"A constructor");}
    virtual void func(){foo();}
    void foo()

[Code] .....

Supposing that class A is the development code; and so for unit-testing purpose I create a mock for it. But I don't know how to override A's constructor such that it will not get executed in my unit-test code.

View 1 Replies View Related

Visual C++ :: Initializing Array In Constructor?

Mar 8, 2015

I am making a tic tac toe program in which they are asking me to have a 3x3 2 dimensional array of integers and have the constructor initialize the empty board to all zeros. They also want me to place a 1 or 2 in each empty board space denoting the place where player 1 or player 2 would move The problem I'm having is, my code initializes the board to all zeros for each element of the array, and prints it out just fine, but I can't figure out how to re-initialize the values in the array to show where each player moves on the board... I was thinking about having a default constructor that has each value set to zero, and then use a setGame function that can change the values on the board to one or two depending on where the player moves....but I don't know if that's possible.....

here is my code

Code:

// header file
#include <iostream>
#include <array>
class tictac {
public:
tictac(int);

[code]....

View 8 Replies View Related

Visual C++ :: Initialize Constructor In Array Of Objects?

Oct 28, 2012

What the best way to initialize a constructor in an array of objects?

Usually I declare an array of objects like so:

Code:
Car toyota[100];

Is this the only way I know to initialize a constructor of an array:

Code:
for (int i = 0; i < 100; i++)
{
toyota[i] = Car(x, y);
}

Is this the proper way?

View 1 Replies View Related

Visual C++ :: Constructor With Functional / Construction Via Lambda?

Feb 17, 2014

I have a class which upon construction has a part which should be executed when the constructor has finished "enough" to make the call valid.

Take the following simplified class example (the actual problem is much more elaborate):

Code:
#include <functional>
class base {
public:
base(int i, std::function<void()> func) : mi(i)

[code]....

Explicitely creating a derived class allows me to properly implement the lambda to call a member of the class.

I can't seem to figure out how to do this for an explicit instantiation of the base class.

Can this be done ? Or is this a shortcoming of VS2012 or something the standard doesn't handle ?

View 12 Replies View Related

C++ :: No Constructor Could Take Source Type Or Constructor Overload Resolution Was Ambiguous

Mar 1, 2014

i am writing this bank accounts program using structures. i haven't implemented the function before that i want to check if the data is being read and printed. When i build and run the program in visual studio it gives me the following error. "No constructor could take the source type, or constructor overload resolution was ambiguous". Now whats wrong in this program?

/* Bank Accounts Program */
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>//needed to use system() function
using namespace std;
const int MAX_NUM = 50;
struct Name{

[code]....

View 1 Replies View Related

C++ :: Calling Constructor From Constructor Set Pointer To Null

Jan 25, 2014

VS 2012 / Windows 7 64 bit

class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}

[Code] ....

it works until the myclass(int* p) calls myclass()

then the MyPointer will become CCCCCCCC (NULL value)!

is there a way to stop the second constructor from resetting the pointer value to null?

View 3 Replies View Related

Visual C++ :: Dimensions Of JPEG In Memory?

Feb 18, 2013

I have a JPEG in memory as a char* and a bytesize, if I save it to disk with a .jpg extension it's a perfect JPEG file. The thing is, I don't want to save it unless it's a minimum width/height. I got it into memory using a socket recv() call. What should I do ?

View 6 Replies View Related

Visual C++ :: Get Maximum Memory Usage Of App?

Apr 21, 2013

I use Visual C++ to write a native C++ application. How to know the maximum memory it will use during its execution?

View 5 Replies View Related

Visual C++ :: Preallocate Memory For CByteArray

Oct 26, 2013

I am using CByteArray as a buffer in Visual C++. I want to preallocate nSize memory for for CByteArray, so that later when I try to change the buffer, by calling SetSize, Add, Remove, etc., as long as all these operations are within nSize, CByteArray will not try to release the memory or reallocate the memory, so to eliminate the possibility of memory fragments in heap. Is that possible?

View 5 Replies View Related

Visual C++ :: How To Avoid Memory Fragments

Mar 4, 2015

I am developing a Visual C++ application. There is an object called CMyObject, as follows:

typedef CMap<UINT, UINT, void *, void*> CMyMap;
class CMyObject {
public:
CMyMap *m_pMyMap;
"Some other member variables"
}

Some instances of CMyObject contains a map, some not. Therefore, to save memory, I define a pointer m_pMyMap and create a new CMap object only if the instance contains a map.

When I test my app, with the increase of the CMyObject instance, the number of memory blocks allocated and deallocated is also increasing. There are a lot of fragments during this period. To prevent this, I try to override the new/delete operator for CMyObject and CMyMap. But still find many fragments. So I try to trace into the MFC source codes for CMap. I find CMap is just using an internal buffer to store the hash table(m_pHashTable), as follows:

m_pHashTable = new CAssoc* [nHashSize];

And for each hash entry, it uses:

P = (CPlex *)new BYTE[sizeof(CPlex) + nMax *cbElement];

To allocate the spaces.

I believe these two may be the reason of the memory fragments and want to eliminate them. However, is there a way to override the new/delete operator for codes such as:

new CAssoc* [nHashSize]
and
(CPlex *)new BYTE[sizeof(CPlex) + nMax *cbElement]

So that the spaces will be allocated from my own memory manager instead of from the default heap?

View 1 Replies View Related

Visual C++ :: Show Memory Leaks In Dll?

Dec 20, 2012

when ending my app in the debugger, memory leaks will be shown like this:

Detected memory leaks!

Dumping objects ->
C:PROGRAM FILESVISUAL STUDIOMyProjectsleaktestleaktest.cpp(20) : {18}
normal block at 0x00780E80, 64 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

How can I tell Visual c++ also to show me the memory leaks in a called dll?

View 14 Replies View Related

Visual C++ :: CDHtmlDialog - Memory Management

Feb 27, 2014

I have a MFC app that has a CDHtmlDialog embedded in it. During run time i update the HTML content from the C++ code. There's a IMAGE tag in the content and the SRC for the tag is updated multiple times in a second to show different images.

Basically i go over a WHILE loop in the C++ and call a JavaScript function to update the "src" for the "img" tag.

The issue am seeing is that, after running this code for a while the application kinda hangs and the system takes up lot's of memory.

How to solve this, as all the code that's in the browser side of app is HTML & JavaScript. I looked through the C++ code plugged all memory leaks there.

View 1 Replies View Related

Visual C++ :: Debugging - Revisiting Same Memory State?

Oct 31, 2013

When running my code in Visual Studio, there is a particular point in the code where my program is crashing. To debug this, I am adding a break point in Debug mode just before that point, and observing what happens as I step through the code. However, to get to this break point in the code takes about a minute of running the program. So I'm wondering if there is a tool in Visual Studio to reload the state of a program's memory from a previous run, so that I can immediately get to the break point without having to wait for a minute each time?

View 6 Replies View Related

Visual C++ :: How To Calculate PCA / Memory Access Violation

Mar 23, 2013

I am trying to use PCA, but it gives memory access violation error. Here is my sample code.

Code:
int main(...) {
.................
vector<float>input_array;
for(i=0;i<number_of_lines;i++) {
for(j=0;j<feature_vector_size;j++) {
input_array.push_back(read_feature[i][j]);

[code]....

View 1 Replies View Related

Visual C++ :: Out Of Memory And Driver Could Not Be Loaded Due To System Error 8

Jan 21, 2013

I am getting "driver could not be loaded due to system error 8" error while connecting to SQL Server 2005 from VC++. Its also throwing out of memory error. Basically i am developing and ISAPI dll. I use the following code to connect to DB.

CDatabase DBConnection;
if(! DBConnection.IsOpen()) {
DBConnection.OpenEx("Driver={SQL Server};Server=10.120.110.30;Database=Test;Trusted_Connection=yes;", CDatabase:penReadOnly | CDatabase::noOdbcDialog);

[Code] .....

for CDatabaseConnection, i can see 2 different method to open the connection, OpenEx and Open. Whats the difference between OpenEx and Open?

View 7 Replies View Related

Visual C++ :: How To Surround Memory Allocation Area By Try-catch Block

Aug 6, 2013

I am using new operator, I don't recall what the allocator's name is. But what is the corresponding Exception (or derived classes) any try-catch block can cope with?

View 1 Replies View Related

Visual C++ :: System Access Violation Exception / Attempted To Read Or Write Protected Memory

Mar 17, 2013

I have CAN Dll program for my application,which was separately used.Now I have included the Drivers program into my application Program and I am having this error System Access Violation Exception:Attempted to read or write protected memory.i am attaching the Dll code and Application code which is throwing this error.

[CODE]

The CAN Dll Code

int receive_data(unsigned char *data_output, int *MsgId, XLportHandle g_xlPortHandle){
XLstatusxlStatus, xlStatus_new;
XLevent xlEvent;
unsigned intmsgsrx=RECEIVE_EVENT_SIZE;
char *local_data ="11111111";
DWORD status;
xlStatus_new = xlSetNotification(g_xlPortHandle, &h, 1);

[code]...

My Application Code which is the receiver thread for accessing the messages got onto the CAN bus.

DWORD WINAPI Rcv_Msg(LPVOID param){
int*MsgId = new int;//msg id from CAN
intRcvVal;//0 = there is data in the queue; 1 = there is no data
unsigned int uMsgId;
*MsgId = 0;
unsigned char CanData[8];

[code]...

View 1 Replies View Related

C++ :: Using Constructor Within Constructor In Same Class

Feb 28, 2012

I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:

error: type "mainClass" is not a direct base of "glavna"

This is the program I tried:

Code:
class mainClass {
private:
int x,y;

Code] ......

View 6 Replies View Related

C++ :: Derived Class Constructor Using Base Class Constructor?

Jan 1, 2013

Is this example correct? This example from a book

Constructor of the Base Class
Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
name = n;
nationality = nat;
sex = s;
}

Constructor of the Derived Class (inherited from the base class)

Student(char* n, int s=0, char* i=""):
Person(n, s)

Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?

View 5 Replies View Related

C++ :: Can One Constructor Of A Class Call Another Constructor Of The Same Class

Mar 19, 2015

to initialize this object? Why C++ FAQ says no? Here is my code,

Code:
class A
{
public:
A(int x, char c);
A(int x);

[code] ....

I don't have any trouble to call the constructor A(int x, char c) from another constructor A(int x).

View 10 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved