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
ADVERTISEMENT
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
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
View Related
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
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
Jun 20, 2013
I have written this code, and at first glance it does what I want, however I am worried that
a) I am overwriting the array that is apssed from chord.getPattern()
b) Im getting a memory leak that I want to get rid of, and
c) is there generally a /what is the neater way to do it:
Code:
uint8_t* ChordBuilder::invert(uint8_t count, Chord chord) {
temp = chord.getPattern();
chord.invert(true);
//TODO count is how many times to invert. Moves root aswell however
for (uint8_t i = 0; i < count; i++){
[Code] ....
temp is a member variable of ChordBuilder - and is expressed as: Code: uint8_t* temp; I dont want the pattern that chord stores, and passes with getPattern() to change - I fear it is at the moment?
I would rather not use the "new" but I cant think how to get rid of it, however Im not sure where I would need to put the "delete"?
View 7 Replies
View Related
Nov 24, 2014
I want to copy over values from x to y, and its not possible as i cant use "y" as a index in my pointer array.
Code:
CellularPhoneStock::CellularPhoneStock(CellularPhoneStock &origObj){
this->phonez[nrOfPhones]->setColour = origObj.getColour();
/*this->model = origObj.model;
this->nrOfType = origObj.nrOfType;
this->price = origObj.price;*/
nrOfPhones++;
}
My questions is, how can I use origObj and phonez combined to use the function getColour() ?
Btw, phones = CellularPhone * phonez[30];
View 14 Replies
View Related
Mar 29, 2014
Suppose you a class declared with a pointer initialization variable. When writing the copy constructor, how would one make a deep copy of the pointer variable? Can it be done in the same manner as automatic variables i.e. in the initialization list or in some other manner?
I am using C++ 11.
View 14 Replies
View Related
Feb 22, 2013
I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.
private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];
[Code].....
View 9 Replies
View Related
Jan 14, 2015
Is there a way to copy a derived class object thru a pointer to base?
For example:
class Base { public: Base( int x ) : x( x ) {}
private: int x; };
class Derived1 : public Base { public: Derived( int z, float f ) : Base( z ), f( f ) {}
private: float f;};
class Derived2 : public Base { public: Derived( int z, string f ) : Base( z ), f( f ) {}
[Code] ....
The question is whether *B[0] would be a Derived1 object and *B[1] a Derived2 object?If not, how could I copy a derived class thru a pointer to the base class?
View 1 Replies
View Related
Sep 19, 2014
Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.
You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.
#include <iostream>
#include <string>
using namespace std;
class Obj {
public:
string name;
Obj(string name): name(name) {cout << "create " << this << endl;}
[code]....
In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
View 3 Replies
View Related
Aug 3, 2013
I am having a problem with a small exercise program. The program works the first time through the loop but not the second. It faults on this line:
cin >> *(pint + i1);, with this error: pint <Unable to read memory>.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int MAX(5);
int count(4);
int value(MAX);
[Code] ....
View 5 Replies
View Related
Jan 24, 2014
I wrote the following C++ constructor, and I get an error - BUFFER too small on strcpy_s
Trace::Trace(const char *str) {
if (str) {
int len = strlen(str);
this->m_name = new char[len+1]; // asking for 'len+1' memory elements of char
strcpy_s(m_name, len, str); // **** I get here an error "BUFFER TOO SMALL" ****
[Code] .....
m_name is a private data member of type char* .
View 3 Replies
View Related
Apr 29, 2013
What I'm trying to do is:
int *p;
someType memoryLocation;
cout<<"Enter your memory location: ";
cin >> memoryLocation;
p = memoryLocation;
cout << *p;
I was just messing around with some code, and was curious to if this was possible.
View 6 Replies
View Related
Aug 26, 2013
I'm working with memory mapped files and I have a block of memory that I've mapped to.
I want to write a function that returns a pointer to a portion of the mapped memory at an offset and length so I can write to it. I've never worked with memory at this level, is what I'm attempting possible?
I know that mapping functions can map to a part of the file at length and offset but I'm not sure if I should make multiple calls to map the memory from the file or just map the memory once and work with the portions I'm interested in using my proposed GetMemory function.
Code:
LPVOID m_lpData;
LPVOID GetMemory(DWORD pos, DWORD length) {
BYTE* buffer = (BYTE*)m_lpData;
buffer += pos;
// how to get a length of the memory?
return ((LPVOID)buffer);
}
View 14 Replies
View Related
Nov 5, 2014
There is a part in the lesson that explains how malloc is used to allocate free memory to a pointer and gives us 2 examples:
Code:
float *ptr = malloc( sizeof(*ptr) ); and Code: float *ptr;
ptr = malloc( sizeof(*ptr) );
From my logic in the first case we allocate the memory to *ptr and in the second to ptr.
It's a bit confusing, am I missing something?
View 14 Replies
View Related
Jun 3, 2013
I have an integer pointer and i want its address without allocating memory,
main() {
int *a;
cout<<a;
}
This is giving 00000000 and its but obvious. Now if i use address of a (&a) along with *a,
main() {
int *a;
cout<<a;
cout<<&a;
}
'cout<<a' gives me a constant address but 'cout<<&a' gives me different address.
what is the reason behind & and why behaviour of 'cout<<a' changes when using with &.
View 8 Replies
View Related
Aug 10, 2014
If I do this:
void* testPtr = malloc ( 1000 )
And then this:
testPtr = realloc ( testPtr, 500 )
Will the realloc just reduce the allocated size and keep the same pointer, or can there be a chance of it finding another place for that allocation ( Meaning that it will expensively move the memory to another location )?
I am trying to create efficient programs by making my dynamic allocations the least resource hungry as possible during runtime.
View 2 Replies
View Related
Feb 13, 2013
I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:
unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);
where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:
double* Rin = new double[length];
and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.
View 1 Replies
View Related
Feb 25, 2015
An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.
#include<iostream>
using namespace std;
class vectorOfint{
int* x;
int size;
public:
vectorOfint();
[Code] .....
View 6 Replies
View Related
Jul 29, 2014
I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.
Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.
Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?
///////////////////////////////////////////////////////////////////////////
"MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info
188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114
189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128
190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128
[Code] .....
View 4 Replies
View Related
Jan 8, 2014
I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.
If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.
Why? I want it to output 1000. If I change that to 500, I want it to output 500.
int *a;
a = malloc(1000 * sizeof(int));
int j = sizeof(&a);
printf("%d", j);
I want to build my skills where I can allocate, inspect and change memory sizes.
View 4 Replies
View Related
Jun 26, 2014
If I have a pointer variable indicating memory location in which we have stored what user entered and the pointer is of type volatile if the user gives the character 'a' twice , then this character will not be fetched twice from the memory but only when the character is changed???
This is the one meaning of the volatile? the other is that the value will be changed without the program itself change it?
View 4 Replies
View Related
Jan 17, 2014
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
bool deleteNode(struct node *head, struct node *delptr)
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Nov 27, 2013
I have to write a program to print pascals triangle and stores it in a pointer to a pointer , which I am not entirely sure how to do. I also have to write the file and read it, then create a binary file. Assignment is attached. I am not the best with programming and especially with pointers. I will post my code below.
Code:
#include <stdio.h>
#include <stdlib.h>
void writePascalTriangle(char *fileName, int heightOfTriangle, int **triangle) {
FILE *fp;
fp=fopen("writePascalTriangle.txt", "w");
[Code] ....
View 4 Replies
View Related