c++ - Manipulating with the double pointers across function calls in VC++ -


my scenario follows:

in caller function::

idirect3dsurface9 * surf = null; func(&surf); hr = surf->lockrect(, , );  // throws exception bcoz "surf" still null. dont know why ?? 

in called function:

func(idirect3dsurface9 **surfreceive) {    surfreceive= new idirect3dsurface9*[10];   idirect3dsurface9* surfcreate = null;          hr = xyz->createoffscreenplainsurface(  width,                                                              height,                                                              formt,                                                              d3dpool_default,                                                              &surfcreate,                                                              null);         if (failed(hr))              return hr;          surfreceive[0] = surfcreate; } 

my doubt that, in caller (as have shown in code above), surf still null after caller returns back. , throws exception when call lockrect() on surf below.

hr = surf->lockrect(, , );   

it's important note "createoffscreenplainsurface() " call returning success , "surfcreate" stores right value , hence surfreceive[0] stores correct value. think making mistake in way access in caller.

your code full of bad , wrong- exhibits exception unsafety , dry violation , memory leaks, in addition stated problem. problem, exactly, not aware of difference between pointers , values. when assign surfreceive, wiping out original value , new value never returned. in addition, going have fun deleting later.

you can tell can never work, because attempt return pointer array of pointers function caller expects regular pointer.

use class-based code, gain clarity, safety, , performance in 1 go.

struct comdeleter {     template<typename t> void operator()(t* p) {         p->release();     } }; void checkd3dresult(hresult hr) { #ifdef _debug     if (failed(hr)) {         __debugbreak();     } #endif } std::unique_ptr<idirect3dsurface9, comdeleter> func() {     idirect3dsurface9* temp = nullptr;     checkd3dresult(xyz->createoffscreenplainsurface(         width,          height,          formt,          d3dpool_default,          &temp,          null));     return std::unique_ptr<idirect3dsurface9, comdeleter>(temp); } std::unique_ptr<idirect3dsurface9, comdeleter> surf = func(); checkd3dresult(surf->lockrect(...)); 

this code respects exceptions, guarantees memory cleanup, , comes @ least close respecting dry.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -